(序列合并) 有两个长度为 N 的单调不降序列 A 和 B,序列的每个元素都是小于 109 的非负整数。在 A 和 B 中各取一个数相加可以得到 N2 个和,求其中第 K 小的和。上述参数满足 N≤105 和 1≤K≤N2。
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 | #include <iostream> using namespace std; const int maxn = 100005; int n; long long k; int a[maxn], b[maxn]; int* upper_bound(int *a, int *an, int ai) { int l = 0, r = ___①___; while (l < r) { int mid = (l+r)>>1; if (___②___) { r = mid; } else { l = mid + 1; } } return ___③___; } long long get_rank(int sum) { long long rank = 0; for (int i = 0; i < n; ++i) { rank += upper_bound(b, b+n, sum - a[i]) - b; } return rank; } int solve() { int l = 0, r = ___④___; while (l < r) { int mid = ((long long)l+r)>>1; if (___⑤___) { l = mid + 1; } else { r = mid; } } return l; } int main() { cin >> n >> k; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) cin >> b[i]; cout << solve() << endl; } |
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)
① 处应填( )?
② 处应填( )?
③ 处应填( )?
④ 处应填( )?
⑤ 处应填( )?