Hdu1394 -- Minimum Inversion Number)

Source: Internet
Author: User

Hdu1394 -- Minimum Inversion Number)
Minimum Inversion NumberTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 10326 Accepted Submission (s): 6359


Problem DescriptionThe 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.

InputThe 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.

OutputFor each case, output the minimum inversion number on a single line.

Sample Input

101 3 6 9 0 8 5 7 4 2

Sample Output
16. Starting from the initial array, add the first one to the last one each time, calculate the number of reverse orders, and obtain n reverse orders in total. Find out the minimum value and calculate the reverse order number for each case, you can merge and sort each time, or a tree array or line segment tree, or roll out the previous number of reverse orders. a [1], a [2], a [3]... A [n], put a [1] to the end, and then put a [2] to the end. You can find the rule and put the first a [I] to the end, the number of reverse orders is greater than a [I] before a [I]. After a [I] is increased, it is larger than a [I, this reduces the size of a [I] before a [I], and the size of a [I] after a [I] is smaller than that of a [I, because the number n given each time ranges from 0 to n, different numbers are obtained. Finally, the number of reverse orders increases by n-a [I], reducing a [I]-1.
#include 
  
   #include 
   
    #define INF 0x3f3f3f3f#include using namespace std;int tree[100000] , p[6000] , q[6000];void update(int o,int x,int y,int u){    if( x == y && x == u )        tree[o]++ ;    else    {        int mid = (x + y)/ 2;        if( u <= mid )            update(o*2,x,mid,u);        else            update(o*2+1,mid+1,y,u);        tree[o] = tree[o*2] + tree[o*2+1];    }}int sum(int o,int x,int y,int i,int j){    int ans = 0 ;    if( i <= x && y <= j )        return tree[o] ;    else    {        int mid = (x + y) /2 ;        if( i <= mid )            ans += sum(o*2,x,mid,i,j);        if( mid+1 <= j )            ans += sum(o*2+1,mid+1,y,i,j);    }    return ans ;}int main(){    int i , j , n , min1 , num ;    while(scanf("%d", &n)!=EOF)    {        min1 = 0 ;        memset(q,0,sizeof(q));        for(i = 1 ; i <= n ; i++)        {            scanf("%d", &p[i]);            p[i]++ ;        }        memset(tree,0,sizeof(tree));        for(i = 1 ; i <= n ; i++)        {            min1 += sum(1,1,n,p[i],n);            update(1,1,n,p[i]);        }        num = min1 ;        for(i = 1 ; i < n ; i++)        {            num = num + ( n - p[i] ) - (p[i] - 1) ;            if( num < min1 )                min1 = num ;        }        printf("%d\n", min1);    }    return 0;}
   
  


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.