ACM Minimum Inversion Number Problem solving report-segment tree

Source: Internet
Author: User

C-minimum Inversion numberTime limit:1000MS Memory Limit:32768KB 64bit IO Format:%i64d &%i64 U

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
1 // Segment Tree topic solving.  2// In fact, initially did not understand, and then manually simulated the whole process of building the tree is clear.  3//
1#include"iostream"2#include"algorithm"3#include"Cstdio"4#include"CString"5#include"Cmath"6 #defineMax (A, b) a>b?a:b7 #defineMin (A, b) a<b?a:b8 #defineLson l,m,rt<<19 #defineRson m+1,r,rt<<1|1Ten using namespacestd; One Const intMX =200000+Ten; A intsum[mx<<2]; - voidPushup (intRT) { -sum[rt]=sum[rt<<1]+sum[rt<<1|1];//Update node parent node is the sum of child nodes the } -  - voidBuild (intLintRintRT) { -sum[rt]=0;//Build an empty tree "before this place in the judgment, the leaf node did clear, the side points are missing." +     if(r==l)return ; -     intM= (r+l) >>1; +Build (Lson);//set up left node ABuild (Rson);//Build Right Node at } -  - voidUpdata (intPintLintRintRT) { -     if(r==l) {//find and update target points -sum[rt]++; -         return ; in     } -     intM= (r+l) >>1; to     if(p<=m) Updata (P,lson);//if it's not the target, look around. +     if(P >m) updata (P,rson); -Pushup (RT);//Updates the child nodes for each point that will be updated.  the } *  $ intQuery (intLintRintLintRintRT) {Panax Notoginseng     if(L&LT;=L&AMP;&AMP;R&GT;=R)//size exceeds entire range -         returnSUM[RT];//Total returned the     intM= (r+l) >>1; +     intret=0; A     if(l<= m) ret + = Query (L,r,lson);//the left value of a tree larger than x[i] and the     if(R > m) ret + = Query (L,r,rson);//The right value of a tree larger than x[i] and +     returnret; - } $ intX[MX]; $ intMain () { -     intN; -     intsums; the     Chars[2]; -      while(~SCANF ("%d",&N)) {Wuyisums=0;  theBuild (0, N-1,1);//"It should be better from the 0~n-1, where the location of the 0 from 1~n is not good." The back is just the same. " -          for(intI=0; i<n; i++) { Wuscanf"%d",&x[i]); -Sums+=query (x[i],n-1,0, N-1,1); AboutUpdata (X[i],0, N-1,1); $         } -         intret=sums; -          for(intI=0; i<n; i++) { -sums=sums+n-2*x[i]-1; Aret=min (ret,sums); +         } theprintf"%d\n", ret); -     } $     return 0; the}
View Code

ACM Minimum Inversion Number Problem solving report-segment tree

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.