HDU 1394 Minimum Inversion Number(线段树 || 树状数组+逆序数 )

来源:互联网 发布:淘宝单刷 编辑:程序博客网 时间:2024/06/05 17:57

Minimum Inversion Number


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
101 3 6 9 0 8 5 7 4 2
 

Sample Output
16

(网上找到的分析,理解了这题就容易了)

先百科一下逆序数的概念: 在一个排列中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序。一个排列中逆序的总数就称为这个排列的逆序数 。逆序数为偶数的排列称为偶排列 ;逆序数为奇数的排列称为奇排列。如2431中,21,43,41,31是逆序,逆序数是4,为偶排列。

逆序数计算方法是:在逐个元素读取原始数列时,每次读取都要查询当前已读取元素中大于当前元素的个数,加到sum里,最后得到的sum即为原始数列的逆序数。

接下来找数列平移后得到的最小逆序数,假设当前序列逆序数是sum,那么将a[0]移到尾部后逆序数的改变是之前比a[0]大的数全部与尾部a[0]组合成逆序数,假设数量为x,则x=n-1-a[0],而之前比a[0]小的数(也就是之前能和a[0]组合为逆序数的元素)不再与a[0]组合成逆序数,假设数量为y,则y=n-x-1,这样,新序列的逆序数就是sum+x-y=sum-2*a[0]+n-1;

接下来说明下线段树的作用,线段区间表示当前已读取的元素个数,比如[m,n]表示在数字m到n之间有多少个数已经读入,build时所有树节点全部为0就是因为尚未读数,update函数是将新读入的数字更新到线段树里,点更新,query函数是查询当前数字区间已存在的数字个数。


AC代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=5000+10;int sum[maxn<<2];  //区间[i,j]逆序数的总个数 int a[maxn];  //存储原序列 int n;void build(int l,int r,int rt){  //建树 sum[rt]=0;if(l==r)return ;int m=(l+r)>>1;    build(l,m,rt<<1);    build(m+1,r,rt<<1|1);}int Query(int i,int j,int l,int r,int rt){  //查询 if(i<=l && j>=r){return sum[rt];}int m=(l+r)>>1;int ans=0;if(i<=m) ans+=Query(i,j,l,m,rt<<1);if(j>m) ans+=Query(i,j,m+1,r,rt<<1|1); return ans;}void Update(int i,int j,int l,int r,int rt){  //更新     if(l==r){ sum[rt]++; return ;}int m=(l+r)>>1;if(i<=m) Update(i,j,l,m,rt<<1);else  Update(i,j,m+1,r,rt<<1|1);sum[rt]=sum[rt<<1]+sum[rt<<1|1];}int main(){while(scanf("%d",&n)==1){build(0,n-1,1);int tol=0;for(int i=0;i<n;i++){scanf("%d",&a[i]);tol+=Query(a[i],n-1,0,n-1,1);Update(a[i],n-1,0,n-1,1);}int cnt=tol;for(int i=0;i<n;i++){cnt=cnt+n-a[i]*2-1;tol=min(tol,cnt);}printf("%d\n",tol);}return 0;}



解法二:树状数组

首先输入数据存入数组a[i]当中,然后逆着从n-1往前计算(这样找的就是比a[i]小的个数,也就是每个a[i]对应的逆序数)。

AC代码:

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=5000+10;int C[maxn]; int a[maxn]; int n;int lowbit(int i){return i&-i;}void add(int i){while(i<=n){C[i]++;i+=lowbit(i);}}int sum(int i){int ans=0;while(i){ans+=C[i];i-=lowbit(i);}return ans;}int main(){while(scanf("%d",&n)==1){memset(C,0,sizeof(C));for(int i=0;i<n;i++){scanf("%d",&a[i]);}int tol=0;for(int i=n-1;i>=0;i--){add(a[i]+1);  //因为a[i]可以为0,而树状数组下标从1开始 tol+=sum(a[i]);}int cnt=tol;for(int i=0;i<n;i++){cnt=cnt+n-a[i]*2-1;tol=min(tol,cnt);}printf("%d\n",tol);}return 0;}



1 0