-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlc170.cpp
85 lines (79 loc) · 2.64 KB
/
lc170.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
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <vector>
class Solution {
public:
int comp_num(int a, int m, int b, std::vector<int>& nums,
std::vector<int>& temp) {
int res = 0, p = a, q = m, i = a;
while (p < m && q < b) {
if (nums[p] > nums[q]) {
res += m-p;
temp[i++] = nums[q++];
} else
temp[i++] = nums[p++];
}
while (q < b) temp[i++] = nums[q++];
while (p < m) temp[i++] = nums[p++];
// for (int i = a; i < b; i++) {
// if (p < m && q < b && nums[p] > nums[q]) {
// temp[i] = nums[q];
// if (nums[p] > 2 * nums[q]) res += m - p;
// q++;
// } else if (p >= m) {
// temp[i] = nums[q];
// q++;
// } else {
// temp[i] = nums[p];
// p++;
// }
// }
for (int i = a; i < b; i++) nums[i] = temp[i];
// std::cout << res << '\n';
return res;
}
void function(int a, int b, std::vector<int>& nums, std::vector<int>& temp,
int* res) {
if (b - a <= 1) return;
int m = a + (b - a) / 2;
// if (a < m - 1) function(a, m, nums, temp, res);
function(a, m, nums, temp, res);
// if (m < b - 1) function(m, b, nums, temp, res);
function(m, b, nums, temp, res);
int t = comp_num(a, m, b, nums, temp);
*res += t;
}
int reversePairs(std::vector<int>& nums) {
int result = 0;
int* res = &result;
std::vector<int> temp = nums;
function(0, nums.size(), nums, temp, res);
return result;
}
};
// class Solution {
// public:
// long long reversePairs(std::vector<int>& nums) {
// long long res = 0;
// for (int i=0; i<nums.size();i++){
// for (int j=i+1; j<nums.size();j++){
// //long long temp = (long long)nums[i] - nums[j];
// //强制类型转换只用转换一个,另一个在计算时longlong -
// int会自动转换成long long
// // float int实数型 整数型,就不会自动转换
// if ((long long)nums[i] - nums[j] > nums[j]){
// res ++;
// // std::cout << res << " is " << nums[i] << " and " <<
// nums[j] << " then \n";
// }
// }
// }
// return res;
// }
// };
int main() {
std::vector<int> nums = {5,4,3,2,1};
Solution solution;
long long n = solution.reversePairs(nums);
std::cout << n;
return 0;
}