zoukankan      html  css  js  c++  java
  • 逆序数(归并排序 )

     题目链接:
    http://acm.nyist.net/JudgeOnline/problem.php?pid=117
    1
    #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int a[1000001], b[1000001]; 6 long long count;  7 8 void Merge(int i, int m, int j){ 9 int k, x, y; 10 y = m; 11 for(k = i; k <= j; ++k) b[k] = a[k]; 12 for(k = i, x = m + 1; i <= m && x <= j; k++){ 13 if(b[i] <= b[x]) a[k] = b[i++]; 14 else{ 15 a[k] = b[x++]; 16 count = count + m - i + 1; 17 } 18 } 19 while(i <= m) a[k++] = b[i++]; 20 while(x <= j) a[k++] = b[x++]; 21 } 22 23 void Msort(int i, int j){ 24 if(i < j){ 25 int m = (i + j) / 2; 26 Msort(i, m); 27 Msort(m + 1, j); 28 Merge(i, m, j); 29 } 30 } 31 32 int main() 33 { 34 int T, i, j, k, N; 35 scanf("%d",&T); 36 while(T--){ 37 scanf("%d", &N); 38 for(i = 1; i <= N; ++i) scanf("%d",&a[i]); 39 count = 0; 40 Msort(1, N); 41 printf("%lld ", count); 42 } 43 return 0; 44 }
  • 相关阅读:
    React 进修之路(1)
    requireJS的配置心得
    DOM的内部插入和外部插入
    h5移动端设备像素比dpr介绍
    原生js--事件类型
    React 进修之路(3)
    javaScript
    html&css
    MyBatis
    Maven基础
  • 原文地址:https://www.cnblogs.com/yaling/p/3180838.html
Copyright © 2011-2022 走看看