HUD 1394 归并求逆序数

题目

Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15360
Accepted Submission(s): 9368

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

Author CHEN, Gaoli

Source ZOJ Monthly, January 2003

分析

首先调用归并排序模板merge_sort
求出初始的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;

代码

#include<stdio.h>  
#include<malloc.h>  
int ans, a[5050], b[5050];
void merge(int left, int mid, int right)
{
    int i, j, cnt = 0;
    int *p;
    p = (int *)malloc((right - left + 1)*sizeof(int));
    i = left;
    j = mid + 1;
    while (i <= mid&&j <= right)//这时候i 和 j 指向的部分都排序完毕了 现在合并  
    {
        if (a[i] <= a[j])
        {
            p[cnt++] = a[i];
            i++;
        }
        else
        {
            p[cnt++] = a[j];
            j++;
            ans += mid - i + 1;//第i个比j大 由于i已经从小到大排过序了 那么i+1到mid的也会比j大  
        }
    }
    while (i <= mid)
    {
        p[cnt++] = a[i++];
    }
    while (j <= right)
    {
        p[cnt++] = a[j++];
    }
    cnt = 0;
    for (i = left; i <= right; i++)
        a[i] = p[cnt++];
    free(p);

}
void merge_sort(int left, int right)
{
    if (left<right) //长度大于1  这是个判断不是循环  
    {
        int mid;
        mid = (left + right) / 2;
        merge_sort(left, mid);
        merge_sort(mid + 1, right);
        merge(left, mid, right);
    }
}

int main()
{
    int n, i, j;
    while (scanf_s("%d", &n) != EOF)
    {

        for (i = 0; i<n; i++) 
        { 
            scanf_s("%d", &a[i]); 
            b[i] = a[i]; 
        }
        ans = 0;
        merge_sort(0, n - 1); 
        int cnt = 999999999;
        if (cnt>ans) cnt = ans;

        for (i = 0; i<n; i++) 
        { 
            ans = ans - b[i] + n - 1 - b[i];
            if (cnt>ans)  cnt = ans;    
        }                                   
        printf("%d\n", cnt);        
    }
    return 0;
}/*那么将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;
  */
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值