HDU 1394 minimum inversion number (line segment tree, number of reverse orders)

Source: Internet
Author: User

Link:

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1394


Question:

For a sequence of N numbers A1, A2,..., An, the range of these numbers is 0 ~ N-1, you can move the number of m above to the back to form a new sequence:

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)

What is the minimum number of reverse sequences?


Analysis and Summary:

(1) When I first read this question, I felt familiar with it, so I read the answer record. I did = last year, but at that time I directly tried to find the reverse order of the data...

Then we want to use the line segment tree for optimization. The so-called reverse order number is actually the sum of each number in the sequence, which is greater than the number before it.

Use the line segment tree to record the numbers. The value of the interval [a, B] indicates the number ~ The number of times that B has appeared, so for AI, you only need to query the number of [ai, N] (the number of AI that was earlier than AI ), it indicates the number of reverse orders of AI.


(2) After finding the reverse numbers of A1, A2,..., An-1, and an, You Can recursively obtain the reverse numbers of other sequences. Suppose we want to move A1 to an, then we split the process into two steps:

1. Remove A1. Through observation we can find that (a1-1) is 0 ~ The number of numbers smaller than A1 in n-1, because A1 is smaller than A1 in the first sequence, so there are (a1-1) after A1 than A1, so the formation of (a1-1) on the reverse order number, when A1 is removed, the total number of reverse orders of the original sequence is reduced (a1-1.

2. Add A1 to. 0 ~ N-1, a number larger than A1 total (n-a1) number, because A1 is now in the last, that is, before it total (n-a1) number is greater than it, that is, increased (n-a1) number of reverse orders.

Combining the two steps of 1, 2, set the original sequence Inverse Order Number to sum, when the original sequence first to move to the last position, the reverse order number to: Sum = sum-(ai-1) + (n-ai );



Code:

#include<iostream>#include<cstdio>#include<cstring>#define lson(x) (x<<1)#define rson(x) (lson(x)|1)using namespace std;const int MAX_NODE = 5005 << 2;int arr[MAX_NODE];struct node{    int left, right;    int num;    int mid(){return (left+right)>>1;}    bool buttom(){return left==right;}};class SegTree{public:    void build(int cur,int left,int right){        t[cur].left  = left;        t[cur].right = right;        if(left == right){            t[cur].num = 0;            return;        }        int m = t[cur].mid();        build(lson(cur),left,m);        build(rson(cur),m+1,right);        push_up(cur);    }    void update(int cur,int data){        ++t[cur].num;        if(t[cur].buttom()){            return;        }        int m = t[cur].mid();        if(data <= m)            update(lson(cur),data);        else            update(rson(cur),data);    }    int query(int cur,int left,int right){        if(t[cur].left==left && t[cur].right==right){            return t[cur].num;        }        int m=t[cur].mid();        if(right <= m)            return query(lson(cur),left,right);        else if(left > m)            return query(rson(cur),left,right);        else             return  query(lson(cur),left,m)+query(rson(cur),m+1,right);    }private:    void push_up(int cur){        t[cur].num = t[lson(cur)].num+t[rson(cur)].num;    }    node t[MAX_NODE];};SegTree st;int main(){    int n,x;    while(~scanf("%d",&n)){        st.build(1,1,n);        int sum=0;        for(int i=0; i<n; ++i){            scanf("%d",&arr[i]);            ++arr[i];            int tmp = st.query(1,arr[i],n);            sum += tmp;            st.update(1,arr[i]);        }        int _min = sum;        for(int i=0; i<n-1; ++i){            sum = sum-(arr[i]-1)+(n-arr[i]);            if(sum < _min) _min=sum;        }        printf("%d\n",_min);    }    return 0;}


 -- The significance of life is to give it a meaningful person.

Original Http://blog.csdn.net/shuangde800,
D_double (reprinted please mark)

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.