标签:
Description
Input
Output
Sample Input
3 1 2 2 1 3 0 2 2 1
Sample Output
1 2
/* 归并排序求逆序对对数,然后减去可以交换的次数 因为数据范围有10^9,所以逆序对的个数可能超过int类型 */ #include <iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int n; long long k,ans; int a[100005],b[100005]; void work(int l,int mid,int r) { int i=l,j=mid+1; int len=0; while(i<=mid && j<=r) { if (a[i]>a[j]) { b[++len]=a[j++]; ans+=mid-i+1; } else b[++len]=a[i++]; } while(i<=mid) b[++len]=a[i++]; while(j<=r) b[++len]=a[j++]; for(int i=1;i<=len;i++) a[l+i-1]=b[i]; } void guibing(int l,int r) { int mid; if (l>=r) return; mid=(l+r)/2; guibing(l,mid); guibing(mid+1,r); work(l,mid,r); } int main() { while(~scanf("%d%lld",&n,&k)) { ans=0; for(int i=1;i<=n;i++) scanf("%d",&a[i]); guibing(1,n); printf("%lld\n",ans-k>0?ans-k:0); } return 0; }
标签:
原文地址:http://www.cnblogs.com/stepping/p/5815040.html