首页 > 代码库 > HDU 1394 Minimum Inversion Number (数据结构-线段树)

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): 9514    Accepted Submission(s): 5860


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
 


Author
CHEN, Gaoli
 


Source
ZOJ Monthly, January 2003
 


Recommend
Ignatius.L
 


题目大意:

求逆序数。也就是给你一个序列,每次求逆序数,然再把第一个数放到这个序列的末尾,构成新的序列。问你这n个序列的最小的逆序数。


解题思路:

1、对于每个序列,其原来的逆序数记为 pre , 如果当前把该序列 第一个数 a[0] 移动到尾部,那么新序列的逆序数为 pre-a[i]+(n-a[i]-1)

因为序列中比a[i]大的数有 n-a[i]-1 个,比a[i]小的有 a[i]个。

因此只需求出第一个序列的逆序数,依次可以递推出这n个序列的逆序数,求出最小的即可

2、求第一个序列的逆序数的方法

(1)暴力算法,据说不会超时

(2)线段树,建 [0,n]这段树,对于数据 a[i] ,先查询 (a[i]+1,n) 这段的值也就是比a[i]大的数的个数也就是 逆序数,然后插入 (a[i],a[i]) 值为1


代码:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdio>
#include <algorithm>
using namespace std;

const int maxn=5100;

struct tree{
    int l,r,sum;
}a[maxn*4];

int data[maxn],n,m;

void build(int l,int r,int k){
    a[k].l=l;
    a[k].r=r;
    a[k].sum=0;
    if(l<r){
        int mid=(l+r)/2;
        build(l,mid,2*k);
        build(mid+1,r,2*k+1);
    }
}

void insert(int l,int r,int k,int c){
    if(l<=a[k].l && a[k].r<=r){
        a[k].sum+=c;
    }else{
        int mid=(a[k].l+a[k].r)/2;
        if(r<=mid) insert(l,r,2*k,c);
        else if(l>=mid+1) insert(l,r,2*k+1,c);
        else{
            insert(l,mid,2*k,c);
            insert(mid+1,r,2*k+1,c);
        }
        a[k].sum=a[2*k].sum+a[2*k+1].sum;
    }
}

int query(int l,int r,int k){
    if(l<=a[k].l  && a[k].r<=r){
        return a[k].sum;
    }else{
        int mid=(a[k].l+a[k].r)/2;
        if(r<=mid) return query(l,r,2*k);
        else if(l>=mid+1) return query(l,r,2*k+1);
        else{
            return query(l,mid,2*k) + query(mid+1,r,2*k+1) ;
        }
    }
}

void solve(){
    int ans=0;
    build(1,n,1);
    for(int i=1;i<=n;i++){
        ans+=query(data[i]+1,n,1);
        insert(data[i]+1,data[i]+1,1,1);
        //cout<<data[i]<<" "<<ans<<endl;
    }
    int tmp=ans;
    for(int i=1;i<=n;i++){
        tmp-=data[i];
        tmp+=n-data[i]-1;
        if(tmp<ans) ans=tmp;
    }
    cout<<ans<<endl;
}

int main(){
    while(scanf("%d",&n)!=EOF){
        for(int i=1;i<=n;i++) scanf("%d",&data[i]);
        solve();
    }
    return 0;
}