首页 > 代码库 > hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)
hdu1394--Minimum Inversion Number(线段树求逆序数,纯为练习)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10326 Accepted Submission(s): 6359
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.
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个逆序数,找出最小值对每种情况都求逆序数,可以每次都归并排序,或树状数组,或线段树,也可以由上一个的逆序数推出;a[1] , a[2] , a[3] 。。。a[n],将a[1]放到最后,然后将a[2]放到最后,可以找到规律,将首个a[i]放到最后时,逆序数增加了 a[i]之前比a[i]大的,增加a[i]之后比a[i]大的,减小了a[i]之前比a[i]小的,减小了a[i]之后比a[i]小的,又因为每次给出的数n个数在0到n,且都不同,最后得出 逆序数会增加 n-a[i]个,减少a[i]-1个#include <cstdio> #include <cstring> #define INF 0x3f3f3f3f #include <algorithm> using namespace std; int tree[100000] , p[6000] , q[6000]; void update(int o,int x,int y,int u) { if( x == y && x == u ) tree[o]++ ; else { int mid = (x + y)/ 2; if( u <= mid ) update(o*2,x,mid,u); else update(o*2+1,mid+1,y,u); tree[o] = tree[o*2] + tree[o*2+1]; } } int sum(int o,int x,int y,int i,int j) { int ans = 0 ; if( i <= x && y <= j ) return tree[o] ; else { int mid = (x + y) /2 ; if( i <= mid ) ans += sum(o*2,x,mid,i,j); if( mid+1 <= j ) ans += sum(o*2+1,mid+1,y,i,j); } return ans ; } int main() { int i , j , n , min1 , num ; while(scanf("%d", &n)!=EOF) { min1 = 0 ; memset(q,0,sizeof(q)); for(i = 1 ; i <= n ; i++) { scanf("%d", &p[i]); p[i]++ ; } memset(tree,0,sizeof(tree)); for(i = 1 ; i <= n ; i++) { min1 += sum(1,1,n,p[i],n); update(1,1,n,p[i]); } num = min1 ; for(i = 1 ; i < n ; i++) { num = num + ( n - p[i] ) - (p[i] - 1) ; if( num < min1 ) min1 = num ; } printf("%d\n", min1); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。