Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4067 Accepted Submission(s): 2446
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
algorithm:
1:求出序列的逆序对数ct(归并排序||树状数组||线段树)
2:按题扫一遍,ct += n - 2*a[i] -1;
归并:
#include <iostream>
#include <cstring>
#define INF 0x3f3f3f3f
#define M 5010
#define clr(x,k) memset((x),(k),sizeof(x))
using namespace std;
int a[M], b[M], c[M], n, ct;
void merge(int l, int mid, int r)
{
int i = 0, j = l, k = mid +1;
while (j<=mid && k<=r)
{
if (a[j]>a[k])
{
//p (a[k], a[j]);
b[i++] = a[k++];
ct += (mid - j + 1);
}
else
{
b[i++] = a[j++];
}
}
while (j<=mid) b[i++] = a[j++];
while (k<=r) b[i++] = a[k++];
for (j=0; j<i;j++)
a[l+j] = b[j];
}
void mergesort(int l, int r)
{
if (l<r)
{
int mid = (l+r)/2;
mergesort(l, mid);
mergesort(mid+1, r);
merge(l, mid, r);
}
return ;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
ct = 0;
clr(a,0);
clr(b,0);
for (int i=0;i<n;i++)
{
scanf("%d",&a[i]);
c[i] = a[i];
}
mergesort(0,n-1);
int mmm = ct;
for (int i=0;i<n-1;i++)
{
ct += n - 2*c[i] -1;
if (mmm>ct) mmm = ct;
}
printf("%d\n",mmm);
}
return 0;
}
树状数组:
#include <iostream>
#include <cstring>
#define INF 0x3f3f3f3f
#define M 5010
#define clr(x,k) memset((x),(k),sizeof(x))
#define lowbit(w) (w & (-w))
using namespace std;
int a[M], b[M], c[M];
int n, m, sum;
void modify(int x)
{
while (x<M)
{
c[x]++;
x += lowbit(x);
}
}
int Sum(int x)
{
int sum = 0;
while (x>0)
{
sum += c[x];
x -= lowbit(x);
}
return sum;
}
int main()
{
while (scanf("%d", &n)!=EOF)
{
clr(c, 0);
clr(b, 0);
for (int i=0;i<n;++i)
{
scanf("%d",&a[i]);
b[i] = Sum(a[i]+1);
modify(a[i]+1);
}
int ct = 0;
for (int i=0;i<n;i++)
{
ct += a[i]-b[i];
}
int mmm = ct;
for (int i=0;i<n-1;i++)
{
ct += n - 2*a[i] -1;
if (mmm>ct) mmm = ct;
}
printf("%d\n",mmm);
}
return 0;
}
线段树:
#include<cstdio>
#include<algorithm>
using namespace std;
int sum[5555<<2];
int query(int x,int y,int s,int t,int r)
{
if((x<=s)&&(y>=t))
return sum[r];
int m=(s+t)>>1;
int ans=0;
if(x<=m) ans+=query(x,y,s,m,r<<1);
if(y>m) ans+=query(x,y,m+1,t,r<<1|1);
return ans;
}
void update(int p,int s,int t,int r)
{
if(s==t)
{
sum[r]++;
return ;
}
int m=(s+t)>>1;
if(p<=m) update(p,s,m,r<<1);
else update(p,m+1,t,r<<1|1);
sum[r]=sum[r<<1]+sum[r<<1|1];
}
int main()
{
int n;
while(~scanf("%d",&n))
{
int ans=0;
int a[5555];
memset(sum,0,sizeof(sum));
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
ans+=query(a[i]+1,n-1,0,n-1,1);
update(a[i],0,n-1,1);
}
int minn=ans;
for(int i=0;i<n-1;i++)
{
ans+=n-a[i]-a[i]-1;
if(ans<minn) minn=ans;
}
printf("%d\n",minn);
}
return 0;
}
ps:线段树不是我写的。。风格。。。