HDU 1394 Minimum Inversion Number (线段树,单点更新)
C - Minimum Inversion Number
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Appoint description:
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.
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
题目大意:
这道题是说,给你一个长度为n的排列(由0到n-1组成),每次可以把第一个元素放到最后面,那么就会产生逆序数,一个排列的逆序数等于的是这个排列中的所有数字的逆序数的和。
那么,这道题,可以用线段树来解决,我们首先建立一个空树,也就是说,这个树中的每个节点的sum==0,表示全0,然后,我们每次插入一个数字,就在这个数字的对应位置放一个1,
也就是按照权值来建立这棵线段树,线段树最下面一层的叶子节点所维护的权值就是0~n-1。每次插入一个数字后,只要统计有多少个数字比这刚刚插入的这个数字大就可以了。
然后我们知道,对于一个仅仅由0~n-1组成的排列,把第一个数字放到最后一位,所产生的贡献是,减少了a[i],增加了n-1-a[i]个。那么每次都算一遍贡献,直到找到那个最小的贡献就可以了。
代码:
# include<cstdio> # include<iostream> using namespace std; # define MAX 5004 # define inf 99999999 # define lid id<<1 # define rid id<<1|1 struct Segtree { int l,r; int sum; }tree[MAX*4]; int a[MAX]; void push_up( int id ) { tree[id].sum = tree[lid].sum+tree[rid].sum; } void build ( int id,int l,int r ) { tree[id].l = l; tree[id].r = r; tree[id].sum = 0; if ( l==r ) { return; } int mid = ( tree[id].l+tree[id].r )/2; build ( lid,l,mid); build ( rid,mid+1,r); push_up(id); } void update( int id,int x,int val ) { if ( tree[id].l==tree[id].r ) { tree[id].sum = 1; return; } int mid = ( tree[id].l+tree[id].r )/2; if ( x<=mid ) update(lid,x,val); else update(rid,x,val); push_up(id); } int query( int id,int l,int r ) { if ( tree[id].l==l&&tree[id].r==r ) { return tree[id].sum; } int mid = ( tree[id].l+tree[id].r )/2; if ( r <= mid ) return query(lid,l,r); else if ( l > mid ) return query(rid,l,r); else { return query(lid,l,mid)+query(rid,mid+1,r); } } int main(void) { int n; while ( scanf("%d",&n)!=EOF ) { for ( int i = 0;i < n;i++ ) scanf("%d",&a[i]); build(1,0,n-1); int sum = 0; for ( int i = 0;i < n;i++ ) { if( a[i]!=n-1 ) { sum+= query(1,a[i]+1,n-1); update(1,a[i],1); } else { sum+=query(1,a[i],n-1); update(1,a[i],1); } } int ans = inf; ans = min(ans,sum); for ( int i = 0;i < n;i++ ) { sum-=a[i]; sum+=n-1-a[i]; ans = min(ans,sum); } printf("%d\n",ans); } return 0; }