• HDU 4911 Inversion


              http://acm.hdu.edu.cn/showproblem.php?pid=4911   归并排序求逆对数

                    Inversion

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 556    Accepted Submission(s): 239


    Problem Description
    bobo has a sequence a1,a2,…,an. He is allowed to swap two adjacent numbers for no more than k times.

    Find the minimum number of inversions after his swaps.

    Note: The number of inversions is the number of pair (i,j) where 1≤i<j≤n and ai>aj.
     
    Input
    The input consists of several tests. For each tests:

    The first line contains 2 integers n,k (1≤n≤105,0≤k≤109). The second line contains n integers a1,a2,…,an (0≤ai≤109).
     
    Output
    For each tests:

    A single integer denotes the minimum number of inversions.
     
    Sample Input
    3 1
    2 2 1
    3 0
    2 2 1
     
    Sample Output
    1
    2
    题意:相邻的最多调换k次,使得逆对数最小,
    思路,先求出整个序列的逆对数-k次就可,如果出现负数就输出为0.用归并排序求逆对数。
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    __int64 a[100005],cnt,c[100005];
    __int64 k;
    void merg(int low, int mid, int high)
    {
      int i=low, j=mid+1;
      cnt=0;
      while(i<=mid&&j<=high)
      {
        if(a[i]>a[j])
        {
          c[cnt++]=a[j++];
          k+=mid-i+1;
        }
        else
        {
          c[cnt++]=a[i++];
        }
      }
      while(i<=mid)
      {
        c[cnt++]=a[i++];
      }
      while(j<=high)
      {
        c[cnt++]=a[j++];
      }
      cnt=0;i=low;
      while(i<=high)
      {
        a[i++]=c[cnt++];
      }
    }
    void merger(int low, int high)
    {
      int mid;
      if(low<high)
      {
        mid=(low+high)/2;
        merger(low,mid);
        merger(mid+1,high);
        merg(low,mid,high);
      }
    }
    int main()
    {
      int i,n,m;
      while(~scanf("%d%d",&n,&m))
      {
               k=0;
        for(i=0; i<n; i++)
        {
          scanf("%I64d",&a[i]);
        }
           merger(0,n-1);
        if(k-m<=0)
           printf("0
    ");
         else
        printf("%I64d
    ",k-m);
      }
      return 0;
    }
     
  • 相关阅读:
    使用JQuery实现延迟加载UserControl
    VisualStudio中的列选择
    SQL SERVER 2008 CTE生成结点的FullPath
    woocommerce独立站建站
    Java NIO使用及原理分析(二)
    java.io学习总结 转载
    java io与装饰器模式
    函数式思维: 不变性
    函数式思维: 运用函数式思维,第2 部分
    maven添加非官方jar包到本地库(maven: install an external jar into local maven repository)
  • 原文地址:https://www.cnblogs.com/cancangood/p/3893959.html
Copyright © 2020-2023  润新知