题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 19514 Accepted Submission(s): 11726
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
Recommend
一道求最小逆序数的题目。。。给一个最初的0~n-1的排列,每次都将最后一个数拿到最前面,求最小的逆序数。
思路:用O(nlogn)复杂度求出最初逆序数后(用线段树做的),就可以用O(1)的复杂度分别递推出其他解。。。
顺便规范了一下线段树的写法。。
#include <iostream>
#include <cstdio>
#include <cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int maxn=5555;
int sum[maxn<<2];
int a[maxn];
void PushUP(int rt)
{
sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt)
{
int m;
sum[rt]=0;
if(l==r)
return ;
m=(l+r)>>1;
build(lson);
build(rson);
}
int query(int L,int R,int l,int r,int rt)
{
int m;
if(L<=l&&R>=r)
{
return sum[rt];
}
int ans=0;
m=(l+r)>>1;
if(L<=m)
ans+=query(L,R,lson);
if(R>m)
ans+=query(L,R,rson);
return ans;
}
void update(int pos,int l,int r,int rt)
{
int m;
if(l==r&&l==pos)
{
sum[rt]++;
return ;
}
m=(l+r)>>1;
if(pos<=m)
update(pos,lson);
else
update(pos,rson);
PushUP(rt);
}
int main()
{
int i,n,sum;
while(~scanf("%d",&n))
{
sum=0;
build(0,n-1,1);//这里下标是0~n-1的原因是原题中要求输入的数0~n-1的排列
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
sum+=query(a[i],n-1,0,n-1,1);//从头到尾,对于每个值a[i],查询从a[i]到n-1区间的和,因为sum[]数组的作用是每次出现了
//某个值a[i],就加1,,所以求区间内的和就相当于求有多少个比a[i]大的数在a[i]之前出现了,即求a[i]的逆序数(只求在a[i]前面的)
update(a[i],0,n-1,1);//对于此时遍历到的a[i],更新每个有a[i]的区间,使得sum[]加1
}
int res=sum;
for(i=0; i<n; i++)
{
sum+=n-1-2*a[i];//这里要注意了!!!!!如果我们要从头到尾遍历的话,就要加上比a[i]大的数字,减去比a[i]小的数字
//(因为是要求从后往前拿的,所以移动到最前面位置的那个时,就相当于后面的已经全部都移动到它前面去了。。。。如果是从后面便利的话
//就是每次减去比a[i]大的,加上比a[i]小的)
res=min(res,sum);
}
printf("%d\n",res);
}
return 0;
}