HDU 1394 minimum inversion number

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394

 

Idea: first calculate the number of reverse orders, then there is a technique (true pitfall) ...... can be done using a line segment tree or a tree array. The shift technique is that the reverse order number is reduced ant [I], while the n-1-ant [I] is increased.

 

#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;}

 

There is an error... Wrong took us an hour .. That is, when the line segment tree is built, it must be open to N or it will overflow...

 

HDU 1394 minimum inversion number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.