【2014微软实习生笔试】3:逆序数问题


Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB

Description
Find a pair in an integer array that swapping them would maximally decrease the inversion count of the array. If such a pair exists, return the new inversion count; otherwise returns the original inversion count.

Definition of Inversion: Let (A[0], A[1] ... A[n]) be a sequence of n numbers. If i < j and A[i] > A[j], then the pair (i, j) is called inversion of A.

Example:
Count(Inversion({3, 1, 2})) = Count({3, 1}, {3, 2}) = 2
InversionCountOfSwap({3, 1, 2})=>
{
  InversionCount({1, 3, 2}) = 1 <-- swapping 1 with 3, decreases inversion count by 1
  InversionCount({2, 1, 3}) = 1 <-- swapping 2 with 3, decreases inversion count by 1
  InversionCount({3, 2, 1}) = 3 <-- swapping 1 with 2 , increases inversion count by 1
}

Input
Input consists of multiple cases, one case per line.Each case consists of a sequence of integers separated by comma.


Output
For each case, print exactly one line with the new inversion count or the original inversion count if it cannot be reduced.


Sample In
3,1,2
1,2,3,4,5

Sample Out
1
0


题目分析:

其中求逆序数也可以单独出一道题,怎么实现更高效,不过在这里就先不讨论了,可以看下这个链接《逆序数的几种求法》,下面关键求解如何有效查找交换哪两个数据,使得逆序数最低。

我们先来判断一下交换i和j,假设a和b中间有m+n个数据。

如果a>b,定义m为小于a的数据个数,n为大于a的数据个数;而这些中间个数中m+i个数据是大于b的,n-i个数据是小于b的==>交换ab的逆序数为(-m+n)+(-m-i+n-i)+(-1) = -1-2i。最后的-1是因为只考虑交换ab,使得逆序数减少1个。

如果我们考虑a<b交换的话,m为小于a的个数,n为大于a的个数;m-i为大于b的个数,n+i为小于b的个数==>交换ab逆序数为(-m+n)+(-m+i+n+i)+(1)=2i+1。

通过分析,我们只考虑a<b时的情况,交换才能有效减小逆序数。那么i的意义为,当a和b中间的数据小于a但是大于b的话,就为i贡献一个。因此我们判断ab之间有多少符合这种情况的数的个数即可。

不过时间复杂度仍然有些高,如果碰到更好的算法,再更新

代码如下:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 20

int ReduceInversion(int buf[],int n);
int CompareInversion(int buf[],int n);

int main()
{
    int buf[SIZE];
    int n;
    int OriginCount;
    int ReduceCount;

    while(scanf("%d",&n) == 1){
        for(int i = 0;i < n;++i){
            scanf("%d",&buf[i]);
        }
        OriginCount = CompareInversion(buf,n);
        printf("OriginCount = %d\n",OriginCount);
        ReduceCount = ReduceInversion(buf,n);
        printf("ReduceCount = %d\n",ReduceCount);
        printf("%d\n",OriginCount-ReduceCount);

    }
    return 0;
}

int ReduceInversion(int buf[],int n)
{
    int arr[SIZE][SIZE] = {0,0,0};  //一开始以为要通过数组保留,其实也可以不要,直接让2*count + 1与maxcount进行比较
    int count = 0;
    int maxcount = 0;

    for(int i = 0;i < n;i++){       //确定i<->j这两个位置交换时带来的逆序数的减小,这里用正数表示
        for(int j = i+1;j < n;j++){
            if(buf[i] > buf[j]){    //如果i比j大,交换才有意义
                count = 0;
                for(int k = i+1;k < j;k++){     //通过分析,得出如下的简化代码
                    if(buf[k] < buf[i] && buf[k] > buf[j])
                        count++;
                }
                arr[i][j] = 2*count + 1;
                if(arr[i][j] > maxcount)
                    maxcount = arr[i][j];
            }
        }
    }

    return maxcount;
}

//找出原方程的逆序数
int CompareInversion(int buf[],int n)
{
    int count = 0;
    for(int i = 0;i < n;++i){
        for(int j = i+1;j < n;++j)
            if(buf[i] > buf[j])
                count++;
    }
    return count;

}


总结:对于数字类的问题,要多进行变换,找出其中的规律。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值