Description

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,问要完成所有工作,最迟要什么时候开始.

Input

* Line 1: A single integer: N

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

Output

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

Sample Input

4

3 5

8 14

5 20

1 16



INPUT DETAILS:



Farmer John has 4 jobs to do, which take 3, 8, 5, and 1 units of

time, respectively, and must be completed by time 5, 14, 20, and

16, respectively.


Sample Output

2



OUTPUT DETAILS:



Farmer John must start the first job at time 2. Then he can do

the second, fourth, and third jobs in that order to finish on time.

水题一道……

首先按deadline降序排一遍,保存一个当前的最大开始时间,那么当加入一个工作,要么保存答案的比这个工作的deadline大,要么后者大。无论如何一定要保证做完这个工作之后的时间比两者都大,这样才满足条件。所以两者取小的就行了。不会的自己再yy吧……

#include<cstdio>
#include<algorithm>
using namespace std;
struct work{
int s,t;
}a[1010];
int n,ans=10000000;
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline int min(int a,int b)
{return a<b?a:b;}
inline bool cmp(const work &a,const work &b)
{return a.s>b.s;}
int main()
{
n=read();
for (int i=1;i<=n;i++)
{
a[i].t=read();
a[i].s=read();
}
sort(a+1,a+n+1,cmp);
for (int i=1;i<=n;i++)
ans=min(ans,a[i].s)-a[i].t;
if (ans<0) ans=-1;
printf("%d",ans);
}

最新文章

  1. tab事件优化-事件代理
  2. css的初步了解
  3. python——threading模块
  4. Android 中的Json解析工具fastjson 、序列化、反序列化
  5. python一个注意的地方
  6. Javascript生成GUID
  7. windows 服务 安装 删除 启动 停止
  8. Ubuntu 升级内核
  9. Android Service 生命周期和使用注意项
  10. kvm虚拟化之克隆篇
  11. Substrings(hd1238)
  12. pyspark 写 logistic regression
  13. UVA 1343 The Rotation Game
  14. sql 数据库还原脚本 (kill链接+独占
  15. [js高手之路] html5 canvas动画教程 - 实时获取鼠标的当前坐标
  16. 每天学一点Docker(6)——镜像和DockerFile
  17. Java经典编程题50道之三
  18. gulp前端构建化工具,帮你搞定不同浏览器的兼容性写法问题
  19. javaScript 物体多形态改变加回调函数
  20. sdram 裸机程序

热门文章

  1. socket使用TCP协议时,send、recv函数解析以及TCP连接关闭的问题
  2. PHP 面向对象中常见关键字使用(final、static、const和instanceof)
  3. UBI(unsorted block image )块管理
  4. 用于防SQL注入的几个函数
  5. Java IO :文件
  6. Laravel-表单篇-controller
  7. 5狐网教你从零基础做Firefox os 手机应用开发赚money
  8. [Javascript] The JSON.stringify API
  9. Oracle442个应用场景------------基础应用场景
  10. getInitParameter()