二分 51Nod1105 第K大的数

传送门:点击打开链接

题意:n<=50000,再给你两个数组A,B,C数组是A[i]*B[j],求第k大

思路:二分。主要想说,,原来在重复的数的有序数组中二分某个数,只要算一次lower_bound就行了- -。。。。

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int>PII;

const int MX = 5e4 + 5;
const int INF = 0x3f3f3f3f;

int n, k;
LL A[MX], B[MX];

LL check(LL x) {
    LL sum = 0;
    for(int i = n; i >= 1; i--) {
        int t = lower_bound(B + 1, B + 1 + n, (x - 1) / A[i] + 1) - B;
        sum += n - t + 1;
        if(sum == 0) break;
    }
    return sum;
}

void solve() {
    LL l = 1, r = A[n] * B[n], m;
    while(l <= r) {
        m = (l + r) >> 1;
        LL f = check(m);
        if(f < k) r = m - 1;
        else l = m + 1;
    }
    printf("%I64d\n", l - 1);
}

int main() {
    //FIN;
    while(~scanf("%d%d", &n, &k)) {
        for(int i = 1; i <= n; i++) {
            scanf("%I64d%I64d", &A[i], &B[i]);
        }
        sort(A + 1, A + 1 + n);
        sort(B + 1, B + 1 + n);
        solve();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值