Minimum Inversion Number

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

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

先百度逆序数:在一个排列中。假设一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数。逆序数为偶数的排列称为偶排列;逆序数为奇数的排列称为奇排列。

如2431中,21。43,41,31是逆序,逆序数是4,为偶排列。

对于这个题求把第一个数放到最后一个数的最小逆序数,对于原序列而言,假设把第一个数放到最后一个数,逆序列添加n-num[0]+1,逆序列降低

num[0].

所以这道题:

#include <stdio.h>
int main()
{
int num[5005],n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
int sum=0,temp;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(num[i]>num[j]) sum++;
temp=sum;
for(int i=n-1;i>=0;i--)
{
temp-=n-1-num[i];
temp+=num[i];
if(temp<sum)
sum=temp;
}
printf("%d\n",sum);
}
return 0;
}

最新文章

  1. C++随笔:.NET CoreCLR之corleCLR核心探索之coreconsole(2)
  2. shell 中的引用
  3. 浅谈iOS网络编程之一入门
  4. C# XML - XmlDocument
  5. CentOS 安装中文输入法
  6. Jquery实现图片轮播源码
  7. 工作总结:WPF技术讲解(大纲)
  8. printf交替使用
  9. 一句话理解字符编码(Unicode ,UTF8,UTF16)
  10. ES6(正则扩展)
  11. Java Web 高性能开发,第 1 部分: 前端的高性能
  12. bootstrap之弹出框
  13. 【LeetCode】335. Self Crossing(python)
  14. kubernets网络模式
  15. Page13:跟踪问题、最优控制[Linear System Theory]
  16. python中获取执行脚本路径方法
  17. 腾讯云ubuntu搭建tomcat
  18. 【转】maven profile实现多环境打包
  19. 初识Qt简单动画
  20. ajax 和xmlHttpRequest区别

热门文章

  1. Ambari API 验证方式
  2. Log4j官方文档翻译(三、配置)
  3. Pointcut is not well-formed: expecting &#39;name pattern&#39; at character position 53
  4. 荔枝丹(litchi)
  5. java写文件的基本操作
  6. 【转】Java进阶之路
  7. 【POJ3667】Hotel(线段树)
  8. a kind of async programming in c#, need to reference definition
  9. poj 3261 Milk Patterns 后缀数组 + 二分
  10. VS Code开发技巧集锦【转】