HDU 1394Minimum inversion number array in reverse order to number and

Source: Internet
Author: User

Minimum Inversion number

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 18543 Accepted Submission (s): 11246


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

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 in a single line.

Sample Input101 3 6 9 0 8 5 7 4 2

Sample Output16

Authorchen, Gaoli

Source ZOJ Monthly, January 2003

RECOMMENDIGNATIUS.L | We have carefully selected several similar problems for you:1166 1698 1540 1542 1255title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1394 Test Instructions: Find out the total number of pairs (Ai,aj) (I<j,ai>aj). That is, the number of the AI on the right is smaller than AI. Each time the sequence of a is transformed, the minimum number of inverse pairs is calculated. idea: Because the most basic function of a tree array is to find the number of points smaller than a point x . So reverse store AI. Code:
#include <iostream>#include<cstdio>#include<cstring>using namespacestd;Const intmaxn=1e6+ -, inf=1e9+ -;intA[MAXN],C[MAXN],ANS[MAXN];intLowbit (intx) {    returnx& (-x);}voidAddintIintval) {     for(i; i<=maxn; i+=lowbit (i)) C[i]+=Val;}intSuminti) {    ints=0;  for(i; i>0; i-=lowbit (i)) s+=C[i]; returns;}intMain () {intI,j,t,n;  while(SCANF ("%d", &n)! =EOF) {         for(I=n; i>=1; i--) scanf ("%d",&A[i]); Memset (c,0,sizeof(c)); memset (ans,0,sizeof(ans)); intcou=0;  for(i=1; i<=n; i++) {Ans[i]=sum (a[i]+1); Cou+=Ans[i]; Add (A[i]+1,1); }        intmin=cou;  for(I=n; i>1; i--)        {             for(j=1; j<=n; J + +)            {                if(j==i)Continue; if(a[i]<A[j]) {Cou++; ANS[J]++; }} cou-=Ans[i]; Ans[i]=0; if(cou<min) min=cou; } cout<<Min<<Endl; }    return 0;}
reverse order to

HDU 1394Minimum inversion number array in reverse order to number and

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.