璟雯院

珺璟如晔,雯华若锦

用户工具

站点工具


icpc:problems:luogup2920
problems
名称Time Management S
题目编号P2920
题目链接luogu.com.cn/…
来源USACO
算法分类二分答案
难易程度容易

Time Management S

想法

按照s进行排序,二分开始时间,贪心优先处理要快结束的任务。

代码实现

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=1010;
struct node{
	int t, s;
	friend bool operator<(const node a, const node b) {
		return a.s < b.s;
	}
};
node line[N];
int n;
bool check(int x) {
	int t = x;
	for(int i=1; i<=n; i++) {
		t += line[i].t;
		if(t > line[i].s)
			return false;
	}
	return true;
}
int main() {
	scanf("%d", &n);
	for(int i=1; i<=n; i++) 
		scanf("%d %d", &line[i].t, &line[i].s);
	sort(line+1, line+1+n);
	int l=0, r=line[1].s, mid;
	while(l < r) {
		mid = (l + r + 1) / 2;
		if(check(mid))
			l = mid;
		else
			r = mid - 1;
	}
	if(!check(0))
		printf("-1");
	else
		printf("%d", l);
	return 0;
}
/app/www/public/data/pages/icpc/problems/luogup2920.txt · 最后更改: 2024/01/29 11:15 由 温婕莺