HDU 1394 tree-like array for reverse order

Source: Internet
Author: User

Minimum Inversion number

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


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

Authorchen, Gaoli

Sourcezoj Monthly, January 2003 topic meaning: Give an n, then an array of length n, each element is less than n and not duplicated, each operation puts the first element at the end, and the smallest number of reverse order in n operations. Idea: First to find the original array of reverse order number, each time the first place at the end, NUM (reverse order number) plus greater than a[i], and then subtract less than a[i]. Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4#include <iostream>5#include <vector>6#include <queue>7#include <cmath>8#include <Set>9 using namespacestd;Ten  One #defineN 5005 A #definell Root<<1 - #defineRR root<<1|1 - #defineMid (A[ROOT].L+A[ROOT].R)/2 the  -  - intMaxintXintY) {returnX>y?x:y;} - intMinintXintY) {returnX<y?x:y;} + intAbsintXintY) {returnx<0?-x:x;} -  + intN; A intB[n]; at intA[n]; -  - intLowbit (intx) { -     returnx& (-x); - } -  in voidSolveintValintx) { -      while(x<=N) { toa[x]+=Val; +x+=lowbit (x); -     } the } *  $ intSumintx) {Panax Notoginseng     intans=0; -      while(x>0){ theans+=A[x]; +x-=lowbit (x); A     } the     returnans; + } -  $ Main () $ { -     intT, I, J, K; -      while(SCANF ("%d", &n) = =1){ theMemset (A,0,sizeof(a)); -         intans=0;Wuyi          for(i=1; i<=n;i++) { thescanf"%d",&b[i]); -b[i]++; WuAns+=sum (N)-sum (b[i]); -Solve1, B[i]); About         } $         intnum=ans; -          for(i=1; i<=n;i++){ -num+=n-b[i]-b[i]+1;//sum (n)-sum (B[i])-sum (b[i]-1); -ans=min (ans,num); A         } +printf"%d\n", ans); the     } -}

HDU 1394 tree-like array for reverse order

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.