[USACO08NOV]时间管理Time Management(排序,贪心)
题目描述
作为一名忙碌的商人,约翰知道必须高效地安排他的时间.他有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
输出格式:
* Line 1: The latest time Farmer John can start working or -1 if Farmer John cannot finish all the jobs on time.
说明
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.
思路:
一道大水题,然而我还是错了……
我们知道他的持续时间和结束时间,那么我们肯定要在结束之前完成所有(否则输出-1)
所以我们按照结束时间排序
因为题目让你求最晚什么时候开始,所以我们尽可能地将任务往后放
对应过来就是从最大的开始,时光倒流
用一个time指针维护上一个开始的时刻
如果time比当前一个的结束点早,那么当前一个的实际结束点就是time
反之实际结束点就是最晚结束点
o(n)扫一遍即可
加上排序时间复杂度总共是O(nlogn+n)
很优化
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define rii register int i
using namespace std;
struct deal{
int keep,start,last;
}x[];
int n,time;
bool cmp(deal ltt,deal kkk)
{
return ltt.last>kkk.last;
}
int main()
{
// freopen("manage.in","r",stdin);
// freopen("manage.out","w",stdout);
scanf("%d",&n);
for(rii=;i<=n;i++)
{
scanf("%d%d",&x[i].keep,&x[i].last);
x[i].start=x[i].last-x[i].keep;
}
sort(x+,x+n+,cmp);
time=x[].start;
for(rii=;i<=n;i++)
{
if(x[i].last<time)
{
time=x[i].start;
}
else
{
time=time-x[i].keep;
}
}
if(time<)
{
printf("-1");
return ;
}
printf("%d",time);
return ;
}
[USACO08NOV]时间管理Time Management(排序,贪心)的更多相关文章
- bzoj1620 / P2920 [USACO08NOV]时间管理Time Management
P2920 [USACO08NOV]时间管理Time Management 显然的贪心. 按deadline从大到小排序,然后依次填充时间. 最后时间为负的话那么就是无解 #include<io ...
- P2920 [USACO08NOV]时间管理Time Management
P2920 [USACO08NOV]时间管理Time Management 题目描述 Ever the maturing businessman, Farmer John realizes that ...
- 题解 P2920 【[USACO08NOV]时间管理Time Management】
题面 作为一名忙碌的商人,约翰知道必须高效地安排他的时间.他有N工作要 做,比如给奶牛挤奶,清洗牛棚,修理栅栏之类的. 为了高效,列出了所有工作的清单.第i分工作需要T_i单位的时间来完成,而 且必须 ...
- [USACO08NOV]时间管理Time Management
题目描述 Ever the maturing businessman, Farmer John realizes that he must manage his time effectively. H ...
- 洛谷 P2920 [USACO08NOV]时间管理Time Management
传送门 题目大意: 每个工作有截至时间和耗费时间,n个工作求最小开始时间. 题解: 贪心 从n-1安排,让结束时间尽量的晚. 注意:优先级 cout<<st<0?-1:st; (X ...
- [LUOGU] P2920 [USACO08NOV]时间管理Time Management
见7.3测试 #include<iostream> #include<algorithm> #include<cstdio> using namespace std ...
- CMSIS-RTOS 时间管理之时间延迟Time Delay
时间管理 Time Management 此RTOS除了可以把你的应用代码作为线程运行,它还可以提供一些时间服务功能,使用这些功能你就可以访问RTOS的一些系统调用. 时间延迟Time Delay 在 ...
- 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 时间管理( 二分答案 )
二分一下答案就好了... --------------------------------------------------------------------------------------- ...
随机推荐
- scss-&父选择器标识符
在使用选择器嵌套的时候有一种情况需要特别注意,先看一段scss代码实例: .text a { color: blue; :hover { color: red } } 也许写此段代码目的是为了将其编译 ...
- Swiper双向轮播
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- wxpython wx.windows的API
wx.Window is the base class for all windows and represents any visible object on screen. All control ...
- App后台开发架构实践笔记
1 App后台入门 1.1 App后台的功能 (1)远程存储数据: (2)消息中转. 1.2 App后台架构 架构设计的流程 (1) 根据App的设计,梳理出App的业务流程: (2) 把每个业务流程 ...
- IOS开发入门实例
关于如何创建第一个 iOS 应用 本篇“第一个 iOS 应用”教程将向你介绍 iOS 应用开发中的“三个T”: Tools(工具)如何利用 Xcode 创建和管理工程. Technologies(技术 ...
- 6.Zabbix 3.0 MySQL 监控
请查看我的有道云笔记: http://note.youdao.com/noteshare?id=94cd760a4848939d33892b7464cc0401&sub=8B6EDD27671 ...
- 配置xtrabackup备份mysql数据库
下载地址:https://www.percona.com/downloads/XtraBackup/LATEST/ 为了方便起见本次安装使用yum源安装方式 1 安装yum源:yum insta ...
- scrum 第四次冲刺
一.项目目的 为生活在长大的学生提供方快捷的生活服务,通过帖子发现自己志同道合的朋友,记录自己在长大点滴.本项目的意义在于锻炼团队的scrum能力,加强团队合作能力.确定本项目采用的技术方向:本项目整 ...
- SAP成都研究院许聚龙:Hello, Coresystems!
Jerry的前一篇文章<SAP成都研究院数字创新空间沟通S/4HANA和C/4HANA的智能服务演示视频和Coresystems分享预告>已经提到,接下来会由SAP成都研究院数字创新空间的 ...
- MySQL学习(三)函数
一.数学函数 绝对值函数ABS():ABS(X) 返回圆周率函数PI() 平方根函数SQRT() 求余函数MOD(X,Y) 获取整数函数CEIL(X),CEILING(X)返回不小于X的最小整数:FL ...