-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProblem0107.h
71 lines (64 loc) · 1.68 KB
/
Problem0107.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//
// Created by Fengwei Zhang on 1/19/22.
//
#ifndef ACWINGSOLUTION_PROBLEM0107_H
#define ACWINGSOLUTION_PROBLEM0107_H
#include <iostream>
#include <cstring>
using namespace std;
class Problem0107 {
private:
static const int N = 500010;
long long CountReversed(int nums[], int temp[], const int st, const int ed) {
if (st >= ed) {
return 0;
}
const auto mid = st + (ed - st) / 2;
auto res = CountReversed(nums, temp, st, mid) + CountReversed(nums, temp, mid + 1, ed);
// 拆分归并排序和统计逆序对
auto l = st;
auto r = mid + 1;
while (r <= ed) {
while (l <= mid && nums[l] <= nums[r]) {
++l;
}
res += mid - l + 1;
++r;
}
l = st;
r = mid + 1;
auto tt = st;
while (l <= mid && r <= ed) {
if (nums[l] <= nums[r]) {
temp[tt++] = nums[l++];
} else {
temp[tt++] = nums[r++];
}
}
while (l <= mid) {
temp[tt++] = nums[l++];
}
while (r <= ed) {
temp[tt++] = nums[r++];
}
memcpy(nums + st, temp + st, (ed - st + 1) * sizeof(int));
return res;
}
int main() {
int nums[N];
int temp[N];
while (true) {
int n;
scanf("%d", &n);
if (!n) {
break;
}
for (int i = 0; i < n; ++i) {
scanf("%d", &nums[i]);
}
printf("%lld\n", CountReversed(nums, temp, 0, n - 1));
}
return 0;
}
};
#endif //ACWINGSOLUTION_PROBLEM0107_H