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): 15476 Accepted Submission(s): 9441


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次线段树的操作是不可取的,所以要特殊考虑规律.....

假如 某个序列的逆序数为 s ,这个序列的最后一个元素是 am,一共n 个数,那么,在这个数的前面 有 n-am个数比am大,有 am-1个数比am 小,那么进过操作之后,逆序数会减少n-am,增加 am-1,可得,新得到的数列的逆序数为 s+n-2*am-1.....


此题用归并求逆序比较好用,因为这个题的数据比较特殊,用线段树还是比较方便的,另外还有树状数组.......


93MS

#include<stdio.h>
#include<string.h>
int sum[4000005],x[1000005];
int find(int rt,int l,int r,int a,int b)
{
    if(l>=a&&r<=b)
    {
        return sum[rt];
    }
    int mid=(l+r)>>1,tp=rt<<1,res=0;
    if(mid>=a)
    {
        res+=find(tp,l,mid,a,b);
    }
    if(mid<b)
    {
        res+=find(tp|1,mid+1,r,a,b);
    }
    return res;
}
void update(int rt,int l,int r,int p)
{
    if(l==r)
    {
        ++sum[rt];
        return;
    }
    int mid=(l+r)>>1,tp=rt<<1;
    if(p<=mid)
    {
        update(tp,l,mid,p);
    }
    else
    {
        update(tp|1,mid+1,r,p);
    }
    sum[rt]=sum[tp]+sum[tp|1];
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int cnt=0;
        memset(sum,0,sizeof(sum));
        for(int i=0;i<n;++i)
        {
            scanf("%d",&x[i]);
            cnt+=find(1,0,n-1,x[i],n-1);
            update(1,0,n-1,x[i]);
        }
        int tp=cnt;
        for(int i=0;i<n;++i)
        {
            tp+=(n-2*x[i]-1);
            if(tp<cnt)
            {
               cnt=tp;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}



归并排序:(46MS)


#include<stdio.h>
#include<string.h>
int cnt,x[5005],t[5005];
void merge(int l,int r)
{
    int mid=(l+r)>>1;
    int a=l,b=mid+1,c=l;
    while(a<=mid&&b<=r)
    {
        if(x[a]<=x[b])
        {
            t[c++]=x[a++];
        }
        else
        {
            cnt+=mid-a+1;
            t[c++]=x[b++];
        }
    }
    while(a<=mid)
    {
        t[c++]=x[a++];
    }
    while(b<=r)
    {
        t[c++]=x[b++];
    }
    for(int i=l;i<=r;++i)
    {
        x[i]=t[i];
    }
}

void sort(int l,int r)
{
    if(l<r)
    {
        int mid=(l+r)>>1;
        sort(l,mid);sort(mid+1,r);
        merge(l,r);
    }
}

int main()
{
    int n,y[5005];
    //freopen("shuju.txt","r",stdin);
    while(~scanf("%d",&n))
    {
        //memset(sum,0,sizeof(sum));
        for(int i=0;i<n;++i)
        {
            scanf("%d",&x[i]);
            y[i]=x[i];
        }
        cnt=0;
        sort(0,n-1);
        int tp=cnt;
        for(int i=0;i<n;++i)
        {
            tp+=(n-2*y[i]-1);
            if(tp<cnt)
            {
               cnt=tp;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}


用树状数组的方法有空再补上......



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值