Minimum Inversion NumberTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18700 Accepted Submission(s): 11322 |
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.
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
Recommend
提示
这题在我们OJ上有中文版的,但在之前在群里问了下没反应就跳过了,这次竟然考到了,一开始想用归并的,模板敲出来却一直过不了。。。
最后想了想,原来这么简单,算是没救了。
因为数字是按0~n-1给的,那么只需先暴力一次求逆序对,之后第一个数x移动到后面时,逆序对增加x个,减少n-1-x个,输出最少的那次就行了。
中文版:
Problem_8 小金防呀防不住_AK
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description
众所周知,C语言的学习是我们程序设计基础的重点和主要内容。
首先恭喜大家终于来到最后一道题目,那么我要告诉你这次的期末考试题目难度是从题目1到题目8依次递增的。
那么作为最后一道水题,它的存在意义当然是让15级的巨巨们好好动动脑子啦。
小金自从进了集训队,做了很多有关于逆序对的题目,他也很喜欢逆序对。介绍一下逆序对。逆序对就是在一个数组序列中的两个数x和y,但这两个数x和y可以算作一个逆序对的规则是x要大于y,但x的位置一定要在y的前面(即i < j && a[i] > a[j] )。
那么小金的问题来了,如果给定一个长度为n的序列,这n个数保证大于等于0且小于n,并且不会重复。那么小金给了你一个移动操作,你可以将这个序列的第一个数拿到序列的最后将序列变为一个新的序列,例如当n=10的时候,给定一个序列。
那么,所有可能通过移动得到的序列如下。
请你输出在所有可能出现的序列中逆序对的个数最小的是多少?
Input
多组输入。
第一行输入整数n,含义如题意描述。(0 < n < = 5000)
第二行有n个整数(0 < = x < = n-1)。
Output
输出最小逆序对的个数。输入输出各占一行,保证数据合法。
Example Input
10 1 3 6 9 0 8 5 7 4 2
Example Output
16
Hint
题目很简单,仔细读题喔
Author
2015级期末测试 Casithy
示例程序
Problem : 1394 ( Minimum Inversion Number ) Judge Status : Accepted
RunId : 18820611 Language : GCC Code Len : 646B
Code Render Status : Rendered By HDOJ GCC Code Render Version 0.01 Beta
#include <stdio.h>
int main()
{
int n,i,i1,min,a[5000],num;
while(scanf("%d",&n)!=EOF)
{
num=0;
for(i=0;n>i;i++)
{
scanf("%d",&a[i]);
}
for(i=0;n>i;i++)
{
for(i1=i+1;n>i1;i1++)
{
if(a[i]>a[i1])
{
num++;
}
}
}
min=num;
for(i=0;n>i;i++)
{
num=num-2*a[i]+n-1;
if(min>num)
{
min=num;
}
}
printf("%d\n",min);
}
return 0;
}