hdu 1394 Minimum Inversion Number 【線段樹查詢】

ACM_e發表於2017-11-16

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


看了好久才明白題意 英語真的好差勁的說

下面有一個解析 非常的清楚 :

說一下線段樹做法O(nlogn):
以這個序列來說明:
1,9,2,3,0,8,5,7,4,6

我們先假設有一個長度為n元素全為0的陣列:
0,0,0,0,0,0,0,0,0,0

我們先計算所給序列的第一項1(實際上是第0項)的數字所對應位置之後所有元素的和(括號裡面的數),和就是當前與這個數逆序的數的個數,這樣做避免了重複統計逆序對,我們把它累計起來:
0,(0,0,0,0,0,0,0,0,0),sum+=0;

我們將所給序列的第一項的數字所對應位置置1:
0,1,0,0,0,0,0,0,0,0

第二項9:
0,1,0,0,0,0,0,0,0,(0),sum+=0;

0,1,0,0,0,0,0,0,0,1

第三項2:
0,1,(0,0,0,0,0,0,0,1),sum+=1;
(2與9逆序)
0,1,1,0,0,0,0,0,0,1

第四項3:
0,1,1,(0,0,0,0,0,0,1),sum+=1;
(3與9逆序)
0,1,1,1,0,0,0,0,0,1

第五項0:
(0,1,1,1,0,0,0,0,0,1),sum+=4;
(0與1,2,3,9逆序)
1,1,1,1,0,0,0,0,0,1

以此類推到最後,sum便是該序列逆序對個數。

現在考慮迴圈同構序列的最小值:

剛才的序列:
1,9,2,3,0,8,5,7,4,6

我們把x0 = 1移到最後:
9,2,3,0,8,5,7,4,6,1

發現逆序對增加了8對(8 = 10 - x0 - 1),減少了1對(1 = x0),可以推出結論,向後移動一個數字,逆序對增加n - xi - 1,減少xi。掃一遍維護最小值即可


附上 我的蒟蒻 程式碼:

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
#define  maxn 100000
int tre[maxn*10];
int a[maxn];
void build(int in,int l,int r){      //這裡白寫了
   if(l==r){
      tre[in]=0;
      return ;
   }
   int mid=(l+r)/2;
   build(in*2,l,mid);
   build(in*2+1,mid+1,r);

}
int query(int in,int l,int r,int x,int y){
   if(l==x&&y==r){
      return tre[in];
   }
   int mid=(x+y)/2;
   if(l>mid){
      return query(in*2+1,l,r,mid+1,y);
   }
   if(r<=mid){
      return query(in*2,l,r,x,mid);
   }
    return query(in*2+1,mid+1,r,mid+1,y)+query(in*2,l,mid,x,mid);
}
void updata(int in,int l,int r,int rt){
    if(l==r){
        tre[rt]=1;                 //一定要等於1  不然會錯
        return ;
    }
    int mid=(l+r)/2;
    if(in<=mid){
        updata(in,l,mid,rt*2);
    }
    else updata(in,mid+1,r,rt*2+1);

    tre[rt]=tre[rt*2]+tre[rt*2+1];
}
int main(){
    int n;
    while(cin>>n&&n){
        memset(tre,0,sizeof(tre));

        int sum=0;
        for(int j=1;j<=n;j++){
            scanf("%d",a+j);
            sum+=query(1,a[j]+1,n,1,n);    //注意 如果 a[j]==0 的情況 所以要 +1 可以用紙寫一下
            //cout<<sum<<endl;
            updata(a[j]+1,1,n,1);
        }
        int k=sum;
        for(int j=1;j<=n;j++){
            k+=(n-2*a[j]-1);
            sum=min(sum,k);
        }
        //if(sum<0) sum=0;
        cout<<sum<<endl;
    }

    return 0;
}




相關文章