hdu1394 Minimum Inversion Number(最小逆序数)

网友投稿 244 2022-09-06

hdu1394 Minimum Inversion Number(最小逆序数)

Minimum Inversion Number

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)

Total Submission(s) : 1   Accepted Submission(s) : 1

Times New Roman | Verdana | Georgia

← →

Problem Description

The inversion number of a given number sequence a1, a2, ..., an 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 will obtain another sequence. There are 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 are 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 two 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 on a single line.

Sample Input

10 1 3 6 9 0 8 5 7 4 2

Sample Output

16

Author

CHEN, Gaoli

Source

ZOJ Monthly, January 2003

​​Statistic​​​ |  ​​​Submit​​​ |  ​​​Back​​

先百度逆序数:在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。逆序数为​​偶数​​的排列称为偶排列;逆序数为奇数的排列称为奇排列。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。

对于这个题求把第一个数放到最后一个数的最小逆序数,对于原序列而言,如果把第一个数放到最后一个数,逆序列增加n-num[0]+1,逆序列减少

num[0].

所以这道题:

#include int main(){ int num[5005],n; while(scanf("%d",&n)!=EOF) { for(int i=0;inum[j]) sum++; temp=sum; for(int i=n-1;i>=0;i--) { temp-=n-1-num[i]; temp+=num[i]; if(temp

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:hdu2033人见人爱A+B
下一篇:这两人有望在东京奥运会开幕式代表运动员宣誓!
相关文章

 发表评论

暂时没有评论,来抢沙发吧~