博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDUOJ---1754 Minimum Inversion Number (单点更新之求逆序数)
阅读量:6976 次
发布时间:2019-06-27

本文共 2192 字,大约阅读时间需要 7 分钟。

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 9342    Accepted Submission(s): 5739


Problem Description
The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai > aj.
For a given sequence of numbers a1, a2, ..., an, if we move the first m >= 0 numbers to the end of the seqence, we will obtain another sequence. There are totally n such sequences as the following:
a1, a2, ..., an-1, an (where m = 0 - the initial seqence)
a2, a3, ..., an, a1 (where m = 1)
a3, a4, ..., an, a1, a2 (where m = 2)
...
an, a1, a2, ..., an-1 (where m = n-1)
You are asked to write a program to find the minimum inversion number out of the above sequences.
 

 

Input
The input consists of a number of test cases. Each case consists of two lines: the first line contains a positive integer n (n <= 5000); the next line contains a permutation of the n integers from 0 to n-1.
 

 

Output
For each case, output the minimum inversion number on a single line.
 

 

Sample Input
10
1 3 6 9 0 8 5 7 4 2
 

 

Sample Output
16
 

 

Author
CHEN, Gaoli
 

 

Source
 
代码:
1 //线段树实现单点更新,并求和 2 #include
3 #define maxn 5001 4 struct node{ 5 int lef,rig,sum; 6 int mid(){ return lef+((rig-lef)>>1) ;} 7 }; 8 node seg[maxn<<2]; 9 int aa[maxn+3];10 void build(int left,int right,int p )11 {12 seg[p].lef=left;13 seg[p].rig=right;14 seg[p].sum=0;15 if(left==right) return ;16 int mid=seg[p].mid();17 build(left,mid,p<<1);18 build(mid+1,right,p<<1|1);19 }20 void updata(int pos,int p,int val)21 {22 if(seg[p].lef==seg[p].rig)23 {24 seg[p].sum+=val;25 return ;26 }27 int mid=seg[p].mid();28 if(pos<=mid) updata(pos,p<<1,val);29 else updata(pos,p<<1|1,val);30 seg[p].sum=seg[p<<1].sum+seg[p<<1|1].sum;31 }32 int query(int be ,int en,int p)33 {34 if(be<=seg[p].lef&&seg[p].rig<=en)35 return seg[p].sum;36 int mid=seg[p].mid();37 int res=0;38 if(be<=mid) res+=query(be ,en ,p<<1);39 if(mid
ans) min=ans;60 }61 printf("%d\n",min);62 }63 return 0 ;64 }
View Code

 

 

转载地址:http://cbesl.baihongyu.com/

你可能感兴趣的文章
pojBuy Tickets2828线段树或者树状数组(队列中倒序插队)
查看>>
【BZOJ】1600: [Usaco2008 Oct]建造栅栏(dp)
查看>>
linux下启动oracle
查看>>
【原创】开源Math.NET基础数学类库使用(02)矩阵向量计算
查看>>
SqlHelper
查看>>
前端画面-下拉后滚动
查看>>
golang使用http client发起get和post请求示例
查看>>
remoting生命周期
查看>>
[zz]malloc()和calloc()
查看>>
Sylius – 100% 免费和开源的电子商务解决方案
查看>>
单片机系列学习
查看>>
BZOJ3571 : [Hnoi2014]画框
查看>>
读枯燥的技术书时怎么集中精神?
查看>>
【github】github 使用教程初级版
查看>>
iOS 依据文本内容为TextView动态定义高度
查看>>
信号练习
查看>>
UML类图几种关系的总结
查看>>
JavaScript学习笔记——流程控制
查看>>
CCF系列之ISBN号码(201312-2)
查看>>
JDBC学习笔记(1)——JDBC概述
查看>>