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.
InputThe 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. 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.
OutputFor each case, output the minimum inversion number on a single line.
Sample Input
10 1 3 6 9 0 8 5 7 4 2Sample Output
16
初学线段树 , 完全没看出来这题能和线段树扯上关系
只要利用线段树将最初逆序数算出来 , 接下来的用公式推就好
应该是1~n的任意一个全排列吧
啊哈! copy 到一个解释这个规律的, 我再补充说明一下:
每一个数往后面移,后面比它小的有 num[i]个,比它大的有 n-num[i]-1 个数
所以。当求出第一次的逆序数之后,值为sum,向后移的过程中,每次的逆序数为 sum=sum+(n-num[i]-1)-num[i]
昂, 一开始想的是直接加比他大的数就好啦 , 为什么还要减比他小的呢 , 嗯,因为加上上一个的值啦 ,所以要减去比他小的 。 ----------------菜到想吃小饼干~~~~
例如:0 3 4 1 2 ----------4
3 4 1 2 0 ----------8(4 + n - x[0] - x[0]-1 = 4 + 5 - 0 - 0 - 1 = 8)以下同理
4 1 2 0 3-----------6
1 2 0 3 4 ---------2
2 0 3 4 1---------4
{
样例再解释 , 防止以后不记得;
0 3 4 1 2
0 0 0 2 2 --------4个
(1).0 ~ 4--------0个
(2)3~ 4-------0个
(3)4 ------0个
(4) 1 ~ 4 ----有3 和4 啦-------2个
(5) 2 ~ 4 ----有3 和 4 啦----2个
}建树的时候不用输入数字,确定的到n
void Build(int l , int r , int rt)
{
Sum[rt] = 0;
if(l == r)
{
return ;
}
int m = (l + r) >> 1;
Build(lson);
Build(rson);
}
单点增减和区间求和这边 void Update(int p , int l , int r , int rt)
{
if(l == r)
{
Sum[rt]++;
return;
}
int m = (l + r) >> 1;
if(p <= m) Update(p , lson);
else Update(p , rson);
PushUp(rt);
}
int query(int L , int R , int l , int r , int rt)
{
if(L <= l && R >= r)
{
return Sum[rt];
}
int m = (l + r) >> 1;
int ret = 0;
if(L <= m) ret += query(L , R , lson);
if(R > m) ret += query(L , R , rson);
return ret;
}
下面代码:
#include <stdio.h>
#include <algorithm>
using namespace std;
#define lson l , m , rt << 1
#define rson m+1 , r , rt << 1|1
const int maxn = 55555;
int Sum[maxn << 2];
void PushUp(int rt)
{
Sum[rt] = Sum[rt << 1] + Sum[rt << 1|1];
}
void Build(int l , int r , int rt)
{
Sum[rt] = 0;
if(l == r)
{
return ;
}
int m = (l + r) >> 1;
Build(lson);
Build(rson);
}
//单点增减
void Update(int p , int l , int r , int rt)
{
if(l == r)
{
Sum[rt]++;
return;
}
int m = (l + r) >> 1;
if(p <= m) Update(p , lson);
else Update(p , rson);
PushUp(rt);
}
int query(int L , int R , int l , int r , int rt)
{
if(L <= l && R >= r)
{
return Sum[rt];
}
int m = (l + r) >> 1;
int ret = 0;
if(L <= m) ret += query(L , R , lson);
if(R > m) ret += query(L , R , rson);
return ret;
}
int x[maxn];
int main()
{
int n,sum;
while(~scanf("%d" , &n))
{
sum = 0;
Build(0 , n-1 , 1);
for(int i = 0 ; i < n ; i++)
{
scanf("%d" , &x[i]);
sum += query(x[i] , n-1 , 0 , n-1 , 1);
Update(x[i] , 0 , n-1 , 1);
}
int ret = sum;
for(int i = 0 ; i < n ; i++)
{
sum += n - x[i] - x[i] -1;
ret = min(ret , sum);
}
printf("%d\n" , ret);
}
return 0;
}