HDU 1394-minimum Inversion number

Source: Internet
Author: User

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 roughly test instructions: one by 0. N-1 sequence, each time you can move the first element of the team to the end of the team;
The number of the smallest inverse pairs in the n sequence formed; The problem-solving idea: Violence law: When the a[0] moves to the end of the team, it will reduce the a[0] reverse order will increase (n-1)-a[0] in reverse order; then only the initial reverse logarithm is required, the rest can be deduced;
1#include <cstdio>2 using namespacestd;3 intMain () {4     intn,a[ the+5],t;5      while(~SCANF ("%d",&N)) {6          for(intI=0; i<n;i++) scanf ("%d",&a[i]);7t=0;8          for(intI=0; i<n;i++)9              for(intj=i+1; j<n;j++)Ten                 if(A[i]>a[j]) t++; One         intmin=T; A          for(intI=0; i<n;i++){ -t=t-a[i]+ (n1)-A[i]; -             if(t<min) min=T; the         } -printf"%d\n", min); -     } -} 

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.