码迷,mamicode.com
首页 > 其他好文 > 详细

PAT (Advanced Level) Practice-1029 Median

时间:2020-07-23 23:22:49      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:return   const   get   mes   div   code   kth   ++   style   

二分查找

#include<bits/stdc++.h>
using namespace std;

const int maxn = 2e5+5;
long long a[maxn], b[maxn];

long long getKth(int k, int lo1, int hi1, int lo2, int hi2) {
    if (lo1 == hi1) return b[lo2+k-1];
    if (lo2 == hi2) return a[lo1+k-1];
    if (k == 1) return min(a[lo1], b[lo2]);
    int mi1 = min(hi1, lo1 + (k >> 1)), mi2 = min(hi2, lo2 + (k >> 1));
    if (a[mi1-1] < b[mi2-1]) return getKth(k-mi1+lo1, mi1, hi1, lo2, hi2);
    else return getKth(k-mi2+lo2, lo1, hi1, mi2, hi2);
}
int main() {
    int na, nb;
    scanf("%d", &na); for (int i = 0; i < na; ++i) scanf("%lld", a + i);
    scanf("%d", &nb); for (int i = 0; i < nb; ++i) scanf("%lld", b + i);
    if ((na + nb) & 1) printf("%lld\n", getKth((na+nb)/2+1, 0, na, 0, nb));
    else printf("%lld\n", getKth((na+nb)/2, 0, na, 0, nb));
    return 0;
}

 

PAT (Advanced Level) Practice-1029 Median

标签:return   const   get   mes   div   code   kth   ++   style   

原文地址:https://www.cnblogs.com/dululu/p/13368600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!