/*
http://acm.hdu.edu.cn/showproblem.php?pid=1394
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7678 Accepted Submission(s): 4708
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.
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
ZOJ Monthly, January 2003
解析:
题意:
给出由0到n-1个数组成的数组,按照题目的轮换要求,求最少的逆序对
思路:
1.首先要求出初始串有多少个逆序对,这里有两种方法:
一:暴力求:由于数据不大此法可行
二;线段数,按照数组的顺序,进行逐次查询和更新,注意:无论是查询还是更新的区间都是为数而不是序号
2.利用规律,更新最小值。
这里解释一下sum=sum+n-a[i]-a[i]-1;
由于数组的数值只有0到n-1且不重复。则对于a[i]来说一定有a[i]个数比它小,n-a[i]-1个数比它大;
假设a[i]的前一状态是在数组首部而此时被放到数组尾部时,逆序对必然减少a[i];增加n-a[i]-1.
http://acm.hdu.edu.cn/showproblem.php?pid=1394
Minimum Inversion Number
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7678 Accepted Submission(s): 4708
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.
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
ZOJ Monthly, January 2003
解析:
题意:
给出由0到n-1个数组成的数组,按照题目的轮换要求,求最少的逆序对
思路:
1.首先要求出初始串有多少个逆序对,这里有两种方法:
一:暴力求:由于数据不大此法可行
二;线段数,按照数组的顺序,进行逐次查询和更新,注意:无论是查询还是更新的区间都是为数而不是序号
2.利用规律,更新最小值。
这里解释一下sum=sum+n-a[i]-a[i]-1;
由于数组的数值只有0到n-1且不重复。则对于a[i]来说一定有a[i]个数比它小,n-a[i]-1个数比它大;
假设a[i]的前一状态是在数组首部而此时被放到数组尾部时,逆序对必然减少a[i];增加n-a[i]-1.
/ 方法一暴力法:250MS 260K 482 B C++
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=5000+10;
int a[maxn];
int main()
{int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
int s=0,ans;
for(i=1;i<=n;i++)
{ for(j=1;j<i;j++)
{
if(a[j]>a[i])
s++;
}
}
ans=s;
for(i=1;i<=n-1;i++)
{
s=s+n-1-2*a[i];
if(s<ans)
ans=s;
}
printf("%d\n",ans);
}
return 0;
}
// 方法二线段树:46MS 312K 1467 B
#include<stdio.h>
#include<string.h>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=5000+10;
int a[maxn];
struct tree
{
int l;
int r;
int num;
}tr[4*maxn];
void build(int k,int l,int r)
{
tr[k].l=l;
tr[k].r=r;
tr[k].num=0;
if(l==r)
{
return;
}
int mid=(tr[k].l+tr[k].r)/2;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
//tr[k].num=tr[k<<1].num+tr[k<<1|1].num;
}
void update(int k,int l,int r)
{
if(l==tr[k].l&&r==tr[k].r)
{
tr[k].num++;
return;
}
int mid=(tr[k].l+tr[k].r)/2;
if(r<=mid)
update(k<<1,l,r);
else if(l>mid)
update(k<<1|1,l,r);
else
{
update(k<<1,l,mid);
update(k<<1|1,mid+1,r);
}
tr[k].num=tr[k<<1].num+tr[k<<1|1].num;
}
int query(int k,int l,int r)
{
if(l==tr[k].l&&r==tr[k].r)
{
return tr[k].num;
}
int mid=(tr[k].l+tr[k].r)/2;
if(r<=mid)
return query(k<<1,l,r);
else if(l>mid)
return query(k<<1|1,l,r);
else
{
return query(k<<1,l,mid)+query(k<<1|1,mid+1,r);
}
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF)
{
int ans,sum;
sum=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
build(0,0,n);
for(i=1;i<=n;i++)
{
if(a[i]<n)
sum+=query(1,a[i]+1,n);//统计将比a[i]大的个数
update(1,a[i],a[i]);//把a[i]插入树中并更新,这里是按照数列的输入顺序进行更新的;
}
ans=sum;
for(i=1;i<=n-1;i++)
{
sum=sum+n+1-2*a[i];//因为前
if(sum<ans)
ans=sum;
}
printf("%d\n",ans);
}
return 0;
}