zoukankan      html  css  js  c++  java
  • 归并排序求逆序対




    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; }

     

  • 相关阅读:
    51nod——T1267 4个数和为0
    cf220B莫队
    cf220b
    poj1436水平可见线
    poj2528贴海报,,
    poj3468
    hdu1698
    ural1989 单点更新+字符串hash
    cf Queries on a String
    hdu4605
  • 原文地址:https://www.cnblogs.com/2014slx/p/7861502.html
Copyright © 2011-2022 走看看