//快速排序 void qsort_my(vector<int> & nums, int st, int ed){ if(st > ed) return; int key = nums[st]; int i = st, j = ed; while(i < j){ while(i < j && nums[j] >= key) --j; nums[i] = nums[j]; while(i < j && nums[i] <= key) ++i; nums[j] = nums[i]; } nums[i] = key; qsort_my(nums, st, i - 1); qsort_my(nums, j + 1, ed); } //单个无序表第K大的数 int KthNum(vector<int> nums, int st, int ed, int k){ int key = nums[st]; int i = st, j = ed; while(i < j){ while(i < j && nums[j] >= key) --j; nums[i] = nums[j]; while(i < j && nums[i] <= key) ++i; nums[j] = nums[i]; nums[i] = key; } nums[i] = key; if(k == i - st + 1) return key; else if(k > i - st + 1) return KthNum(nums, i + 1, ed, k - i + st - 1); else return KthNum(nums, st, i - 1, k); } //两个有序表第K大的数 int KthNum2(vector<int> nums1, int st1, int ed1, vector<int> nums2, int st2, int ed2, int k){ if(st1 > ed1) return nums2[st2 + k - 1]; else if(st2 > ed2) return nums1[st1 + k - 1]; else if(k == 1) return min(nums1[st1], nums2[st2]); int pt1 = min(k / 2 + st1 - 1, ed1); int pt2 = min(k / 2 + st2 - 1, ed2); if(nums1[pt1] >= nums2[pt2]) return KthNum2(nums1, st1, ed1, nums2, pt2 + 1, ed2, k - pt2 + st2 - 1); else return KthNum2(nums1, pt1 + 1, ed1, nums2, st2, ed2, k - pt1 + st1 - 1); }