HDU 1394 Minimum Inversion Number

Minimum Inversion Number

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


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

求最小逆序数,n个数,有n种形式

线段树和树状数组都能做。

线段树代码

#include<stdio.h>
struct st
{
    int l;
    int r;
    int sum;
}f[4*5002];
int date[5002];
void build(int l,int r,int n)
{
    int mid=(l+r)/2;
    f[n].l=l;
    f[n].r=r;
    if(l==r)
        f[n].sum=date[l];
    else
    {
        build(l,mid,n*2);
        build(mid+1,r,n*2+1);
        f[n].sum=f[n*2].sum+f[n*2+1].sum;
    }
}
void update(int wz,int num,int n)
{
    int mid=(f[n].l+f[n].r)/2,t;
    if(f[n].l==wz&&f[n].r==wz)
    {
        f[n].sum=f[n].sum+num;
        t=n/2;
        while(t)
        {
            f[t].sum=f[t*2].sum+f[t*2+1].sum;
            t=t/2;
        }
    }
    else if(mid>=wz)
        update(wz,num,n*2);
    else update(wz,num,n*2+1);
}
int getsum(int l,int r,int n)
{
    int mid=(f[n].l+f[n].r)/2;
    if(f[n].l==l&&f[n].r==r)
        return f[n].sum;
    else if(mid>=r)
        getsum(l,r,n*2);
    else if(mid<l)
        getsum(l,r,n*2+1);
    else 
    {
        return getsum(l,mid,n*2)+getsum(mid+1,r,n*2+1);
    }
}
int main()
{
    int i,k,n,sum,s;
    while(scanf("%d",&n)>0)
    {
        for(i=1;i<=n;i++)
            date[i]=0;
        build(1,n,1);
        sum=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&date[i]);
            date[i]++;
        }
        for(i=1;i<=n;i++)
        {
            update(date[i],1,1);
            k=getsum(1,date[i],1);
            sum=sum+i-k;
        }
        s=sum;
        for(i=1;i<=n;i++)
        {
        //    printf("%d ",s);
            k=n-date[i]-(date[i]-1);
            s=s+k;
            if(s<sum)
                sum=s;    
        }
        printf("%d\n",sum);
    }
    return 0;
}

 

树状数组代码:

#include<stdio.h>
int a[5003],d[5003];
int lowbit(int x)
{
    return x&(-x);
}
int sum(int x,int *p)
{
    int k=0;
    while(x)
    {
        k=k+p[x];
        x=x-lowbit(x);
    }
    return k;
}
void add(int x,int n,int num,int *p)
{
    int i;
    for(i=x;i<=n;i=i+lowbit(i))
        p[i]=p[i]+num;
}
int main()
{
    int i,j,k,n,m,cz,k1,k2,min;
    while(scanf("%d",&n)>0)
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            a[i]++;
            d[i]=0;
        }
        for(i=1,cz=0;i<=n;i++)
        {
            add(a[i],n,1,d);
            k1=i-sum(a[i],d);
            cz=k1+cz;
        }
    //    printf("%d\n",cz);
        for(i=1,k1=0,min=cz;i<n;i++)
        {
            k1=n-a[i];
            k2=-a[i]+1;
            min=k1+k2+min;
        //    printf("%d ",k1+k2);
            if(min<cz)
                cz=min;
        }
        printf("%d\n",cz);
    }
    return 0;
}



            

 

posted @ 2013-04-29 20:40  芷水  阅读(142)  评论(0编辑  收藏  举报