zoukankan      html  css  js  c++  java
  • 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 ",ans); } return 0; }

      

  • 相关阅读:
    Inception V1-V4
    NDCG的理解
    进程与线程
    Java中的接口和抽象类
    HashMap的工作原理
    基于比较排序的算法复杂度的下界
    数据库-left join,right join,inner join,full join
    外排序 External sorting
    数据流中的中位数 Find Median from Data Stream
    Codeforces Round #272 (Div. 2)
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4742661.html
Copyright © 2011-2022 走看看