HDU4911——归并排序——Inversion

http://acm.hdu.edu.cn/showproblem.php?pid=4911

/*
裸题 有重复的不能用树状数组!!!!sort的时候会出错 */ /************************************************ * Author :Powatr * Created Time :2015-8-19 16:24:11 * File Name :A.cpp ************************************************/ #include <cstdio> #include <algorithm> #include <iostream> #include <sstream> #include <cstring> #include <cmath> #include <string> #include <vector> #include <queue> #include <deque> #include <stack> #include <list> #include <map> #include <set> #include <bitset> #include <cstdlib> #include <ctime> using namespace std; #define lson l, mid, rt << 1 #define rson mid + 1, r, rt << 1 | 1 typedef long long ll; const int MAXN = 1e5 + 10; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; int a[MAXN],temp[MAXN]; ll ans = 0; void mergesort(int *a,int first,int mid,int last,int *temp) { int i = first,j = mid + 1; int k = first; while( i <= mid && j <= last){ if(a[i] <= a[j]) temp[k++] = a[i++]; else { ans += j - k; temp[k++] = a[j++]; } } while( i <= mid) temp[k++] = a[i++]; while( j <= last) temp[k++] = a[j++]; for(int i = first;i <= last;i++) a[i] = temp[i]; } void mergearray(int *a,int first,int last,int *temp) { if(first < last){ int mid = (first + last) >> 1; mergearray(a,first,mid,temp); mergearray(a,mid+1,last,temp); mergesort(a,first,mid,last,temp); } } int main() { int n,m; while(~ scanf("%d%d",&n, &m)){ ans = 0; memset(temp, 0, sizeof(temp)); for(int i = 1; i <= n; i++) scanf("%d",&a[i]); mergearray(a,1,n,temp); ans -= m; if(ans < 0) ans = 0; printf("%I64d\n",ans); } return 0; }

  

posted @ 2015-08-19 16:41  Painting、时光  阅读(123)  评论(0编辑  收藏  举报