标签:inverse practice question pre sep ack inter https 一个
class Solution { public: int result=0; void Merge(vector<int>&ans, int t, int m, int n) { vector<int>asd; int i = t; int j = m + 1; while (i <= m && j <= n) { if (ans[i] < ans[j]) { asd.push_back(ans[i]); i++; } else { result= result+m - i + 1; result %= 1000000007; asd.push_back(ans[j]); j++; } } while (i <= m) { asd.push_back(ans[i]); i++; } while (j <= n) { asd.push_back(ans[j]); j++; } for (i = 0; i < asd.size(); i++) { ans[t + i] = asd[i]; } } void MergeSort(vector<int>&ans,int m,int n) { if (m < n) { int k = (m + n) / 2; MergeSort(ans, m, k); MergeSort(ans, k + 1, n); Merge(ans, m, k, n); } } int InversePairs(vector<int> &data) { if (data.empty()) return 0; int len = data.size(); MergeSort(data, 0, len - 1); return result; } };
int main() { Solution a; int x; vector<int> ans; for (int i = 0; i < 8; i++) { cin >> x; ans.push_back(x); } cout<<a.InversePairs(ans)<<endl; for (int i = 0; i < 8; i++) { cout << ans[i] << " "; } }
标签:inverse practice question pre sep ack inter https 一个
原文地址:https://www.cnblogs.com/yoriko/p/12257526.html