Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11640 Accepted Submission(s): 7145
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 <iostream>
#include <cstdio>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include<ctime>
#define esp 1e-6
#define LL unsigned long long
#define inf 0x0f0f0f0f
using namespace std;
struct Node
{
int left,right;
int sum;
};
Node T[5005<<2];
void build(int cur,int l,int r)
{
T[cur].left=l;
T[cur].right=r;
T[cur].sum=0;
if(l!=r)
{
build(2*cur,l,(l+r)/2);
build(2*cur+1,(l+r)/2+1,r);
T[cur].sum+=T[2*cur].sum+T[cur*2+1].sum;
}
}
int query(int cur,int l,int r)
{
if(l<=T[cur].left&&T[cur].right<=r)
return T[cur].sum;
else
{
int ans=0;
if(l<=(T[cur].left+T[cur].right)/2)
ans+=query(2*cur,l,r);
if(r>(T[cur].left+T[cur].right)/2)
ans+=query(2*cur+1,l,r);
return ans;
}
}
void change(int cur,int x)
{
if(T[cur].left==T[cur].right)
T[cur].sum++;
else
{
if(x<=(T[cur].left+T[cur].right)/2)
change(2*cur,x);
if(x>(T[cur].left+T[cur].right)/2)
change(2*cur+1,x);
T[cur].sum=T[2*cur].sum+T[2*cur+1].sum;
}
}
int main()
{
int t,cas;
int n,m,i;
int num[5005<<2];
int ans;
while(scanf("%d",&n)!=EOF)
{
ans=0;
build(1,0,n-1);
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
ans+=query(1,num[i],n-1);
change(1,num[i]);
}//求出原先序列的逆序对
int tt=ans;
for(i=0;i<n;i++)
{
/*
因为序列为[0, n-1],若最前面一个数为x,序列中比x
小的数为[0, x-1], 共x个,比x大的数为[x+1, n-1],
共n-x-1个,将x移到最后,比x小的数的逆序数均减1,
x的前面比x大的数有n-x-1个,x的逆序数增加n-x-1。
所以新序列的逆序数为原序列的逆序数加上n-2*x-1。
*/
tt+=n-2*num[i]-1;
ans=min(ans,tt);
}
printf("%d\n",ans);
}
return 0;
}