- Reverse Pairs
中文English
Reverse pair is a pair of numbers (A[i], A[j]) such that A[i] > A[j] and i < j. Given an array, return the number of reverse pairs in the array.
Example
Example1
Input: A = [2, 4, 1, 3, 5]
Output: 3
Explanation:
(2, 1), (4, 1), (4, 3) are reverse pairs
Example2
Input: A = [1, 2, 3, 4]
Output: 0
Explanation:
No reverse pair
Solution 1: Fenwick tree
reference online answer.
note:
- The solution of the C [x] x represents the inside of the actual value of x, instead of the subscript.
- Code words discrete useful [3,2,100000] may be simplified to [2,1,3], and does not affect the results. Note the last to +1.
for (int i = 0; i < A.size(); ++i) {
A[i] = lower_bound(sortedA.begin(), sortedA.begin() + uniqLen, A[i]) - sortedA.begin() + 1;
}
- Note to heavy, so use the unique () function is very useful.
- A [i] to be from the beginning, C [] A ratio of size [] over.
- Two line sequential calculation result for loop can be interchanged.
result += sum(uniqLen) - sum(A[i]);
add(A[i], 1);
code show as below:
class Solution {
public:
/**
* @param A: an array
* @return: total of reverse pairs
*/
long long reversePairs(vector<int> &A) {
vector<int> sortedA = A;
sort(sortedA.begin(), sortedA.end());
uniqLen = unique(sortedA.begin(), sortedA.end()) - sortedA.begin();
C.resize(uniqLen + 1, 0);
long long result = 0;
//discrete A[]
for (int i = 0; i < A.size(); ++i) {
A[i] = lower_bound(sortedA.begin(), sortedA.begin() + uniqLen, A[i]) - sortedA.begin() + 1;
}
for (int i = 0; i < A.size(); ++i) {
result += sum(uniqLen) - sum(A[i]);
add(A[i], 1);
}
return result;
}
private:
int uniqLen;
vector<int> C;
int lowbit(int x) {
return x & (-x);
}
int sum(int x) {
int result = 0;
for (int i = x; i > 0; i -= lowbit(i)) {
result += C[i];
}
return result;
}
void add(int x, int v) {
for (int i = x; i <= uniqLen; i += lowbit(i)) {
C[i] += v;
}
}
};
Method 2: Similar to Method 1, but similarly constructed with C brutal force []. Time complexity of O (n ^ 2).
Time out.
class Solution {
public:
/**
* @param A: an array
* @return: total of reverse pairs
*/
long long reversePairs(vector<int> &A) {
vector<int> sortedA = A;
sort(sortedA.begin(), sortedA.end());
uniqLen = unique(sortedA.begin(), sortedA.end()) - sortedA.begin();
C.resize(uniqLen + 1, 0);
long long result = 0;
//discrete A[]
for (int i = 0; i < A.size(); ++i) {
A[i] = lower_bound(sortedA.begin(), sortedA.begin() + uniqLen, A[i]) - sortedA.begin() + 1;
}
for (int i = 0; i < A.size(); ++i) {
for (int j = A[i]; j <= uniqLen; ++j) {
C[j]++;
}
result += C[uniqLen] - C[A[i]];
}
return result;
}
private:
int uniqLen;
vector<int> C;
};
Solution 3: Merge Sort
need to add this line:
Result = MID + -. 1 left +;
class Solution {
public:
/**
* @param A: an array
* @return: total of reverse pairs
*/
long long reversePairs(vector<int> &A) {
buf.resize(A.size(), 0);
return mergeSort(A, buf, 0, A.size() - 1);
}
private:
long long mergeSort(vector<int> & A, vector<int> & buf, int start, int end) {
if (start >= end) return 0;
int result = 0;
int mid = start + (end - start) / 2;
result += mergeSort(A, buf, start, mid);
result += mergeSort(A, buf, mid + 1, end);
result += merge(A, buf, start, end);
return result;
}
long long merge(vector<int> & A, vector<int> & buf, int start, int end) {
int result = 0;
int mid = start + (end - start) / 2;
int left = start, right = mid + 1, index = start;
while(left <= mid && right <= end) {
if (A[left] <= A[right]) {
buf[index++] = A[left++];
} else {
buf[index++] = A[right++];
result += mid - left + 1;
}
}
while(left <= mid) {
buf[index++] = A[left++];
}
while(right <= end) {
buf[index++] = A[right++];
}
for (index = start; index <= end; index++) {
A[index] = buf[index];
}
return result;
}
vector<int> buf;
};