hdoj 1394 Minimum Inversion Number 線段樹||樹狀數組||分治

題目:

Minimum Inversion Number

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


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

這個題求逆序數,唔,我的解法是線段樹,先挖個坑,等我學習一波樹狀數組和分治後再回來學習一個。

這個題的條件給的是比較弱的,0——N-1的一個排列,求有多少逆序數對,這個爲我們提供了方便。

思路:

求逆序數:

按順序從左到右插入,先詢問元素x到N-1之間這個區間有多少個數x,ret+=x,再把每個元素放置在它應該在的位置,以1,0,2,4,3爲例,這裏N=5,首先第一個數是1,詢問[1,4]這個區間上已經有了多少個數a,那說明這a個數在1之前已經插入了,也就是說這a個數比1大,但位置在1之前,因此ret+=a.

(0)ret=0

(1)詢問[1,4],ret+=0,插入1.

(2)詢問[0,4],ret+=1,(已經插入了1),再插入0

(3)詢問[2,4],ret+=0,再插入2

(4)詢問[4,4],ret+=0,再插入4

(5)詢問[3,4],ret+=1,(在這之前已經插入了4),再插入3

(6)結束

2,維護,暨每次把隊頭放在隊尾。我們可以推出公式:

逆序數減少:a[i]

逆序數增加:N-1-a[i]

sum+=N-1-a[i]-a[i]

ret=min(ret,sum)

code:

#include<cstdio>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define min(a,b) (a<b?a:b)
const int MAXN=5000+5;
int a[MAXN<<2];
void PushUp(int rt){
    a[rt]=a[rt<<1]+a[rt<<1|1];
}
void update(int p,int l,int r,int rt){
    if(l==r){
        a[rt]++;
        return;
    }
    int m=(l+r)>>1;
    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 a[rt];
    int m=(l+r)>>1;
    int ret=0;
    if(L<=m)ret+=Query(L,R,lson);
    if(R>m)ret+=Query(L,R,rson);
    return ret;
}
int x[MAXN];
int main(void){
    int N;
    while(scanf("%d",&N)==1){
        memset(a,0,sizeof(a));
        int sum=0;
        for(int i=0;i<N;++i){
            scanf("%d",&x[i]);
            sum+=Query(x[i],N-1,0,N-1,1);
            update(x[i],0,N-1,1);
        }
        int ret=sum;
        for(int i=0;i<N;++i){
            //printf("ret=%d\n",ret);
            sum+=N-x[i]-1-x[i];
            ret=min(sum,ret);
        }
        printf("%d\n",ret);
    }
    return 0;
}

關於求逆序數:

然後我們考慮,如果它不是一個排列,而是任意N個無重複整數,N<=1e5,a[i]<=1e9,那麼我們該怎麼做?bingo,離散化,先sort後再重新標序號,如 3,13456789,123456789,我們讓map[3]=0,map[13456789]=1,map[123456789]=2,然後又是剛纔的解法。

然後,我們再想如果有重複我們該怎麼辦,逆序數對定義,i<j&&a[i]>a[j],因此在原代碼的改進上,我們只需要訪問[a+1,N-1]之間的就好了,另比如第某個數是1,我們就看[2,N-1]中已經有多少個數了。注意排序後標序號時就ok了。

附上ugly代碼:

#include<cstdio>
#include<map>
#include<algorithm>
#include<cstring>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define min(a,b) (a<b?a:b)
using namespace std;
const int MAXN=5000+5;
int a[MAXN<<2];
void PushUp(int rt){
    a[rt]=a[rt<<1]+a[rt<<1|1];
}
void update(int p,int l,int r,int rt){
    if(l==r){
        a[rt]++;
        return;
    }
    int m=(l+r)>>1;
    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 a[rt];
    int m=(l+r)>>1;
    int ret=0;
    if(L<=m)ret+=Query(L,R,lson);
    if(R>m)ret+=Query(L,R,rson);
    return ret;
}
int x[MAXN],temp[MAXN];
int main(void){
    int N;
    while(scanf("%d",&N)==1){
        map<int,int>m;
        for(int i=0;i<N;++i){
            scanf("%d",temp+i);
            x[i]=temp[i];
        }
        sort(temp,temp+N);
        int index=0;m[temp[0]]=index;
        for(int i=1;i<N;++i)
            if(temp[i]!=temp[i-1])m[temp[i]]=++index;
            else m[temp[i]]=index;


        memset(a,0,sizeof(a));
        int sum=0;
        for(int i=0;i<N;++i){
            x[i]=m[x[i]];
            if(x[i]+1<=N-1)sum+=Query(x[i]+1,N-1,0,N-1,1);
            /*其實應該是這樣,但我們發現else 沒意義
            if(x[i]+1<=N-1)
                sum+=Query(x[i]+1,N-1,0,N-1,1);
            else
                sum+=Query(N-1,N-1,0,N-1,1)*/
            update(x[i],0,N-1,1);
        }
        printf("ret=%d\n",sum);
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章