Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 13558 Accepted Submission(s): 8277
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
Author
CHEN, Gaoli
Source
线段树求逆序数 !!!!!
以前只是听说过 ,现在完美解决
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
int sum=0;
const int INF=5005;
struct Tree
{
int left,right,count;
} tree[5005<<2];
int create(int root,int left,int right)
{
tree[root].left=left;
tree[root].right=right;
if(left==right)
return tree[root].count=0;
int a,b,mid=(left+right)>>1;
a=create(root<<1,left,mid);
b=create(root<<1|1,mid+1,right);
return tree[root].count=a+b;
}
int cal (int root,int left,int right)
{
if(tree[root].left>right||tree[root].right<left)
return 0;
if(tree[root].left>=left&&tree[root].right<=right)
return tree[root].count;
int a,b,mid=(tree[root].left+tree[root].right)>>1;
a=cal(root<<1,left,right);
b=cal(root<<1|1,left,right);
return a+b;
}
void update(int root,int pos,int val)
{
if(tree[root].left==tree[root].right)
{
tree[root].count=1;
return ;
}
int mid=(tree[root].left+tree[root].right)>>1;
if(pos<=mid)
update(root<<1,pos,val);
else
update(root<<1|1,pos ,val);
tree[root].count=tree[root<<1].count+tree[root<<1|1].count;
}
int main()
{
int n,result=0 ,cnt[5004];
while(scanf("%d",&n)!=EOF)
{
result=0;
create(1,0,n-1);
for(int i=0; i<n; i++)
{
scanf("%d",&cnt[i]);
result+=cal(1,cnt[i],n-1);
update(1,cnt[i],1);
}
int RR=result;
for(int i=0; i<n; i++)
{
result=result-cnt[i]+(n-cnt[i]-1);
RR=min(RR,result);
}
printf("%d\n",RR);
}
return 0;
}