写不出来不肯睡觉系列。。。。。。
#include<stdio.h> #include<iostream> #include<vector> using namespace std; class Solution { public: int upper(vector<int> a, int l, int r, int key){ if(a.size() == 0) return -1; if (key <= a[l]) { while(key == a[l]) l++; return l; } int mid; while (l <= r) { mid = (l + r) >> 1; if (a[mid - 1] <= key && key <=a[mid]) return mid; if (a[mid - 1] >= key) { r = mid - 1; } else { l = mid + 1; } } return r + 1; } double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int minL = nums1.size(); int maxL = nums2.size(); vector<int> minN; vector<int> maxN; if (minL <= maxL) { minN = nums1; maxN = nums2; } else { minL = nums2.size(); maxL = nums1.size(); minN = nums2; maxN = nums1; } int oneDone = -1; if (minL == 0) { oneDone = 0; } else if(maxL == 0){ oneDone = 1; } int curL = 0; int cen = (minL + maxL) / 2; //printf("%d\n", cen); int minP = 0; int maxP = 0; int index; int minMax = 0; while (curL < cen) { if(oneDone != -1) break; if(maxP + 1 < maxL && maxN[maxP] == maxN[maxP + 1]) { maxP++; curL++; } index = upper(minN, minP, minL - 1, maxN[maxP]); // while(index < minL && maxN[maxP] == minN[index]) index++; curL += index - minP; minP = index; minMax = 0; //printf("cen = %d, curL = %d, minP = %d, maxP = %d, index = %d\n", cen, curL, minP, maxP, index); if (minP >= minL){ oneDone = 0; break; } if (curL >= cen) break; if(minP + 1 < minL && minN[minP] == minN[minP + 1]) { minP++; curL++; } index = upper(maxN, maxP, maxL - 1, minN[minP]); //while(index < maxL && minN[minP] == maxN[index]) index++; curL += index - maxP; maxP = index; minMax = 1; //printf("cen = %d, curL = %d, minP = %d, maxP = %d, index = %d\n", cen, curL, minP, maxP, index); if (maxP >= maxL){ oneDone = 1; break; } if (curL >= cen) break; //int haha; //scanf("%d",&haha); } double ans = 0; if (oneDone == 0) { if (curL > cen) { int indexR = index - (curL - cen); if ((minL + maxL) % 2 == 1) { ans = minN[indexR]; } else { int indexL = indexR - 1; double cenR = minN[indexR]; double cenL = minN[indexL]; if (maxN[maxP] > minN[indexL]) { cenL = maxN[maxP]; } ans = (cenR + cenL) / 2.0; } } else { int indexR = maxP + cen - curL; if ((minL + maxL) % 2 == 1) { ans = maxN[indexR]; } else { int indexL = indexR - 1; if (indexL >= 0) { ans = (maxN[indexL] + maxN[indexR]) / 2.0; } if (minN[minL - 1] > maxN[indexL]) { ans = (minN[minL - 1] + maxN[indexR]) / 2.0; } } } } else if (oneDone == 1) { if (curL > cen) { int indexR = index - (curL - cen); if ((minL + maxL) % 2 == 1) { ans = maxN[indexR]; } else { int indexL = indexR - 1; double cenR = maxN[indexR]; double cenL = maxN[indexL]; if (minN[minP] > maxN[indexL]) { cenL = minN[minP]; } //printf("%f %f\n", cenR, cenL); ans = (cenR + cenL) / 2.0; } } else { int indexR = minP + cen - curL; if ((minL + maxL) % 2 == 1) { ans = minN[indexR]; } else { int indexL = indexR - 1; if (indexL >= 0) { ans = (minN[indexL] + minN[indexR]) / 2.0; } if(maxN[maxL - 1] > minN[indexL]){ ans = (maxN[indexL] + minN[indexR]) / 2.0; } } } } else { if (curL == cen) { if (minMax == 0) {// if ((minL + maxL) % 2 == 1) { ans = maxN[maxP]; } else { double cenR = maxN[maxP]; double cenL = minN[index - 1]; if(maxP >= 0 && maxN[maxP - 1] > cenL) { cenL = maxN[maxP - 1]; } ans = (cenR + cenL) / 2.0; } } else {// if ((minL + maxL) % 2 == 1) { ans = minN[minP]; } else { double cenR = minN[minP]; double cenL = maxN[index - 1]; if(minP >= 0 && minN[minP - 1] > cenL) { cenL = minN[minP - 1]; } ans = (cenR + cenL) / 2.0; } } } else { int indexR = index - (curL - cen); if (minMax == 0) { if ((minL + maxL) % 2 == 1) { ans = minN[indexR]; } else { double cenR = minN[indexR]; double cenL = minN[indexR - 1]; if (maxN[maxP] > minN[indexR - 1]) { cenL = maxN[maxP]; } ans = (cenR + cenL) / 2.0; } } else { if ((minL + maxL) % 2 == 1) { ans = maxN[indexR]; } else { double cenR = maxN[indexR]; double cenL = maxN[indexR - 1]; if (minN[minP] > maxN[indexR - 1]) { cenL = maxN[maxP]; } ans = (cenR + cenL) / 2.0; } } } } return ans; } }; int main() { vector<int> num1; num1.push_back(2); //num1.push_back(2); // num1.push_back(3); vector<int> num2; num2.push_back(1); num2.push_back(3); num2.push_back(4); // num2.push_back(10); // num2.push_back(11); // num2.push_back(13); // num2.push_back(15); // num2.push_back(21); // num2.push_back(22); // num2.push_back(23); // num2.push_back(24); // num2.push_back(25); // num2.push_back(26); // Solution s; double ans = s.findMedianSortedArrays(num1, num2); cout << ans << endl; //cout << s.upper(num1, 0, 3, 4) << endl; return 0; }
时间: 2024-10-07 05:27:02