USACO2008 Time Management /// 贪心 oj24386
题目大意:
有N个工作被编号为1..N (1 ≤ N ≤ 1,000)
完成第i个工作需要T_i (1 ≤ T_i ≤ 1,000)的时间
第i个工作需在S_i (1 ≤ S_i ≤ 1,000,000)前结束
若能按时完成则输出 最晚开始工作的时间 若不能则输出 -1
* 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.
4
3 5
8 14
5 20
1 16
2
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.
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.
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
struct job
{
int s,e;
}a[];
bool cmp(struct job a,struct job b)
{
return a.e<b.e;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d",&a[i].s,&a[i].e);
sort(a+,a++n,cmp); /// 按最晚结束时间排序
int ans=INF;
for(int i=n;i>=;i--) /// 从最晚结束的事件开始遍历
ans=min(ans,a[i].e)-a[i].s;
/* 最晚开始时间与前一件事的最晚结束时间取更早的一个
最终推出第一件事的最晚开始时间
若无法按时完成 则时间会被推到0之前 也就是ans<0 */
if(ans<) printf("-1\n");
else printf("%d\n",ans);
return ;
}
USACO2008 Time Management /// 贪心 oj24386的更多相关文章
- USACO 刷题记录bzoj
bzoj 1606: [Usaco2008 Dec]Hay For Sale 购买干草——背包 #include<cstdio> #include<cstring> #incl ...
- BZOJ 1620 [Usaco2008 Nov]Time Management 时间管理:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1620 题意: 有n个工作,每一个工作完成需要花费的时间为tim[i],完成这项工作的截止日 ...
- 【BZOJ】1620: [Usaco2008 Nov]Time Management 时间管理(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1620 一开始想不通啊.. 其实很简单... 每个时间都有个完成时间,那么我们就从最大的 完成时间的开 ...
- bzoj 1620: [Usaco2008 Nov]Time Management 时间管理【贪心】
按s从大到小排序,逆推时间模拟工作 #include<iostream> #include<cstdio> #include<algorithm> using na ...
- 1620: [Usaco2008 Nov]Time Management 时间管理
1620: [Usaco2008 Nov]Time Management 时间管理 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 506 Solved: ...
- BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )
二分一下答案就好了... --------------------------------------------------------------------------------------- ...
- BZOJ1229 USACO2008 Nov toy 玩具 【三分+贪心】*
BZOJ1229 USACO2008 Nov toy 玩具 Description 玩具 [Chen Hu, 2006] Bessie的生日快到了, 她希望用D (1 <= D <= 10 ...
- BZOJ_1229_[USACO2008 Nov]toy 玩具_三分+贪心
BZOJ_1229_[USACO2008 Nov]toy 玩具_三分+贪心 Description 玩具 [Chen Hu, 2006] Bessie的生日快到了, 她希望用D (1 <= D ...
- Bzoj 1229: [USACO2008 Nov]toy 玩具 题解 三分+贪心
1229: [USACO2008 Nov]toy 玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 338 Solved: 136[Submit] ...
随机推荐
- “今日头条杯”首届湖北省大学程序设计竞赛--F. Flower Road
题目链接:点这 github链接:(包含数据和代码,题解):点这 链接:https://www.nowcoder.com/acm/contest/104/E来源:牛客网 题目描述 (受限于评测机,此题 ...
- SDNU 1217 CD收藏——并查集
Description lmh平常爱听歌,所以买了很多的CD来收藏,但是因为平常整理不当,所以忘记了这些CD的歌手是谁.现在他想知道他到底收藏了多少位歌手的专辑,于是他想了一个办法,同时拿出两 ...
- 用scala 实现top N 排名
object TopNApp { def main (args: Array[String]) { if (args != 3) { System.err.println("usage: & ...
- Java中的数据结构有哪些?HashMap的工作原理是什么?
Java中常用数据结构 常用的数据结构有哈希表,线性表,链表,java.util包中有三个重要的接口:List,Set,Map常用来实现基本的数据结构 HashMap的工作原理 HashMap基于ha ...
- wangEditor 图片上传失败提示
wangEditor 官网自定义上传事件:https://www.kancloud.cn/wangfupeng/wangeditor2/123689 声明:我用的wangEditor版本是2.1.23 ...
- 2018-8-10-win10-uwp-气泡
title author date CreateTime categories win10 uwp 气泡 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 17: ...
- ulimit - 获取和改变用户的限制设定
大纲 #include <ulimit.h> long ulimit(int cmd, long newlimit); 描述 警告: 这个函数已经被废弃. glibc 不再提供这个包含文件 ...
- C#获取本月开始日期和结束日期
DateTime dt = DateTime.Now; //本月第一天时间 DateTime dt_First = dt.AddDays( - (dt.Day)); //获得某年某月的天数 int y ...
- BCZM : 1.16
24点游戏 解法一:穷举法 解法二:分治法
- WindowsPowerShell常用命令
zai 获得Shell权限之后,可使用如下命令对系统进行文件操作: cd 后跟相应参数: cd ../ 返回上一级目录 cd +路径 跳转至制定目录(如果路径存在且正确的话) type flag.tx ...