|
| 1 | +#include <bits/stdc++.h> |
| 2 | +#include <ext/rope> |
| 3 | +using namespace std; |
| 4 | +using namespace __gnu_cxx; |
| 5 | + |
| 6 | +using ci = const int; |
| 7 | +using ld = long double; |
| 8 | +using llint = long long; |
| 9 | +using ullint = unsigned long long; |
| 10 | +using pii = pair <int,int>; |
| 11 | +using pcc = pair <char,char>; |
| 12 | +using pss = pair <string,string>; |
| 13 | +using vi = vector <int>; |
| 14 | +using vb = vector <bool>; |
| 15 | +using vii = vi::iterator; |
| 16 | + |
| 17 | +#define INF (1<<30) |
| 18 | +#define MOD 1000000007 |
| 19 | +#define mp make_pair |
| 20 | +#define mt make_tuple |
| 21 | +#define all(c) c.begin(), c.end() |
| 22 | +#define ms(name,val) memset(name, val, sizeof name) |
| 23 | +#define np nullptr |
| 24 | + |
| 25 | + |
| 26 | +int median(int *x, int n) |
| 27 | +{ |
| 28 | + if (n&1) return x[n>>1]; |
| 29 | + return (x[n>>1] + x[(n>>1)-1])>>1; |
| 30 | +} |
| 31 | + |
| 32 | +int getMedian(int *a, int *b, int n) |
| 33 | +{ |
| 34 | + if (n <= 0) return -1; |
| 35 | + if (n == 1) return (a[0] + b[0])>>1; |
| 36 | + if (n == 2) return (max(a[0],b[0])+min(a[1],b[1]))>>1; |
| 37 | + |
| 38 | + int m1 = median(a, n); |
| 39 | + int m2 = median(b, n); |
| 40 | + |
| 41 | + if (m1 == m2) |
| 42 | + return m1; |
| 43 | + |
| 44 | + if (m1 < m2) |
| 45 | + { |
| 46 | + if (n % 2 == 0) |
| 47 | + return getMedian(a + n/2 - 1, b, n - n/2 + 1); |
| 48 | + |
| 49 | + return getMedian(a + n/2, b, n- n/2); |
| 50 | + } |
| 51 | + |
| 52 | + if (n % 2 == 0) |
| 53 | + return getMedian(b + n/2 - 1, a, n - n/2 + 1); |
| 54 | + |
| 55 | + return getMedian(b + n/2, a, n - n/2); |
| 56 | +} |
| 57 | + |
| 58 | +int main() |
| 59 | +{ |
| 60 | + ios_base::sync_with_stdio(0); |
| 61 | + //cin.tie(0); |
| 62 | + |
| 63 | + int a[] = {1, 2, 3, 6}; |
| 64 | + int b[] = {4, 6, 8, 10}; |
| 65 | + int n = 4; |
| 66 | + |
| 67 | + cout << getMedian(a, b, n) << '\n'; |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
0 commit comments