Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17260 Accepted Submission(s): 10496
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
题意:给出一个数组,可以将数组的第一个数放到数组的最后一个数的后面,求经过此种操作后形成数组的最小逆序数。
思路:构建一棵线段树,大区间为[0,n-1],按照数组的顺序插入各个数,按照样例,第一个插入的为1,则将[1,1]置为1,然后向上跟新,然后查询[2,9]区间内已经插入的数的个数,(即比1大且在数组中顺序在1之前的数,1的逆序数),以此类推。
当得到一种数组的逆序数后,可以通过公式 ans = ans + (n - 1 - a[i]) + a[i]来求得最小逆序数。
代码如下:
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int a[5005];
struct node
{
int l,r,sum;
}t[20020];
int ss;
void build_tree(int l, int r, int k)
{
if(l == r)
{
t[k].l = l;
t[k].r = r;
t[k].sum = 0;
return;
}
int mid = (l + r) / 2;
t[k].l = l;
t[k].r = r;
t[k].sum = 0;
build_tree(l, mid, 2 * k);
build_tree(mid + 1, r, 2 * k + 1);
}
void insert_tree(int i, int num, int k)
{
if(t[k].l == t[k].r && t[k].l == i)
{
t[k].sum = num;
return ;
}
int mid = (t[k].l + t[k].r) / 2;
if(i <= mid)insert_tree(i, num, 2 * k);
else insert_tree(i, num, 2 * k + 1);
t[k].sum = t[2 * k].sum + t[2 * k + 1].sum;
}
void search_tree(int l, int r, int k)
{
if(t[k].l == l && t[k].r == r)
{
ss += t[k].sum;
return ;
}
int mid = (t[k].l + t[k].r) / 2;
if(r <= mid) search_tree(l, r, 2 * k);
else if(l > mid) search_tree(l, r, 2 * k + 1);
else
{
search_tree(l, mid, 2 * k);
search_tree(mid + 1, r, 2 * k + 1);
}
}
int main()
{
int n, i, j, ans;
while(scanf("%d", &n) != EOF)
{
ans = 0;
build_tree(0,n-1,1);
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
insert_tree(a[i], 1, 1);//每个数插入的值都为1
ss = 0;
if(a[i] != n - 1)//最大的数的逆序数必为0
search_tree(a[i] + 1, n - 1, 1);
ans += ss;//加上每个数的逆序数
}
int minn = ans;
for(i = 0; i < n; i++)
{
ans = ans + (n - 1 - a[i]) - a[i];
if(ans < minn)
minn = ans;
}
cout << minn <<endl;
}
return 0;
}