洛谷 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;
}
唯愿,青春不辜负梦想,未来星辰闪耀