hdu 4491
#include <iostream> #include<stdio.h> #include<algorithm> #include<cmath> using namespace std; int a[100005]; int b[100005]; long long int ans; void Merge (int l,int t,int r) { int i=l; int j=t+1; int k=l; while(i <= t && j <= r) { if(a[i]>a[j]) { b[k++] = a[j++]; ans += t- i + 1; } else { b[k++] = a[i++]; } } while(i <= t) b[k++] = a[i++]; while(j <= r) b[k++] = a[j++]; for(int i=l;i<=r;i++) a[i] = b[i]; } void Merge_sort(int l,int r) { if(r>l) { int t=(r+l)/2; Merge_sort(l,t); Merge_sort(t+1,r); Merge(l,t,r); } } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } ans=0; Merge_sort(1,n); cout<<max(ans-m,(__int64)0)<<endl; } return 0; }