码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1394

时间:2014-12-09 12:17:27      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:hdu   1394   归并排序   

Minimum Inversion Number

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


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
 


逆序数概念:

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

由于本题的数据特殊性,使得我们可以在已知当前逆序对数量后,计算下一次移动后产生的逆序对数:

假设我们知道了原序列的逆序数是first,那么每次移动后怎么算出新的逆序数呢?因为每次都只是移动头元素,假设头元素为x,那么可以知道由x产生的逆序对的个数为x,因为有x个数小于它(0,1,2……x-1),如果将它放到了末尾,那么这x个逆序对将会消失。同样地,整个序列中有(n-1-x)个元素大于x,那么x移到了末尾,将产生(n-1-x)个新的逆序对(这些逆序对分别为(x+1,x),(x+2,x),(x+3,x)……(n-1,x))。因此可以递推地解决这个问题。如果知道了当前序列逆序数为sum,那么移动头元素后的逆序数将会是sum-x+(n-1-x)

#include<iostream>
using namespace std;
#define M 5010
int a[M];
int aux[M];
int b[M];
long long int ans;
void merge(int a[],int l,int mid,int h)
{
    int i=l;
    int j=mid+1;
    for(int k=l;k<=h;++k)
        aux[k]=a[k];
    for(int k=l;k<=h;++k)
    {
        if(i>mid)a[k]=aux[j++];
        else if(j>h)a[k]=aux[i++];
        else if(aux[i]>aux[j])
        {
            a[k]=aux[j++];
            ans+=mid-i+1;
        }
        else
            a[k]=aux[i++];
    }
}
void sort(int a[],int l,int h)
{
    if(l>=h)return ;
    int mid=l+(h-l)/2;
    sort(a,l,mid);
    sort(a,mid+1,h);
    merge(a,l,mid,h);
}
int main(int argc, char *argv[])
{
    freopen("1394.in","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        memset(aux,0,sizeof(aux));
        memset(b,0,sizeof(b));
        int i=0;
        while(n--)
        {
            scanf("%d",&a[i]);
            b[i]=a[i];
            i++;
        }
        ans=0;
        sort(a,0,i-1);
        long long int MIN=ans;
        for(int j=0;j<i;++j)
        {
            ans=ans-b[j]+(i-b[j]-1);
            if(ans<MIN)MIN=ans;
        }
        printf("%lld\n",MIN);
    }
    return 0;
}


HDU 1394

标签:hdu   1394   归并排序   

原文地址:http://blog.csdn.net/wdkirchhoff/article/details/41821525

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!