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] ...
随机推荐
- QtConcurrent::run() 的使用
QFuture<T>run(constClass *object,T(Class::*fn)(Param1,Param2,Param3,Param4,Param5)const,constA ...
- php 常用加密函数
md5()md5()默认情况下以 32 字符十六进制数字形式返回散列值,它接受两个参数,第一个为要加密的字符串,第二个为raw_output的布尔值,默认为false,如果设置为true,md5()则 ...
- I/O复用select 使用简介
一:五种I/O模型区分: 1.阻塞I/O模型 最流行的I/O模型是阻塞I/O模型,缺省情形下,所有套接口都是阻塞的.我们以数据报套接口为例来讲解此模型(我们使用UDP而不是TCP作为例子的原 ...
- SonarQube搭建和使用教程
我想使用 SonarQube 查阅代码 请问怎么做,现在只有一个要审查代码的项目
- jQuery 事件委派
/******************************************************************/ $(function(){ //live()事件委派,后续添加 ...
- Redis启动Sentinel出现警告的解决
Redis 3.0.7版本启动时出现警告的解决办法 7283:M 12 Mar 12:13:33.749 # WARNING: The TCP backlog setting of 511 canno ...
- Naive RNN vs LSTM vs GRU、attention基础
原文地址:https://www.jianshu.com/p/b8653f8b5b2b 一.Recurrent Neural Network 二.Naive RNN Naive RNN更新参数时易出现 ...
- vue-router 路由配置
前提: 项目由 vue-cli 脚手架创建. 首先,先下载 vue-router npm install vue-router 安装完成后,运行项目 npm run dev 打开 main.js , ...
- Java 设计模式之 装饰者模式
装饰者模式(Decorator Pattern): 概述:装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象 特点: (1) ...
- JDK8新特性之Stream流
是什么是Stream流 java.util.stream.Stream Stream流和传统的IO流,它们都叫流,却是两个完全不一样的概念和东西. 流可以简单的说是处理数据集合的东西,可以申明式流式A ...