列表

详情


NC210102. 时间管理

描述

Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. He has N jobs conveniently numbered 1..N (1 <= N <= 1,000) to accomplish (like milking the cows, cleaning the barn, mending the fences, and so on). To manage his time effectively, he has created a list of the jobs that must be finished. Job i requires a certain amount of time T_i (1 <= T_i <= 1,000) to complete and furthermore must be finished by time S_i (1 <= S_i <= 1,000,000). Farmer John starts his day at time t=0 and can only work on one job at a time until it is finished. Even a maturing businessman likes to sleep late; help Farmer John determine the latest he can start working and still finish all the jobs on time.

N个工作,每个工作其所需时间,及完成的Deadline,问要完成所有工作,最迟要什么时候开始.

输入描述

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains two space-separated integers: T_i and S_i

输出描述

* Line 1: The latest time Farmer John can start working or -1 if Farmer John cannot finish all the jobs on time.

示例1

输入:

4
3 5
8 14
5 20
1 16

输出:

2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++ 11.0.1) 解法, 执行用时: 3ms, 内存消耗: 388K, 提交时间: 2022-08-10 20:26:21

#include<bits/stdc++.h>
using namespace std;
struct sj
{
	int s;
	int j;
}a[1010];
bool cmp(sj a,sj b)
{
	return a.j<b.j;
}
int main()
{
	int n,l=0,r,mid,i,c,f,da=-1;
	cin>>n;
	for(i=1;i<=n;i++)
		cin>>a[i].s>>a[i].j;
	sort(a+1,a+n+1,cmp);
	r=a[n].j;
	while(l<=r)
	{
		mid=l+(r-l)/2,c=mid,f=1;
		for(i=1;i<=n;i++)
		{
			if(c+a[i].s>a[i].j)
			{
				f=0;
				break;
			}
			c+=a[i].s;
		}
		if(f==1)
		{
			da=mid;
			l=mid+1;
		}
		else
			r=mid-1;
	}
	cout<<da;
	return 0;
}

上一题