hdu 1394 Minimum Inversion Number

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   for   div   sp   

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=1394

 

思路:先求逆序數,然後有個技巧(真坑)......用線段樹或者樹狀數組都可以做。移位的技巧就是逆序數是減少ant[i],而增加n-1-ant[i]的.

 

#include <iostream>#include <cstring> #include <cstdio> #define MAXSIZE 500000using namespace std;struct node{    int l,r;    int sum;}tree[MAXSIZE*4];void build(int root,int l,int r){    tree[root].l = l;    tree[root].r = r;    if(l == r)    {        return;    }    int mid = (l+r)/2;        build(root*2,l,mid);    build(root*2+1,mid+1,r);}void update(int root,int pos,int value){    if(tree[root].r == tree[root].l&&tree[root].l == pos)    {        tree[root].sum += value;        return;    }        int mid = (tree[root].r+tree[root].l)/2;        if(pos>mid)    {        update(root*2+1,pos,value);    }    if(pos<=mid)    {        update(root*2,pos,value);        }    tree[root].sum = tree[root*2].sum + tree[root*2+1].sum;    }int query(int root,int l,int r){    if(tree[root].l==l&&tree[root].r==r)    {        return tree[root].sum;    }    int mid = (tree[root].r +tree[root].l)/2;        if(r<=mid)    {        query(root*2,l,r);    }    else if(l>mid)    {        query(root*2+1,l,r);    }    else    {        int a = query(root*2,l,mid);        int b = query(root*2+1,mid+1,r);        return a+b;    }    }int main(){//    freopen("data.txt","r",stdin);    int n;    int sum;    int res;    int ant[5500];    while(scanf("%d",&n)!=EOF)    {        memset(ant,0,sizeof(ant));        memset(tree,0,sizeof(tree));        build(1,0,n);        sum = 0;        for(int i=1;i<=n;i++)        {            scanf("%d",&ant[i]);            sum += query(1,ant[i]+1,n);            update(1,ant[i],1);            }                res = sum;        for(int i = 1;i<=n;i++)        {            sum = sum+n-2*ant[i]-1;            res = min(sum,res);        }        printf("%d\n",res);    }    return 0;}

 

還有個錯誤。。。wrong了我倆小時。。就是線段樹build的時候要開到n不然會溢出。。。

 

hdu 1394 Minimum Inversion Number

聯繫我們

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