Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12991 Accepted Submission(s): 7939
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
题意:已知一个数列,数列个数n<=5000,数列中的数的范围在[0,n-1],并且数不重复。每次可以将数列的第一个数移动到
最后面,这样可以构造出n个数列。求这n个数列中的最小逆序数。
(求一个数列的逆序对方法就不说了,想了解的话看我上一篇博客)
思路:我们求出第一个数列的逆序数后,没有必要再去进行同样操作去求其他数列的逆序对(再说时间不允许)。其实只要计算出第一个数列的逆序数,其他数列的逆序数可以用这个已经计算好的逆序数推出。具体的做法是:当一个数a移动到最后面时,那么这个数后面会有n-a-1个数比它大,a个比它小,移动后,这个数的逆序数会增加n-a-1,其它数的逆序数会减小a,所以对当前序列来说逆序数会增加(n - 1 - a - a)。因此只需要n次遍历便可求出其他数列的逆序对数目,然后每次更新即可。
我是先用树状数组写的,上面有注释,后面三个用线段树和归并排序写的。
树状数组:62ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
#define MAX 5000
using namespace std;
int c[MAX<<2];
int n;
struct record
{
int val, pos;
}num[MAX];
bool cmp1(record a,record b)
{
return a.val > b.val;
}
bool cmp2(record a,record b)
{
return a.pos < b.pos;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x <= n)
{
c[x] += 1;
x += lowbit(x);
}
}
int sum(int x)
{
int s=0;
while(x > 0)
{
s += c[x];
x -= lowbit(x);
}
return s;
}
int main()
{
int i, j;
LL ans;//最小的逆序对数
LL res;//中间变量 更新
while(~scanf("%d", &n))
{
memset(c, 0, sizeof(c));
for(i = 0; i < n; i++)
{
scanf("%d", &num[i].val);
num[i].pos = i + 1;
}
sort(num, num+n, cmp1);
ans = 0;
for(i = 0; i < n; i++)
{
update(num[i].pos);
if(num[i].pos == 1)
continue;
ans += sum(num[i].pos - 1);
}
sort(num, num+n ,cmp2);//还原成原来的数列
res = ans;//没有移动数前具有的逆序对数目
for(i = 0; i < n; i++)
{
res += n - 1 - num[i].val -num[i].val;
ans = min(ans, res);//每次更新
}
printf("%lld\n", ans);
}
return 0;
}
线段树 lazy 单点更新:93ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
#define MAX 5000+10
using namespace std;
int sum[MAX<<2];
struct record
{
int val, pos;
}num[MAX];
bool cmp1(record a,record b)
{
return a.val > b.val;
}
bool cmp2(record a,record b)
{
return a.pos < b.pos;
}
void PushUp(int o)
{
sum[o] = sum[o<<1] + sum[o<<1|1];
}
void build(int o, int l, int r)
{
sum[o] = 0;
if(l == r)
return ;
int mid = (l+r) >> 1;
build(o<<1, l , mid);
build(o<<1|1, mid+1, r);
PushUp(o);
}
void update(int o, int l, int r, int L)
{
if(l == r)
{
sum[o] += 1;
return ;
}
int mid = (l+r) >> 1;
if(L <= mid) update(o<<1, l, mid, L);
else update(o<<1|1, mid+1, r,L);
PushUp(o);
}
int query(int o ,int l, int r, int L, int R)
{
if(L <= l && R >= r)
{
return sum[o];
}
int mid = (l+r) >> 1;
int res = 0;
if(L <= mid) res += query(o<<1, l ,mid, L, R);
if(R > mid) res += query(o<<1|1, mid+1, r, L, R);
return res;
}
int main()
{
int n;
int i;
LL ans, res;
while(~scanf("%d", &n))
{
build(1, 1, n);
for(i = 0; i < n; i++)
{
scanf("%d", &num[i].val);
num[i].pos = i + 1;
}
sort(num, num+n, cmp1);
ans = 0;
for(i = 0; i < n; i++)
{
update(1, 1, n, num[i].pos);
if(num[i].pos == 1)
continue;
ans += query(1, 1, n, 1, num[i].pos - 1);
}
res = ans;
sort(num, num+n, cmp2);
for(i = 0; i < n; i++)
{
res += n - 1 - num[i].val - num[i].val;
ans = min(ans, res);
}
printf("%lld\n", ans);
}
return 0;
}
线段树 (不用Lazy):62ms
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL long long
#define MAX 5000+10
using namespace std;
int sum[MAX<<2];
struct record
{
int val, pos;
}num[MAX];
bool cmp1(record a,record b)
{
return a.val > b.val;
}
bool cmp2(record a,record b)
{
return a.pos < b.pos;
}
void build(int o, int l, int r)
{
sum[o] = 0;
if(l == r)
return ;
int mid = (l+r) >> 1;
build(o<<1, l , mid);
build(o<<1|1, mid+1, r);
}
void update(int o, int l, int r, int L)
{
sum[o] += 1;
if(l == r)
return ;
int mid = (l+r) >> 1;
if(L <= mid) update(o<<1, l, mid, L);
else update(o<<1|1, mid+1, r, L);
}
int query(int o ,int l, int r, int L, int R)
{
if(L == l && R == r)
{
return sum[o];
}
int mid = (l+r) >> 1;
int res = 0;
if(R <= mid) return query(o<<1, l ,mid, L, R);
else if(L > mid) return query(o<<1|1, mid+1, r, L, R);
else return query(o<<1, l, mid, L, mid) + query(o<<1|1, mid+1, r, mid+1, R);
}
int main()
{
int n;
int i;
LL ans, res;
while(~scanf("%d", &n))
{
build(1, 1, n);
for(i = 0; i < n; i++)
{
scanf("%d", &num[i].val);
num[i].pos = i + 1;
}
sort(num, num+n, cmp1);
ans = 0;
for(i = 0; i < n; i++)
{
update(1, 1, n, num[i].pos);
if(num[i].pos == 1)
continue;
ans += query(1, 1, n, 1, num[i].pos - 1);
}
res = ans;
sort(num, num+n, cmp2);
for(i = 0; i < n; i++)
{
res += n - 1 - num[i].val - num[i].val;
ans = min(ans, res);
}
printf("%lld\n", ans);
}
return 0;
}
归并排序:46ms(没想到这么高效,要掌握好)
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 5000+10
#define LL long long
using namespace std;
int a[MAX], tmp[MAX];
int b[MAX];//存储原先数列
LL ans;
void Merge(int l, int m, int r)
{
int i = l;
int j = m + 1;
int k = l;
while(i <= m && j <= r)
{
if(a[i] > a[j])
{
tmp[k++] = a[j++];
ans += m - i + 1;
}
else
{
tmp[k++] = a[i++];
}
}
while(i <= m) tmp[k++] = a[i++];
while(j <= r) tmp[k++] = a[j++];
for(int i = l; i <= r; i++)
{
a[i] = tmp[i];
}
}
void Merge_sort(int l,int r)
{
if(l < r)
{
int m = (l + r) >> 1;
Merge_sort(l,m);
Merge_sort(m+1,r);
Merge(l,m,r);
}
}
int main()
{
int n;
int i, j;
LL res;
while(~scanf("%d", &n))
{
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
ans = 0;
Merge_sort(0,n-1);
res = ans;
for(i = 0; i < n; i++)
{
res += n - 1 - b[i] - b[i];
ans = min(ans, res);
}
printf("%lld\n", ans);
}
return 0;
}