(归并第 k 小) 已知两个长度均为 n 的有序数组 a1 和 a2(均为递增序,但不保证严 格单调递增),并且给定正整数 k(1≤k≤2n),求数组 a1 和 a2 归并排序后的数组里 第 k 小的数值。
试补全程序。
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 | #include <bits/stdc++.h> using namespace std; int solve(int *a1, int *a2, int n, int k) { int left1 = 0, right1 = n - 1; int left2 = 0, right2 = n - 1; while (left1 <= right1 && left2 <= right2) { int m1 = (left1 + right1) >> 1; int m2 = (left2 + right2) >> 1; int cnt = ①; if (②) { if (cnt < k) left1 = m1 + 1; else right2 = m2 - 1; } else { if (cnt < k) left2 = m2 + 1; else right1 = m1 - 1; } } if (③) { if (left1 == 0) { return a2[k - 1]; } else { int x = a1[left1 - 1], ④; return std::max(x, y); } }else { if (left2 == 0) { return a1[k - 1]; } else { int x = a2[left2 - 1], ⑤; return std:: max(x, y); } } } |
0 of 5 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 5 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
①处应填( )?
②处应填( )?
③处应填( )?
④处应填( )?
⑤处应填( )?