Chri_K

第k小(二分)
#include<iostream>
using namespace std;
#define N 90000
#define ll long long
#define inf 1000000000
ll s[N],t[N];
int n,k;
bool check(ll x)
{
    int tp = 0;
    for(int i = 1;i <= n;i++)
    {
        if(x >= s[i] && x <= t[i]) tp += (x - s[i]);
        else if(x > t[i]) tp += (t[i] - s[i] + 1);
    }
    return tp <= k;
}
int main()
{
       cin>>n;
    ll l = inf + 10,r = -inf - 10;
    for(int i = 1;i <= n;i++)
    {
        cin>>s[i]>>t[i];
        l = min(s[i],l),r = max(r,t[i]);
    }
    cin>>k;
    while(l <= r)
    {
        ll mid = (l + r) >> 1;
        if(check(mid)) l = mid + 1;
        else r = mid - 1;
    }
    cout<<r;
}
//3
//5 9 
//1 6 
//7 10 
//11

 

posted on 2020-10-17 12:25  Chri_K  阅读(71)  评论(0编辑  收藏  举报