HDU 1394 Minimum Inversion number

Source: Internet
Author: User

Minimum Inversion number




Problem DescriptionThe Inversion number of a given number sequence A1, A2, ..., the number of pairs (AI, aj) that SA Tisfy 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.

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

Outputfor each case, output the minimum inversion number to a single line.

Sample Input101 3 6 9 0 8 5 7 4 2

Sample Output16
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 6 structSegtree7 {8     intL,r;9     intnum;Ten     intMid () One     { A         return(l+r) >>1; -     } - }; the  -Segtree tree[5005<<2]; - inta[5005<<2]; -  + voidPushup (intRT) - { +tree[rt].num=tree[rt<<1].num+tree[rt<<1|1].num; A } at  - voidBuildintLintRintRT) - { -Tree[rt].l=l; -Tree[rt].r=R; -tree[rt].num=0; in     if(L==R)return; -     intm=Tree[rt].mid (); toBuild (l,m,rt<<1); +Build (m+1,r,rt<<1|1); - } the  * intQueryintLintRintRT) $ {Panax Notoginseng     if(tree[rt].l==l&&tree[rt].r==R) -         returnTree[rt].num; the     intm=Tree[rt].mid (); +     if(r<=m) A         returnQuery (l,r,rt<<1); the     Else if(l>m) +         returnQuery (l,r,rt<<1|1); -     Else $         returnQuery (l,m,rt<<1) +query (m+1,r,rt<<1|1); $ } -  - voidUpdateintPosintRT) the { -     if(tree[rt].l==TREE[RT].R)Wuyi     { thetree[rt].num++; -         return; Wu     } -     intm=Tree[rt].mid (); About     if(pos<=m) Update (pos,rt<<1); $     ElseUpdate (pos,rt<<1|1); - pushup (RT); - } -  A intMain () + { the     intn,i,j; -      while(SCANF ("%d", &n)! =EOF) $     { the          for(i=0; i<n;i++) thescanf"%d",&a[i]); theBuild0, N-1,1); the         intsum=0; -          for(i=0; i<n;i++) in         { theSum+=query (a[i],n-1,1); theUpdate (A[i],1); About         } the         intans=999999999; theans=min (ans,sum); the          for(i=0; i<n;i++) +         { -sum=sum-a[i]+n-1-A[i]; theans=min (ans,sum);Bayi         } theprintf"%d\n", ans); the     } -     return 0; -}

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.