Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18926 Accepted Submission(s): 11427
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
Ignatius.L
这个题真的是不想说什么了, 第一次提交超时,当时就怀疑是不是自己算法出现了问题,结果想了大半天也没觉得自己算法问题出在哪里,后来看了网上的代码,换了种思路去写,结果在提交的时候发现自己主函数里面的,while循环输出值得时候,没有终止条件,后来把自己超时的代码,while循环里面加了个~,就过了,46ms,我。。。。。。这种错误真的是吐血啊
贴一下两种不同的求逆序数的代码吧
第一种,在树状数组里面开始每次放值最大的数,当然在输入的时候用了一了pos数组来记录一下该数在数组中的位置,然后用树状数组计算该数前面有多少已经被放上的数,被放到树状数组里面的数标记为1,然后放完所有的数的时候,记录的sum值就是逆序数的值
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 5005;
int n;
int a[maxn], c[maxn], pos[maxn];
int lowbit(int x) {
return x & -x;
}
int add(int k, int num) {
while(k <= n) {
c[k] += num;
k += lowbit(k);
}
}
int Sum(int k) { //求1 ~ k区间数的和
int sum = 0;
while(k > 0) {
sum += c[k];
k -= lowbit(k);
}
return sum;
}
int main()
{
while(~scanf("%d", &n)) {
int i;
memset(c, 0, sizeof(c));
for(i=1; i<=n; ++i) {
scanf("%d", &a[i]);
pos[a[i]] = i;
}
int ans = 0;
for(i=n-1; i>=0; --i) { //一个一个忘树状数组里面放数
add(pos[i], 1);
ans += Sum(pos[i] - 1);
}
int temp = ans;
for(i=1; i<=n; ++i) {
temp += n-1 - a[i] - a[i]; //规律
ans = min(temp, ans);
}
printf("%d\n", ans);
}
return 0;
}
第二种,借鉴的网上的思路
1、对于某一序列,其中的某一个数a[i]能构成多少个逆序,只须判断在a[i]+1~n的范围内找之前的数是否出现过的次数;
2、然后求出第一个序列的逆序数。
3、由第一个序列的逆序数可以推出它下一个序列的逆序数。 有规律 下一个序列的逆序数 sum = 上一个的sum - a[i] - 1 - a[i];如果得出呢 比如说:
2、然后求出第一个序列的逆序数。
3、由第一个序列的逆序数可以推出它下一个序列的逆序数。 有规律 下一个序列的逆序数 sum = 上一个的sum - a[i] - 1 - a[i];如果得出呢 比如说:
序列 3 6 9 0 8 5 7 4 2 1 把3移到后面,则它的逆序数会减少3个(0 2 1) 但同时会增加 n - a[i] - 1个。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn = 5005;
int n;
int a[maxn], c[maxn];
int lowbit(int x) {
return x & -x;
}
int add(int k, int num) {
while(k <= n) {
c[k] += num;
k += lowbit(k);
}
}
int Sum(int k) { //求1 ~ k区间数的和
int sum = 0;
while(k > 0) {
sum += c[k];
k -= lowbit(k);
}
return sum;
}
int main()
{
while(~scanf("%d", &n)) {
int i, ans = 0;
memset(c, 0, sizeof(c));
for(i=1; i<=n; ++i) {
scanf("%d", &a[i]);
add(a[i] + 1, 1);//这句话个下面这节话可以交换次序,并不会影响程序结果
ans += Sum(n) - Sum(a[i] + 1);
}
int temp = ans;
for(i=1; i<=n; ++i) {
temp += n-1 - a[i] - a[i];
ans = min(temp, ans);
}
printf("%d\n", ans);
}
return 0;
}