Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19645 Accepted Submission(s): 11803
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.
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
贴上逆序数的定义:一个数列中逆序对的总数;
逆序对的定义:数列a[1],a[2],a[3]…中的任意两个数a[i],a[j] (i<j),如果a[i]>a[j],那么我们就说这两个数构成了一个逆序对。
题意:给出一系列数,你可以把这一串数的前m(0<m<n)移到最后(有点可旋转的意思),问在所有情况下逆序对对数的最小值
思路:我们可以先求出当前的逆序对对数ans,然后把依次这一串的第一个数加到尾部(因为是连续的数,故可以得到公式),更改ans值,过程中记录最小值即可。
方法:本题的耗时主要在于求逆序对对数上,但由于数据范围较小,可以暴力解决O(n^2),也可以用线段树和线状数组O(nlogn),不过最方便的写法就是线状数组啦
法一:暴力
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
const int maxn = 5005;
const int mod = 10005;
#define ll long long
#define rush() int T;scanf("%d",&T);while(T--)
int a[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
f(i,0,n)
{
scanf("%d",&a[i]);
}
int cnt=0;
f(i,0,n)
f(j,i+1,n)
{
if(a[i]>a[j])
cnt++;
}
int ans=cnt;
f(i,0,n)
{
cnt=cnt-a[i]+(n-a[i]-1);
ans=min(cnt,ans);
}
printf("%d\n",ans);
}
return 0;
}
法二:线段树
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
const int maxn =5005;
const int mod = 10005;
const int INF = 1e9;
#define ll long long
#define m ((l+r)>>1)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define rush() int T;scanf("%d",&T);while(T--)
int tree[maxn<<2];
int a[maxn];
void pushup(int rt)
{
tree[rt]=tree[rt<<1]+tree[rt<<1|1];
}
void build(int l,int r,int rt)
{
if(l==r)
{
tree[rt]=0;
return;
}
build(lson);
build(rson);
pushup(rt);
}
void update(int p,int l,int r,int rt)
{
if(l==r)
{
tree[rt]=1;
return;
}
if(p<=m)
update(p,lson);
else update(p,rson);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
if(L<=l&&r<=R)
{
return tree[rt];
}
int ans=0;
if(L<=m)
{
ans+=query(L,R,lson);
}
if(m<R)
{
ans+=query(L,R,rson);
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
build(1,n,1);
int sum=0;
f(i,0,n)
{
scanf("%d",&a[i]);
sum+=query(a[i]+1,n,1,n,1);
update(a[i]+1,1,n,1);
}
int Min=sum;
f(i,0,n)
{
sum=sum-a[i]+(n-a[i]-1);
Min=min(sum,Min);
}
printf("%d\n",Min);
}
return 0;
}
法三:线状数组
#include <bits/stdc++.h>
using namespace std;
#define mst(a,b) memset((a),(b),sizeof(a))
#define f(i,a,b) for(int i=(a);i<(b);++i)
typedef long long ll;
const int maxn=5005;
const int mod = 475;
const ll INF = 0x3f3f3f3f;
const double eps = 1e-6;
#define rush() int T;scanf("%d",&T);while(T--)
int n;
int tree[maxn];
int a[maxn];
int lowbit(int x)
{
return x&(-x);
}
int query(int x)
{
int ans=0;
while(x>0)
{
ans+=tree[x];
x-=lowbit(x);
}
return ans;
}
void update(int x)
{
while(x<maxn)
{
tree[x]++;
x+=lowbit(x);
}
}
int main()
{
while(~scanf("%d",&n))
{
int ans=0;
mst(tree,0);
for(int i=1;i<=n;++i)
{
scanf("%d",&a[i]);
a[i]++;
}
for(int i=n;i>0;--i)
{
ans+=query(a[i]-1);
update(a[i]);
}
for(int i=1;i<=n;i++)
a[i]--;
int Min=ans;
for(int i=1;i<=n;i++)
{
ans=ans-a[i]+(n-a[i]-1);
Min=min(ans,Min);
}
printf("%d\n",Min);
}
return 0;
}