-
Notifications
You must be signed in to change notification settings - Fork 0
/
kth_merge_sorted.cpp
60 lines (54 loc) · 1.1 KB
/
kth_merge_sorted.cpp
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
#include <cstdio>
#include <deque>
#include <queue>
#include <stack>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <bitset>
#include <list>
#include <set>
#include <map>
using namespace std;
// this function find KTH element in the sorted array.
// which will be constructed by merging of two sorted array a[], b[].
const int N = 10001;
int n, m;
int a[N], b[N];
// O(log k).
inline int findKth(int *a, int n, int *b, int m, int k) {
if (n > m) {
return findKth(b, m, a, n, k);
}
if (n == 0) {
return b[k - 1];
}
if (k == 1) {
return min(a[0], b[0]);
}
int i = min(k / 2, n), j = k - i;
if (a[i - 1] > b[j - 1]) {
return findKth(a, i, b + j, m - j, k - j);
} else {
return findKth(a + i, n - i, b, j, k - i);
}
}
int main() {
scanf("%d %d", &n, &m);
for (int i = 0; i < n; i++) {
scanf("%d", a + i);
}
for (int j = 0; j < m; j++) {
scanf("%d", b + j);
}
int q;
scanf("%d", &q);
while (q--) {
int k;
scanf("%d", &k);
int ans = findKth(a, n, b, m, k);
printf("%d\n", ans);
}
}