Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11455 Accepted Submission(s): 7041
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
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
const int N =5000+10;
struct node{
int l,r ;
int c;
}a[N<<2];
void build(int l,int r,int cur){
int mid = (l + r )>>1;
a[cur].l = l ;
a[cur].r = r;
a[cur].c= 0 ;
if( l == r)return ;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
}
void update(int i,int k){
if(a[i].l == k && a[i].r == k){
a[i].c = 1;
return ;
}
int mid = (a[i].l + a[i].r )>>1;
if( k <= mid)
update(i<<1,k);
else
update(i<<1|1,k);
a[i].c = a[i<<1].c+a[i<<1|1].c;
}
int GetSum(int l,int r,int cur){
if(a[cur].l >= l&& a[cur].r <= r){
return a[cur].c;
}
else {
int mid = (a[cur].l + a[cur].r )>>1;
int ans = 0 ;
if( l <= mid )
ans += GetSum(l,r,cur<<1);
if( r > mid)
ans += GetSum(l,r,cur<<1|1);
return ans ;
}
}
int main()
{
int n;
while(scanf("%d",&n)>0){
int b[N];
build(1,n,1);
int ans = 0 ;
for(int i = 0 ; i < n ; i++){
scanf("%d",&b[i]);
ans += GetSum(b[i]+1,n,1);//每加入一个元素,就统计比它大的数
update(1,b[i]);//更新线段树
}
int _min = ans ;
for(int i = 0 ; i < n ; i++){
ans = ans + (n-2*b[i]-1);//每把第一个元素放到末尾,就是在前一个的基础上
_min=min(ans,_min);//减去b[i],加上n-b[i]-1
}
printf("%d\n",_min);
}
return 0;
}
树状数组解法:
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define mem0(a) memset(a,0,sizeof(a))
const int N = 5000+10;
int a[N],b[N],n;
int sum(int x){
int ret = 0 ;
while( x <= n){
ret += b[x];
x += (x&-x);
}
return ret;
}
void add(int x){
while(x){
b[x]++;
x -=(x&-x);
}
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n<=0)break;
mem0(b);
int _min = 0 ;
for(int i = 1 ; i <= n ; i++){
scanf("%d",&a[i]);
_min += sum(a[i]+1);
add(a[i]+1);
}
int y = _min;
for(int i = 1 ; i < n ; i++){
y+= (n-2*a[i]-1);
_min = min(_min,y);
}
printf("%d\n",_min);
}
}