参见:http://soj.sysu.edu.cn/show_problem.php?pid=1004&cid=569
果然人脑是有问题的,测试样列还是随机生成的好
Design an efficient fine_median algorithm of logrithmic running time.
typedef int Comparable;
Comparable find_median(const vector<Comparable> &l1, const vector<Comparable> &l2);
/*
Pre: The ordered lists l1,l2 are not all empty.
Post: The median of the two lists combined in order is returned. If the
size of the merged list is even, then the first of the two medians
is returned. For exmaple, l1 =(1,2), l2=(1,3,4,5), then 2 is returned.
*/
#include <vector> using std::vector; typedef int Comparable; //#include <iostream> //#include <cstdlib> //using std::cout; Comparable find_median(const Comparable *arrA, int lenA, const Comparable *arrB, int lenB) { // static int record = 0; // ++record; // if (record % 100 == 0) { // system("pause"); // } // cout << "Times " << record << "\narrA: "; // for (int i = 0; i < lenA; ++i) { // cout << arrA[i] << ‘ ‘; // } // cout << ‘\n‘; // cout << "arrB: "; // for (int i = 0; i < lenB; ++i) { // cout << arrB[i] << ‘ ‘; // } // cout << "\n\n"; if (lenA == lenB && lenA == 1) { return (*arrA > *arrB ? *arrB: *arrA); } int midA = (lenA - 1) / 2; int midB = (lenB - 1) / 2; if (lenA == 1) { if (lenB % 2 == 1) { if (arrA[0] >= arrB[midB]) { return arrB[midB]; } else if (arrA[0] <= arrB[midB - 1]) { return arrB[midB - 1]; } else { return arrA[0]; } } else { if (arrA[0] >= arrB[midB + 1]) { return arrB[midB + 1]; } else if (arrA[0] <= arrB[midB]) { return arrB[midB]; } else { return arrA[0]; } } } else if (lenB == 1) { return find_median(arrB, lenB, arrA, lenA); } int l = (midA <= midB? midA: midB); //cout << "l: " << l << " midA: " << midA << " midB: " << midB << ‘\n‘; if (lenA == 2 || lenB == 2) { // ++l; } if (arrA[midA] == arrB[midB]) { return arrA[midA]; } else if (arrA[midA] < arrB[midB]) { return find_median(arrA + l, lenA - l, arrB, lenB - l); } else { return find_median(arrA, lenA - l, arrB + l, lenB - l); } } Comparable find_median (const vector<Comparable> &l1, const vector<Comparable> &l2) { return find_median(&l1[0], l1.size(), &l2[0], l2.size() ); } // 下面这个是O(n)版本,用于检测正确性 using std::vector; Comparable find_median2(const vector<Comparable> &l1, const vector<Comparable> &l2) { vector<Comparable>::const_iterator it1, it2; it1 = l1.begin(); it2 = l2.begin(); unsigned int mid = (l1.size() + l2.size() - 1) / 2; unsigned int k = 0; while (k < mid && it1 != l1.end() && it2 != l2.end()) { if (*it1 <= *it2) { ++it1; } else { ++it2; } ++k; } while (it1 != l1.end() && k < mid) { ++it1; ++k; } while (it2 != l2.end() && k < mid) { ++it2; ++k; } if (it1 == l1.end()) { return *it2; } else if (it2 == l2.end()) { return *it1; } else { return (*it1 < *it2? *it1: *it2); } } // 测试程序 #include <algorithm> #include <iostream> #include <ctime> #include <cstdlib> using std::cout; using std::sort; int main() { //vector<Comparable> l1 = {1, 2, 4.2, 7, 56}; vector<Comparable> l1; vector<Comparable> l2; srand(time(0)); int N = 100; // 测试样例个数 while (N--) { l1 = vector<Comparable>(rand() % 4 + 1); l2 = vector<Comparable>(rand() % 4 + 1); for (auto &i : l2) { i = rand() % 100;; } for (auto &i : l1) { i = rand() % 100; } sort(l1.begin(), l1.end()); sort(l2.begin(), l2.end()); int m1 = find_median(l1, l2); int m2 = find_median2(l1, l2); cout << m1 << ‘\n‘; cout << m2 << ‘\n‘; if (m1 != m2) { system("pause"); } } }
时间: 2024-10-03 22:15:39