我是靠谱客的博主 调皮服饰,这篇文章主要介绍HDU1394 Minimum Inversion Number【线段树 点修改+区间查询】Minimum Inversion Number,现在分享给大家,希望可以做个参考。

Minimum Inversion Number

http://acm.hdu.edu.cn/showproblem.php?pid=1394

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 26274    Accepted Submission(s): 15429


 

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

复制代码
1
2
3
10 1 3 6 9 0 8 5 7 4 2

Sample Output

复制代码
1
16

Author

CHEN, Gaoli

 

Source

ZOJ Monthly, January 2003

 

题意

有一个n个整数的排列,这n个整数就是0,1,2,3...n-1这n个数(但不一定按这个顺序给出)。现在先计算一下初始排列的逆序数,然后把第一个元素a1放到an后面去,形成新排列a2 a3 a4...an a1,然后再求这个排列的逆序数。继续执行类似操作(一共要执行n-1次)直到产生排列an a1 a2...an-1为止。计算上述所有排列的逆序数,输出最小逆序数。

思路

为了方便处理,我们统一将输入的数加一,这样数的范围就是[1,n]。设ans表示初始数列的逆序数,a[i]表示当前数列的第一个数,那么将a[i]移动到最后形成的新的数列的逆序数为多少?是不是ans2=ans-(a[i]-1)+(n-a[i])=ans+n+1-2*a[i],很容易想到,就是在原来的逆序数的基础上进行加减,减a[i]-1是因为把a[i]移动到最后了,原本和a[i]构成逆序对的(x,a[i]),x<a[i],x共a[i]-1个,变成了顺序对了,所以要减(a[i]-1),同理,原本和a[i]构成顺序对的(x,a[i]),x>a[i],x共n-a[i]个,变成了逆序对了,所以要加(n-a[i])。

那么现在问题的关键就是求出初始数列的逆序数,此处使用线段树进行求解,设结点rt表示的区间是[l,r],那么结点rt存放的信息为[l,r]范围内的数出现的次数,我们可以这样求初始数列的逆序数,求出每个数前面比它大的数的数量,具体看代码

C++代码

 

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include<iostream> #include<cstring> #include<algorithm> using namespace std; #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 const int N=5010; int a[N],sum[N<<2]; void pushup(int rt) { sum[rt]=sum[rt<<1]+sum[rt<<1|1]; } //单点修改 sum[L]+=1 void update(int L,int l,int r,int rt) { if(l==r) { sum[rt]+=1; return; } int m=(l+r)>>1; if(L<=m) update(L,ls); else update(L,rs); pushup(rt); } //区间查询,查询当前线段树中值的范围在[L,R]中的数的数量 int query(int L,int R,int l,int r,int rt) { if(L<=l&&r<=R) return sum[rt]; int m=(l+r)>>1; int res=0; if(L<=m) res+=query(L,R,ls); if(R>m) res+=query(L,R,rs); return res; } int main() { int n; while(~scanf("%d",&n)) { //由于建树是让sum[i]=0,因此直接将sum数组置0就能代替建树过程 memset(sum,0,sizeof(sum)); int ans=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); a[i]++;//让a[i]加一,使得a[i]在[1,n]的范围而不是原来的[0,n-1],这样更好处理 //查询a[i]的前面有多少数比a[i]大,即查询多又是个元素在[a[i]+1,n]的范围内 if(a[i]<n) ans+=query(a[i]+1,n,1,n,1); update(a[i],1,n,1); } //ans就是初始数列的逆序数,现在求变化n-1次的过程中的逆序数,求最少数 for(int i=1,tmp=ans;i<n;i++) { tmp=tmp+n+1-2*a[i];//通过前一个状态下的逆序数求现在这个状态的逆序数 ans=min(ans,tmp);//取最少值 } printf("%dn",ans); } return 0; }

 

最后

以上就是调皮服饰最近收集整理的关于HDU1394 Minimum Inversion Number【线段树 点修改+区间查询】Minimum Inversion Number的全部内容,更多相关HDU1394内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(94)

评论列表共有 0 条评论

立即
投稿
返回
顶部