HDU

来源:互联网 发布:巴迪斯吊顶设计软件 编辑:程序博客网 时间:2024/06/08 18:21

Minimum Inversion Number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 21182    Accepted Submission(s): 12709


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
 

Author
CHEN, Gaoli
 

Source
ZOJ Monthly, January 2003 
 

题意:给你一个0到n-1的一个数列,你可以将这个数列的前m项移到最后,可以得到一个新的数列,0 ≤ m < n ,就是一共有n个数列,找到其中逆序对数最少的,并输出该逆序对数。

逆序对就是一个序列中一对数(A[i],A[j])满足A[i]>A[j]且 i < j 条件

求一个序列的逆序对数有以下几种方法:

1、比较暴力的来,对每一个数求它有多少个逆序对,最后求和,两个for循环就行了。O(n^2)

2、归并(这个不怎么会)。

3、树状数组,可以边放边找(按值,找已经放入的数中小于等于该值的数有几个,减去放入的总数就是在该数放入前的比该数大的数,就是该数对应的逆序对数),也可以放完后再从大到小放(按位置,开一个数组标记每个位置的数放了没,刚开始初始化所有位置为0,每放入一个数,将该数对应位置的值更变为1,因为是从大到小放入的,所以每次放入时整个数组中只有比该值大的数对应位置的值为1,所以只要求区间[1,pos]内数组的和就行了,该值代表该数对应的逆序对数,pos为该数对应位置)。O(n lg n)


思路:如果直接对n个数列进行求对应的逆序对数,时间复杂度就是O(n^2lgn),n≤5000,会TLE。所以这样不行。说明应该可以有一推多,然后推一下,就可以发现当把一个序列的第一个数ai从整个数列中拿出,该数列会减少ai个逆序对数(ai从0至n-1,若ai为1至n则减少a1-1个),再将其放至数列尾部,该数列会增加n-1-ai个逆序对数(ai从0至n-1,若ai为1至n则增加n-ai个),就可以基于第一次得到的逆序对数快速的求出剩余的n-1个数列对应的逆序对数找到最小值。


//树状数组#include<iostream>#include<string.h>#include<stdio.h>using namespace std;int n,a[5050],b[5050],sum,mi;int lowbit(int x){    return x&-x;}void add(int pos,int ad){    while(pos<=n)    {        b[pos]+=ad;        pos+=lowbit(pos);    }}int Sum(int pos){    int ans = 0;    while(pos>0)    {        ans+=b[pos];        pos-=lowbit(pos);    }    return ans;}int main(){    while(scanf("%d",&n)!=EOF)    {        memset(b,0,sizeof b);        sum = 0;        for(int i=0;i<n;i++)        {            scanf("%d",&a[i]);            a[i]++;//我写的是按值来找,因为树状数组不能有0项,而ai中会出现0,所以ai要+1,            add(a[i],1);            sum+=i+1-Sum(a[i]);        }        mi = sum;        for(int i=0;i<n;i++)        {            sum = sum - (a[i] - 1) + (n - a[i]);            mi = min(mi,sum);        }        printf("%d\n",mi);    }    return 0;}



//线段树#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int maxn = 5005;int n,mi,a[maxn],b[maxn<<2],sum;void add(int pos,int l,int r,int x){    b[x]++;    int mid = (l+r)>>1;    if(l==r) return;    if(pos>mid) add(pos, mid+1, r, x<<1|1);    else add(pos, l, mid, x<<1);}int Sum(int pos,int l,int r,int x){    int ans = 0,mid = (l+r)>>1;    if(pos==r) return b[x];    if(pos>mid) ans+=b[x<<1]+Sum(pos,mid+1,r,x<<1|1);    else ans+=Sum(pos,l,mid,x<<1);    return ans;}int main(){    while(scanf("%d",&n)!=EOF)    {        sum = 0;        memset(b,0,sizeof(b));        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);            a[i]++;            add(a[i],1,n,1);            sum+=(i-Sum(a[i],1,n,1));        }        mi = sum;        for(int i=1;i<=n;i++)        {            sum = sum - (a[i]-1) + (n-a[i]);            mi = min(mi,sum);        }        printf("%d\n",mi);    }    return 0;}