-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedian.cpp
49 lines (48 loc) · 901 Bytes
/
Median.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
#include <iostream>
long int a[200010];
int main()
{
std::ios::sync_with_stdio(false);
long int n, m;
std::cin >> n;
for (long int i = 0; i < n; i++)
{
std::cin >> a[i];
}
std::cin >> m;
long int median = (m + n + 1) / 2;
long int j = 0;
long int count = 0;
long int b;
for (long int i = 0; i < m; i++)
{
std::cin >> b;
while (a[j] < b && j < n)
{
count++;
if (count == median)
{
std::cout << a[j];
return 0;
}
j++;
}
count++;
if (count == median)
{
std::cout << b;
return 0;
}
}
while (true)
{
count++;
if (count == median)
{
std::cout << a[j];
return 0;
}
j++;
}
return 0;
}