标签:cond else res void color fir ack etc sort
class Solution { public: vector<int> count; void merge_sort_two_vec(vector<pair<int, int>>& sbu_vec1, vector<pair<int, int>> &sbu_vec2, vector<pair<int, int>>& vec) { int i = 0, j = 0; while(i<sbu_vec1.size() && j<sbu_vec2.size()) { if(sbu_vec1[i].first <= sbu_vec2[j].first) { count[sbu_vec1[i].second] += j; vec.push_back(sbu_vec1[i]); i++; } else { vec.push_back(sbu_vec2[j]); j++; } } for(;i<sbu_vec1.size(); ++i) { count[sbu_vec1[i].second] += j; vec.push_back(sbu_vec1[i]); } for(;j<sbu_vec2.size(); ++j) { vec.push_back(sbu_vec2[j]); } } void merge_sort(vector<pair<int, int>>& vec) { if(vec.size() < 2) return ; int mid = vec.size()/2; vector<pair<int, int>> sbu_vec1; vector<pair<int, int>> sbu_vec2; for(int i=0; i<mid; ++i) { sbu_vec1.push_back(vec[i]); } for(int i=mid; i<vec.size(); ++i) { sbu_vec2.push_back(vec[i]); } merge_sort(sbu_vec1); merge_sort(sbu_vec2); vec.clear(); merge_sort_two_vec(sbu_vec1, sbu_vec2, vec); } vector<int> countSmaller(vector<int>& nums) { vector<pair<int, int>> vec; for(int i=0; i<nums.size(); ++i) { vec.push_back(make_pair(nums[i], i)); } count.resize(nums.size(), 0); merge_sort(vec); return count; } };
标签:cond else res void color fir ack etc sort
原文地址:https://www.cnblogs.com/randyniu/p/9348939.html