Minimum Inversion Number(归并排序)
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 14952 Accepted Submission(s): 9140
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.
#include<stdio.h>
#include<string.h>
#define MIN(x,y)(x<y?x:y)
const int MAXN=;
int a[MAXN],b[MAXN],anser,c[MAXN];
void mergesort(int l,int r,int mid){
int i=l,j=mid+,k=l;
while(i<=mid&&j<=r){
if(a[i]<=a[j])b[k++]=a[i++];
else{
anser+=j-k;
b[k++]=a[j++];
}
}
while(i<=mid)b[k++]=a[i++];
while(j<=r)b[k++]=a[j++];
for(i=l;i<=r;i++)a[i]=b[i];
}
void merge(int l,int r){
if(l<r){
int mid=(l+r)>>;
merge(l,mid);
merge(mid+,r);
mergesort(l,r,mid);
}
}
int main(){
int N;
while(~scanf("%d",&N)){
for(int i=;i<N;i++)scanf("%d",a+i),c[i]=a[i];
anser=;
merge(,N-);
int temp=anser;
//printf("%d\n",anser);
for(int i=;i<N;i++){
temp=temp+(N--*c[i]);
//printf("%d %d\n",c[i],temp);
anser=MIN(anser,temp);
}
printf("%d\n",anser);
}
return ;
}
//n-a-1-(a) N-2*a+1;
總結(jié)
以上是生活随笔為你收集整理的Minimum Inversion Number(归并排序)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Codeforces - 1081C -
- 下一篇: ThinkPHP模版时间显示