HDU 1394 Minimum Inversion Number(树状数组/归并排序实现

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24067    Accepted Submission(s): 14252


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
 
 
题意:给你一个1~n的全排列,让你恢复成逆序的情况,只能改变首尾的位置
题解:一开始没思路准备暴力,冷静分析一波肯定会T,正确的思路是,为了形成全排列,那么第一个数a[1]对于形成全排列的逆序的贡献就是比他小的那些数即a[1]-1,如果把第一个数放到最后面,那么我们所得到的贡献就是当前的逆序对个数 - 第一个数形成全排列数的贡献 + 比第一个数大的贡献(即有多少个比他大的个数) 这是移动一次的结果,保存移动n次后的最小值即位所求
此处得出逆序对的公式:ans+=n-a[i]-(a[i]-1)
 
 
归并排序方法,直接用归并排序找出逆序对的个数,然后再用求逆序对的公式求得答案即可
 
代码如下:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int  bit[maxn];
int a[maxn];
int n;
int lowbit(int x){
    return x&-x;
}
int  sum(int i){
    int ans=0;
    while(i>0){
        ans+=bit[i];
        i-=lowbit(i);
    }
    return ans;
}

void update(int i){
    while(i<=n){
        bit[i]++;
        i+=lowbit(i);
    }
}


int main(){
    while(scanf("%d",&n) !=EOF){
        memset(bit,0,sizeof(bit));
        long long  ans=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            a[i]++;
            ans+=sum(n)-sum(a[i]);
            update(a[i]);
        }
        long long  minn=ans;
        for(int i=1;i<=n;i++){
            ans+=n-a[i]-(a[i]-1);
            minn=min(ans,minn);
        }
        printf("%lld\n",minn);
    }
}
View Code
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
int a[maxn];
int b[maxn];
int c[maxn];
int n;
int ans;
void mergsort(int l,int r){
    if(l==r) return;
    int mid=(l+r)>>1;
    mergsort(l,mid);
    mergsort(mid+1,r);
    int i=l;
    int j=mid+1;
    int pos=0;
    while(i<=mid&&j<=r){
        if(a[i]<=a[j])
            b[pos++]=a[i++];
        else{
            b[pos++]=a[j++];
            ans+=(mid-i+1);
        }
    }
    while(i<=mid) b[pos++]=a[i++];
    while(j<=r) b[pos++]=a[j++];
    for(i=0;i<pos;i++) a[i+l]=b[i];
}


int main(){
    int n;
    while(~scanf("%d",&n)){
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
        c[i]=a[i];
    }
    ans=0;
    mergsort(0,n-1);
    int minn=ans;
    for(int i=0;i<n;i++){
        ans+=(n-c[i]-1-c[i]);
        minn=min(minn,ans);
    }
    cout<<minn<<endl;
    
    }

}
View Code

 

 
 
posted @ 2018-08-08 10:24  buerdepepeqi  阅读(140)  评论(0编辑  收藏  举报