码迷,mamicode.com
首页 > 编程语言 > 详细

九度oj 1348 数组中的逆序对

时间:2015-05-27 22:29:53      阅读:233      评论:0      收藏:0      [点我收藏+]

标签:

原题链接:http://ac.jobdu.com/problem.php?pid=1348 
归并排序求逆序对。。。

技术分享
 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cstdlib>
 4 #include<cstring>
 5 #include<cstdio>
 6 typedef unsigned long long ull;
 7 const int Max_N = 100010;
 8 ull res;
 9 int arr[Max_N], temp[Max_N];
10 void Merge(int *A, int l, int m, int r) {
11     int p = 0;
12     int x = l, y = m + 1;
13     while (x <= m && y <= r) {
14         if (A[x] > A[y]) res += m - x + 1, temp[p++] = A[y++];
15         else temp[p++] = A[x++];
16     }
17     while (x <= m) temp[p++] = A[x++];
18     while (y <= r) temp[p++] = A[y++];
19     for (x = 0; x < p; x++) A[l + x] = temp[x];
20 }
21 void MergeSort(int *A, int l, int r) {
22     int mid;
23     if (l < r) {
24         mid = (l + r) >> 1;
25         MergeSort(A, l, mid);
26         MergeSort(A, mid + 1, r);
27         Merge(A, l, mid, r);
28     }
29 }
30 int main() {
31 #ifdef LOCAL
32     freopen("in.txt", "r", stdin);
33     freopen("out.txt", "w+", stdout);
34 #endif
35     int n;
36     while (~scanf("%d", &n)) {
37         res = 0;
38         for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
39         MergeSort(arr, 0, n - 1);
40         printf("%lld\n", res);
41     }
42     return 0;
43 }
View Code

 

九度oj 1348 数组中的逆序对

标签:

原文地址:http://www.cnblogs.com/GadyPu/p/4534387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!