归并排序求逆序对
数组中的逆序对
http://www.nowcoder.com/questionTerminal/96bd6684e04a44eb80e6a68efc0ec6c5
class Solution { public: int t[200005]; long long merge_sort(vector<int> &data, int l, int r) { long long num = 0; if(l < r) { int mid = (l + r) / 2; num += merge_sort(data, l, mid) % 1000000007; num += merge_sort(data, mid + 1, r) % 1000000007; int i = l, j = mid + 1, k = 0; while(i <= mid && j <= r) { if(data[i] > data[j]) t[k ++] = data[j ++], num += mid - i + 1; else t[k ++] = data[i ++]; } while(i <= mid) t[k ++] = data[i ++]; while(j <= r) t[k ++] = data[j ++]; for(int i = l; i <= r; i ++) data[i] = t[i - l]; } return num % 1000000007; } int InversePairs(vector<int> data) { return merge_sort(data, 0, data.size() - 1); } };