Minimum Inversion number

Source: Internet
Author: User

Description

The inversion number of a given number sequence A1, A2, ..., 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'll Obtain another sequence. There is 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 is 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 the 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 in a single line.

Sample Input

101 3 6 9 0 8 5 7 4 2

Sample Output

16 The main idea: to find the minimum number of reverse order, divided into two steps, the first step to find the original sequence of the reverse number, the second step loop n-1 times each state of the reverse number, and finally output the minimum value. With the brute force solution may time out, as if you can use line tree and reverse array, but now only a little bit of line tree, with a number of segments, in each value to store the left and right end of the value of the number of reverse order, with two query and update function, The query function is used to output the inverse number of the number from left to right, and the number of the reverse number exists in itself to the maximum, that is, A[n] to n-1 (because it is from 0 to n-1), if the n-1 is less than or equal to the mid, the description is the value of all of the values in his ieft son; , the description is all the values of the right son, otherwise there are all the sons, and the update function is to enter a number to update the node that has that number, because the number of builds built up from left to right is 1 to n-1, so with the mid to the son's judgment, update.
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAX =5555;structsegtree{intLeft ; intRight ; intVal;} Tt[max<<2];voidBuildintNintLeft,intRight ) {Tt[n].left=Left ; Tt[n].right=Right ; Tt[n].val=0; if(left = right)return ; intMid = (Tt[n].left + tt[n].right) >>1; Build (n<<1, left, mid); Build (n<<1|1, Mid +1, right);}intQueryintNintLeft,intRight ) {    if(Tt[n].left = = Left && Tt[n].right = =Right )returnTt[n].val;//root node   intMid = (Tt[n].left + tt[n].right) >>1; if(Right <=mid)returnQuery (n <<1, left, right); Else if(Left >mid)returnQuery (n <<1|1, left, right); Else returnQuery (n <<1, left, mid) + query (n <<1|1, Mid +1, right);}voidUpdateintNintidx) {Tt[idx].val++; if(Tt[idx].left = = tt[idx].right)return ; intMid = (Tt[idx].left + tt[idx].right) >>1; if(N <=mid) Update (n, IDX<<1); ElseUpdate (n, IDX <<1|1);}intMain () {intN,a[max], ans;  while(~SCANF ("%d",&N)) {Build (1,0N1 ); intsum =0;  for(inti =0; I < n; i++) {scanf ("%d", &A[i]); Sum+ = Query (1, A[i],n-1); Update (A[i],1); } ans=sum;  for(inti =0; I < n; i++) {sum= Sum-a[i] + (n-1-A[i]); Ans=min (ans, sum); } printf ("%d\n", ans); }    return 0;}
View Code

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.