1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| auto optimize_cpp_stdio = []() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return 0; }(); class Solution { public: const static int maxn = 1e5 + 10; const static int maxm = 1e5 + 10; const static int INF = 0x3f3f3f3f; vector<int> getSeq(vector<int> &num, int k) { vector<int> st(k); int top = -1, remain = num.size(); for (int i = 0; i < num.size(); ++i) { while (top >= 0 && top + 1 + remain > k && st[top] < num[i]) { --top; } --remain; if (top + 1 < k) st[++top] = num[i]; } return st; } vector<int> merge(vector<int> &nums1, vector<int> &nums2) { int l1 = 0, l2 = 0; int m = nums1.size(), n = nums2.size(); if (m == 0) return nums2; if (n == 0) return nums1; vector<int> ret; ret.reserve(m + n); while (l1 < m && l2 < n) { if (cmp(nums1, nums2, l1, l2)) ret.emplace_back(nums1[l1++]); else ret.emplace_back(nums2[l2++]); } while (l1 < m) ret.emplace_back(nums1[l1++]); while (l2 < n) ret.emplace_back(nums2[l2++]); return ret; } vector<int> maxNumber(vector<int> &nums1, vector<int> &nums2, int k) { int m = nums1.size(), n = nums2.size(); int l = max(0, k - n), r = min(m, k); vector<int> seq1, seq2, seq; vector<int> ans(k, 0); for (int i = l; i <= r; ++i) { seq1 = getSeq(nums1, i); seq2 = getSeq(nums2, k - i); seq = merge(seq1, seq2); if (cmp(seq, ans, 0, 0)) { ans = seq; } } return ans; } bool cmp(vector<int> &nums1, vector<int> &nums2, int l1, int l2) { int m = nums1.size(), n = nums2.size(); int i = l1, j = l2; while (i < m && j < n) { if (nums1[i] != nums2[j]) return nums1[i] > nums2[j]; i++; j++; } return m - l1 > n - l2; } };
|