洛谷 P2920 [USACO08NOV]时间管理Time Management

时间管理Time Management

二分枚举开始时间。

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
//Mystery_Sky
//
#define ll long long
#define M 10001000
#define INF 0x7f7f7f7f
ll l, r, mid;
int n;
struct node{
    int t, s;
}t[M];

inline bool cmp(node a, node b)
{
    return a.s < b.s;
}

inline bool check(ll ans)
{
    ll time = ans, cnt = 0;
    for(int i = 1; i <= n; i++) {
        ans += t[i].t;
        if(ans <= t[i].s) cnt++;
        else break;
    }
    return cnt == n;
}

int main() {
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d%d", &t[i].t, &t[i].s);
    sort(t+1, t+1+n, cmp);
    l = 0, r = t[n].s;
    if(!check(0)) {
        printf("-1\n");
        return 0;
    }
    while(l < r) {
        mid = (l + r + 1)/2;
        if(check(mid)) l = mid;
        else r = mid - 1;
    }
    printf("%lld\n", l);
    return 0;
} 

转载于:https://www.cnblogs.com/Benjamin-cpp/p/10877797.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值