hdu 1394 Minimum Inversion Number 【線段樹查詢】
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.
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;
}
相關文章
- HDU 1394 Minimum Inversion Number (樹狀陣列求逆序數)陣列
- 線段樹 transformation——hdu 4578ORM
- HDU1754 I Hate It 【線段樹基礎:點修改+區間查詢】
- hdu 1754 I Hate It (線段樹)
- HDU1698 Just a Hook【線段樹基礎:區間修改+區間查詢】Hook
- Transformation HDU - 4578線段樹綜合操作ORM
- hdu 1754 【線段樹/RMQ】I Hate ItMQ
- hdu 3973 字串hash+線段樹字串
- hdu4288 離線處理線段樹
- hdu 4836 The Query on the Tree(線段樹or樹狀陣列)陣列
- HDU1166 敵兵佈陣【線段樹基礎:點修改+區間查詢】
- HDU 1754 I Hate It 線段樹入門
- HDU 1556 Color the ball(線段樹|樹狀陣列)陣列
- 線段樹(1)建樹、單點修改、單點查詢、區間查詢和例題
- [ABC337G] Tree Inversion(換根 dp + 值域線段樹)
- 芻議線段樹 2 (區間修改,區間查詢)
- HDU 1754 I Hate It (線段樹 區間最值)
- HDU 1556 Color the ball 線段樹入門題
- HDU 1698 Just a Hook (線段樹區間更新)Hook
- hdu 2665 可持久化線段樹求區間第K大值(函式式線段樹||主席樹)持久化函式
- HDU 3333 Turing Tree(線段樹+離線操作)
- HDU 3074 Multiply game(線段樹 單點更新)GAM
- (hdu 1754) I Hate It(線段樹基礎,單點更新)
- POJ 3468 【區間修改+區間查詢 樹狀陣列 | 線段樹 | 分塊】陣列
- POJ 2777-Count Color(線段樹-區間染色查詢)
- 分組查詢連線號段
- 線~段~樹
- 線段樹
- 1080 線段樹練習 單點修改及區間查詢
- POJ 3264 Balanced Lineup 線段樹入門(點的查詢)
- 1081 線段樹練習 2 單點查詢及區間修改
- 1082 線段樹練習 3 區間查詢與區間修改
- HDU 4417-Super Mario(劃分樹-二分查詢)
- 線段樹 hate it
- 【模版】線段樹
- 01 線段樹
- 線段樹--RMQMQ
- 李超線段樹