HDU 1394 線段樹or 樹狀數組~

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   ar   for   

                              Minimum Inversion Number  

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題目大意就是給你 N 個區間,裡麵包含有0~N-1的整數,允許你每次將序列的頭調到序列尾,讓你求出最小的逆序和,思路:先用線段樹or樹狀數組預先處理出原先的逆序和,然後再一個一個推出,每次操作後所產生的的新的逆序和,就拿範例來說,一開始的逆序和是22,如果將1調到末尾那裡的話,原先在1後面比1小的數有1個,這個數是0,而當1調到後面的時候,在1前面的比一大的數有8個,為3,6,9,8,5,7,4,2,那麼新序列的逆序和為22 - 1 + 8 = 29。以此類推 AC代碼:線段樹的方法,借鑒了notonlysuccess大神的姿勢:
Memory: 320 KB   Time: 78 MS
#include <cstdio>#include <iostream>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1const int maxn = 5555;int sum[maxn<<2];void PushUp(int rt){    sum[rt] = sum[rt<<1] + sum[rt<<1|1];}void build(int l,int r,int rt){    sum[rt] = 0;    if(l == r) return ;    int m = (l + r) >> 1;    build(lson);    build(rson);    return ;}void update(int p,int l,int r,int rt){    if(l == r){        sum[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 sum[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(){    int n;    while(~scanf("%d",&n)){        build(0,n - 1,1);        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 res = sum;        for(int i = 0;i < n;i++){            sum += n - x[i] - x[i] - 1;            res = min(res,sum);        }        printf("%d\n",res);    }    return 0;}
View Code

後來我有用樹狀數組做了一下,發覺樹狀數組一般都要比線段樹要快~

Memory: 332 KB   Time: 31 MS
#include <cstdio>#include <iostream>#include <cstring>using namespace std;const int maxn = 5555;int c[maxn];int a[maxn];int n;inline int lowbit(int x){    return x&(-x);}int sum(int x){    int ret = 0;    while(x > 0){        ret += c[x];x -= lowbit(x);    }    return ret;}void add(int x,int d){    while(x <= n){        c[x] += d;x += lowbit(x);    }}int main(){    while(~scanf("%d",&n)){        memset(c,0,sizeof(c));        memset(a,0,sizeof(a));        int ans = 0;        for(int i = 1;i <= n;i++){            scanf("%d",&a[i]);            ans += i - 1 - sum(a[i]);            add(a[i] + 1,1);        }        //cout<<ans<<endl;        int MIN = ans;        for(int i = 1;i <= n;i++){            MIN += n - (a[i] + 1) - (a[i]);            ans = min(MIN,ans);        }        printf("%d\n",ans);    }    return 0;}
View Code

 

 

 

HDU 1394 線段樹or 樹狀數組~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.