标签:des style blog color java os io strong
3 1 2 2 1 3 0 2 2 1
1 2
#include <iostream> #include <cstdio> #include <cstring> #include <vector> #include <string> #include <algorithm> #include <queue> #include <map> using namespace std; const int maxn = 100000+10; int sum[maxn]; int n,k; int num[maxn],tn[maxn]; map<int,int> mma; bool cmp(int a,int b){ return a > b; } int lowbit(int x){ return x&(-x); } void add(int x,int d){ while(x < maxn){ sum[x] += d; x += lowbit(x); } } int getS(int x){ int ret = 0; while(x > 0){ ret += sum[x]; x -= lowbit(x); } return ret; } int main(){ while(~scanf("%d%d",&n,&k)){ mma.clear(); memset(sum,0,sizeof sum); for(int i = 1; i <= n; i++){ scanf("%d",&num[i]); tn[i] = num[i]; } sort(tn+1,tn+n+1,cmp); int i = 1,cnt = 1; while(i <= n){ mma[tn[i]] = cnt; int j = i+1; while(j <= n && tn[i]==tn[j]){ j++; } cnt++; i = j; } long long ret = 0; for(int i = 1; i <= n; i++){ ret += getS(mma[num[i]]-1); add(mma[num[i]],1); } long long ans = ret-k; if(ans < 0) ans = 0; cout<<ans<<endl; } return 0; }
HDU4911-Inversion(树状数组),布布扣,bubuko.com
标签:des style blog color java os io strong
原文地址:http://blog.csdn.net/mowayao/article/details/38406435