BZOJ 1620 [Usaco2008 Nov]Time Management 时间管理:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1620
题意:
有n个工作,每一个工作完成需要花费的时间为tim[i],完成这项工作的截止日期为dead[i]。
问你在保证所有工作按时完成的前提下,最晚什么时候开始工作。
(每天从时刻0开始算。如果无论如何都完成不了,输出-1)
题解:
贪心。
先将所有工作按dead从大到小排序。
当前开始工作的时间为start(初始为INF)。
对于每个工作,start一定要满足两个条件:
start <= dead - tim (最后一刻才完成这项工作的开始时间)
start + tim <= 原来的start (这项工作做完了之后,才能开始别的工作)
所以start = min(start - tim, dead - tim)
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#define MAX_N 1005
#define INF 10000000 using namespace std; struct Work
{
int tim;
int dead;
Work(int _tim,int _dead)
{
tim=_tim;
dead=_dead;
}
Work(){}
friend bool operator < (const Work &a,const Work &b)
{
return a.dead>b.dead;
}
void read_work()
{
cin>>tim>>dead;
}
}; int n;
int start=INF;
Work work[MAX_N]; void read()
{
cin>>n;
for(int i=;i<n;i++)
{
work[i].read_work();
}
} void solve()
{
sort(work,work+n);
for(int i=;i<n;i++)
{
Work now=work[i];
start=min(start-now.tim,now.dead-now.tim);
}
} void print()
{
if(start>=) cout<<start<<endl;
else cout<<-<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 1620 [Usaco2008 Nov]Time Management 时间管理:贪心的更多相关文章
- BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )
二分一下答案就好了... --------------------------------------------------------------------------------------- ...
- BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理
Description Ever the maturing businessman, Farmer John realizes that he must manage his time effecti ...
- BZOJ——1620: [Usaco2008 Nov]Time Management 时间管理
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 920 Solved: 569[Submit][Status][Discuss] Description ...
- 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 时间管理(贪心)
http://www.lydsy.com/JudgeOnline/problem.php?id=1620 一开始想不通啊.. 其实很简单... 每个时间都有个完成时间,那么我们就从最大的 完成时间的开 ...
- bzoj1620 [Usaco2008 Nov]Time Management 时间管理
Description Ever the maturing businessman, Farmer John realizes that he must manage his time effecti ...
- Bzoj 1229: [USACO2008 Nov]toy 玩具 题解 三分+贪心
1229: [USACO2008 Nov]toy 玩具 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 338 Solved: 136[Submit] ...
- BZOJ 1229 [USACO2008 Nov]toy 玩具(三分+贪心)
[题木链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1229 [题目大意] 每天对玩具都有一定的需求ni,每天可以花f价值每条购买玩具, 当天 ...
随机推荐
- 邁向IT專家成功之路的三十則鐵律 鐵律二十六:IT人閱讀之道-慎選
IT人經常一整天工作回來早已用腦過度,此時收看什麼樣的電視節目,以及閱讀甚麼樣的書籍.聽什麼樣的音樂與有聲書最適合我們,讓我們可以在放鬆之餘,還能夠讓自己內在的心靈與外在的能力繼續成長呢? 身為IT工 ...
- Android 中状态栏、标题栏、View的大小及区分
1.获得状态栏的高度(状态栏相对Window的位置): Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisp ...
- 【层次查询】Hierarchical Queries之亲兄弟间的排序(ORDER SIBLINGS BY)
http://blog.itpub.net/519536/viewspace-624176 有关层次查询之前的文章参考如下. [层次查询]Hierarchical Queries之"树的遍历 ...
- 如何提高NodeJS程序的运行的稳定性
如何提高NodeJS程序运行的稳定性 我们常通过node app.js方式运行nodejs程序,但总有一些异常或错误导致程序运行停止退出.如何保证node程序的稳定运行? 下面是一些可以考虑的方案: ...
- Why is chkconfig no longer available in Ubuntu?
Question: I can not use chkconfig tools in Ubuntu 12.10 It's a very useful tools to configure the se ...
- How to set the initial value of a select element using AngularJS ng-options & track by
原文: https://www.gurustop.net/blog/2014/01/28/common-problems-and-solutions-when-using-select-element ...
- JavaScript校验输入的字符串是否包含特殊字符
校验在文本框输入的字符串中是否包含特殊字符串,js代码如下 function strInclude(substring){ if(substring){ var reg = new RegExp(&q ...
- vue2.0 自定义 饼状图 (Echarts)组件
1.自定义 图表 组件 Echarts.vue <!-- 自定义 echart 组件 --> <template> <div> <!-- echart表格 ...
- codefoeces B. Friends and Presents
B. Friends and Presents time limit per test 1 second memory limit per test 256 megabytes input stand ...
- Spring的Scheme位置
org.springframework.aop.config org.springframework.contex.config org.springframework.ejb.config org. ...