标签:des style blog color java os io strong
归并排序求逆序数,然后ans-k与0取一个最大值就可以了。
也可以用树状数组做,比赛的时候可能姿势不对,树状数组wa了、、
3 1 2 2 1 3 0 2 2 1
1 2
#include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #include <iomanip> #include <stdio.h> #include <string> #include <queue> #include <cmath> #include <stack> #include <map> #include <set> #define eps 1e-12 ///#define M 1000100 #define LL __int64 ///#define LL long long ///#define INF 0x7ffffff #define INF 0x3f3f3f3f #define PI 3.1415926535898 #define zero(x) ((fabs(x)<eps)?0:x) using namespace std; const int maxn = 10010000; int num[maxn]; LL ans; int tmp[maxn]; void Sell(int l, int mid, int r) { int i = l; int j = mid+1; int k = 0; while(i <= mid && j <= r) { if(num[i] > num[j]) { tmp[k++] = num[j++]; ans += mid-i+1; } else tmp[k++] = num[i++]; } while(i <= mid) tmp[k++] = num[i++]; while(j <= r) tmp[k++] = num[j++]; for(int i = 0; i < k; i++) num[i+l] = tmp[i]; } void Sort(int l, int r) { if(l < r) { int mid = (l+r)/2; Sort(l, mid); Sort(mid+1, r); Sell(l, mid, r); } } int main() { int n, k; while(cin >>n>>k) { ans = 0; for(int i = 0;i < n; i++) scanf("%d",&num[i]); Sort(0, n-1); cout<<max(0LL, ans-k)<<endl; } return 0; }
HDU 4911 Inversion(归并排序求逆序数),布布扣,bubuko.com
标签:des style blog color java os io strong
原文地址:http://blog.csdn.net/xu12110501127/article/details/38396323