这个题不难,但我是蒟蒻。。。

先看题目

题目:

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工作要 做,比如给奶牛挤奶,清洗牛棚,修理栅栏之类的.

为了高效,列出了所有工作的清单.第i分工作需要T_i单位的时间来完成,而 且必须在S_i或之前完成.现在是0时刻.约翰做一份工作必须直到做完才能停 止.

所有的商人都喜欢睡懒觉.请帮约翰计算他最迟什么时候开始工作,可以让所有工作按时完成.(如果无法完成全部任务,输出-1)

输入格式

* Line 1: A single integer: N

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

(翻译:* 第1行: 单个整数: n

* 第2行. n + 1行: 第 i + 1行包含两个空格分隔的整数: t _ i 和 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)

输入输出样例

输入 #1复制

4

3 5

8 14

5 20

1 16

输出 #1复制

2

说明/提示

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.

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.

(翻译:约翰有4个工作要做,分别需要3、8、5和1个时间单位,必须分别在5、14、20和16个时间段内完成。那么他就可以按照这个顺序做第二、第四、第三项工作,以便按时完成。)

思路:

只要倒着遍历模拟就行了,比较类似P1843奶牛晒衣服

代码:

(本蒟蒻用c++,python的可以走了)

#include<bits/stdc++.h>//万能头
using namespace std;
struct Work{int ut,lt;
}a[1010];
bool cmp(Work x,Work y){return x.lt<y.lt || x.lt==y.lt&&x.ut<y.ut;//比较大小
}
int main(){int n;cin>>n;for(int i=1;i<=n;i++){cin>>a[i].ut>>a[i].lt;}sort(a+1,a+n+1,cmp);int zwsj=a[n].lt-a[n].ut;//定义初始值for(int i=n-1;i>=1;i--){//模拟if(zwsj>a[i].lt){zwsj=a[i].lt;}zwsj-=a[i].ut;}if(zwsj>-1){cout<<zwsj;}else{cout<<-1;}
}

P2920题解【[USACO08NOV]时间管理Time Management】相关推荐

  1. 题解 P2920 【[USACO08NOV]时间管理Time Management】

    好了,废话不多说,我们切入正题,首先,不懂得分治的可以去看这位大佬的文章, 这道题是让我们求最晚可以在什么时间起床,这里我们需要加入一个小小的贪心,就是结束时间短的放前面处理,至于为什么,相信你肯定能 ...

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

    时间管理Time Management 二分枚举开始时间. #include <iostream> #include <cstdio> #include <algorit ...

  3. P2920 [USACO08NOV]时间管理Time Management

    题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. H ...

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

    emm贪心题,但不知道怎么让我搞成了并查集 先将数组按结束时间排序,因为肯定先安排靠后的工作,后面处理时冲突会减小很多 然后如何并查集乱搞呢? 假如下图是一个没有加入任务的时间线{{20,5},{15 ...

  5. LUOGU P2920 [USACO08NOV]时间管理Time Management

    题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. H ...

  6. [USACO08NOV]时间管理Time Management

    题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. H ...

  7. [BZOJ] 1620: [Usaco2008 Nov]Time Management 时间管理

    1620: [Usaco2008 Nov]Time Management 时间管理 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 850  Solved ...

  8. bzoj 1620: [Usaco2008 Nov]Time Management 时间管理(贪心)

    1620: [Usaco2008 Nov]Time Management 时间管理 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 834  Solved ...

  9. Time Management 时间管理

    Time Management 时间管理 世界上最快而又最慢,最长而又最短,最平凡而又最珍贵,最易被忽视而又最令人后悔的是什么? ------时间 ------接下来敢不敢完成挑战测试:你对时间的感知 ...

最新文章

  1. Linux系统详解 系统的启动、登录、注销与开关机
  2. 复制表数据和结构的方法
  3. 双流棠湖中学怎么样_棠湖中学教师团队荣获四川省“最美教师团队”!
  4. MySql采用range分区可提升查询效率
  5. Redis 持久化策略 : RDB持久化、AOF持久化、混合持久化
  6. php 工资 2018,佛山市2018年平均工资(社平工资)
  7. shiro java配置,InI 配置 - 跟我学 Apache Shiro_教程_Java开发社区
  8. leetcode256. 粉刷房子
  9. Facebook升级到MySQL 8.0付出的代价
  10. 2017.9.2 最大半联通子图 思考记录
  11. pat1057 stack
  12. 移动通信原理学习笔记之一
  13. 使用python根据图片链接下载图片
  14. android怎么硬解锁,手机怎么强制解锁
  15. CTF挑战赛-合天网安实验室
  16. OEM,ODM,OBM,JDM概念
  17. svg中 path标签的d属性
  18. 美食小吃加盟网站源码餐饮奶茶招商加盟类网站
  19. 编译支持JPEG2000压缩格式的GDAL(ECW库、OpenJPEG)
  20. 光纤收发器工作原理及技术详解

热门文章

  1. 【信息系统项目管理师】复盘立项整体范围进度记忆敲出Part1
  2. 简单的Nginx代理配置
  3. 软考中级科目备考详细资料汇总
  4. ipad怎样分屏_【工具】ZotExpert:实现iPad文献管理的无缝同步
  5. HTML怎么去除描边,css中文字如何描边?
  6. List的浅拷贝和深拷贝
  7. Linux-输出重定向命令
  8. 快捷键大全之IE浏览器快捷键大全
  9. 互联网晚报 |12/7星期三| 荣耀回应借壳上市传闻;三亚机票酒店预订暴涨3倍;12月6日起,进入首都机场航站楼可不查验核酸证明...
  10. 【故障处理基础】0-12.4.4