hdu Minimum Inversion Number(線段樹求逆序數有關問題的一個小歸納)

來源:互聯網
上載者:User
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4904    Accepted Submission(s): 2991


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

首先,求逆序數對的思路: 1.得到整個數列後,從前往後掃,統計比a[i]小的,在a[i]後面的有多少個 這樣做的話,應該是只有n2的暴力作法,沒想到更好的方法 2.統計a[i]前面的,且比它大的數 這樣做的話,就可以利用輸入的時效性,每輸入一個數,就把這個數的num[i]值加1, 然後統計比這個數大的數的num和, 因為這裡的和一定是在這個數列中比a[i]大,且在它前面出現的數之和, 然後把把這個和加到總逆序數sum裡。 這樣做的話直接的暴力作法依然是n2,但是, 我們可以在, 統計 比這個數大的數的num和這一步進行最佳化,利用線段樹求區間域值的複雜度是logn, 所以總體複雜度就降到了nlogn。
再來看這道題,求得初始數列的逆序數後,再求其他排列的逆序數有一個規律,就是 sum = sum + (n - 1 - a[i]) - a[i]; 這個自行驗證吧,相信很容易得出
最後,拓展一下,如果要求正序數怎麼辦。很簡單,無非是大小調一下 再問,如果要求滿足i<j<k,且a[i]>a[j]>a[k]的數對總數怎麼辦。
可以從中間的這個數入手,統計a[i]>a[j]的對數m,以及a[j]>a[k]的對數n,m*n就是。。。 要求a[i]>a[j]的個數還是一樣的,那麼a[j]>a[k]的個數呢。 兩種思路: 1.得到a[i]>a[j]的對數後,將數列倒過來後再求a[j]<a[k]的對數 2.更簡單的做法是,找到規律發現,n = 整個數列中比a[j]小的數 — 在a[j]前面已經出現的比a[j]小的數的個數 即(假設數列是從1開始的) n = (a[j] -1) - (j - 1 - m )


這道題代碼
#include <stdio.h>#include <algorithm>using namespace std;int a[5005];struct Node{    int l,r,num;}tree[50000];void Build(int n,int x,int y){    tree[n].l = x;    tree[n].r = y;    tree[n].num = 0;    if(x == y){        return;    }    int mid = (x + y) / 2;    Build(2*n,x,mid);    Build(2*n+1,mid+1,y);}void Modify(int n,int x){    int l = tree[n].l;    int r = tree[n].r;    int mid = (l + r) / 2;    if(x == l && x == r){        tree[n].num = 1;        return;    }    if(x <= mid)    Modify(2*n,x);    else            Modify(2*n+1,x);    tree[n].num = tree[2*n].num + tree[2*n+1].num;}int Query(int n,int x,int y){    int l = tree[n].l;    int r = tree[n].r;    int mid = (l + r) / 2;    int ans = 0;;    if(x == l && y == r)        return tree[n].num;    if(x <= mid)   ans += Query(2*n,x,min(mid,y));    if(y > mid)    ans += Query(2*n+1,max(mid+1,x),y);    return ans;}int main(){    int n,sum,ans;    int i,j;    while(scanf("%d",&n) != EOF){        sum = 0;        Build(1,0,n);        for(i = 1;i <= n;i++){            scanf("%d",&a[i]);            Modify(1,a[i]);            sum += Query(1,a[i]+1,n);        }        ans = sum;        for(i = 1;i < n;i++){            sum = sum + (n - 1 - a[i]) - a[i];            if(sum < ans)                ans = sum;        }        printf("%d\n",ans);    }}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.