-
Notifications
You must be signed in to change notification settings - Fork 1
/
493.cpp
38 lines (34 loc) · 1.12 KB
/
493.cpp
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
class Solution {
public:
vector<int> buff;
int count(vector<int>& nums, int begin, int mid, int end) {
int ans = 0;
int j = mid + 1;
for (int i = begin; i <= mid; i++) {
while (j <= end && nums[i] > nums[j] * 2LL) j++;
ans += j - (mid + 1);
}
return ans;
}
void merge(vector<int>& nums, int begin, int mid, int end) {
int i = begin;
int j = mid + 1;
for (int k = begin; k <= end; k++)
buff[k] = (j > end || (i <= mid && nums[i] <= nums[j])) ? nums[i++] : nums[j++];
for (i = begin; i <= end; i++) nums[i] = buff[i];
}
int sort(vector<int>& nums, int begin, int end) {
if (begin < end) {
int mid = (begin + end) / 2;
int ans = sort(nums, begin, mid) + sort(nums, mid + 1, end) + count(nums, begin, mid, end);
merge(nums, begin, mid, end);
return ans;
}
else return 0;
}
int reversePairs(vector<int>& nums) {
int len = nums.size();
buff.resize(len);
return sort(nums, 0, len - 1);
}
};