题目描述

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

输出格式:

  • Line 1: The latest time Farmer John can start working or -1 if Farmer John cannot finish all the jobs on time.

输入输出样例
输入样例#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.

解题思路

考试题,二分答案斜挂。。。我真的是个人才, 发现必须要有ans,不能直接输出l,否则会有些奇奇怪怪的错误。我的做法是将原数列按S排序,然后二分答案,时间复杂度O(nlogn),100000还是可以过的。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>using namespace std;
const int MAXN = 100005;inline int rd(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;
}int n,ans=0;struct Time{int st,ed;
}t[MAXN];inline bool cmp(Time a,Time b){return a.ed<b.ed;
}inline bool check(int x){int k=x;for(register int i=1;i<=n;i++){k+=t[i].st;if(k>t[i].ed) return false;}return true;
}int main(){
//  freopen("manage.in","r",stdin);
//  freopen("manage.out","w",stdout);n=rd();for(register int i=1;i<=n;i++)t[i].st=rd(),t[i].ed=rd();sort(t+1,t+1+n,cmp);int l=0,r=t[1].ed-t[1].st;
//  for(register int i=1;i<=n;i++) cout<<t[i].st<<" "<<t[i].ed<<endl;if(r<0) {puts("-1");return 0;}while(l<=r){int mid=(l+r)>>1;
//      cout<<l<<" "<<r<<" ";
//      cout<<mid<<endl;if(check(mid)) {l=mid+1;ans=mid;}else r=mid-1;}if(ans==0 && !check(0)) puts("-1");else printf("%d",ans);return 0;
}/*
4
3 5
8 14
5 20
1 16
*//*
3
1 100
100 105
110 300
*/

LUOGU P2920 [USACO08NOV]时间管理Time Management相关推荐

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

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

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

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

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

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

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

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

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

    这个题不难,但我是蒟蒻... 先看题目 题目: Ever the maturing businessman, Farmer John realizes that he must manage his ...

  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. 后台开发之IO缓冲区管理
  2. 5. 连续时间马氏过程-强Markov族
  3. 广播多路访问链路上的OSPF
  4. 羽毛球 机器人 Robocon 2015 泰国预选赛(全国大学生机器人竞赛)
  5. YooQ与《Charlotte》
  6. Oracle修改表空间为自动扩展
  7. 测试思路系列:《谷歌的软件测试之道》读书笔记
  8. JIRA /mnt/server/atlassian-jira-6.3.6-standalone/bin/start-jira.sh
  9. 面试必会 InnoDB的多版本并发控制(MVCC)
  10. 国内首款安全软件开源,金山卫士源码共享
  11. Command ‘docker‘ not found, but can be installed with:
  12. PHP课程设计《PHP网上购物系统的设计与实现》
  13. mini programe
  14. 商业计划书(BP)应该包含哪些点?看 BP 的人最想从中得到什么?
  15. 计算机系统存储器 分类,存储器的分类
  16. python写入文件到docx_从python中的txt文件写入docx文件
  17. 解决uniapp从首页列表进入详情页,再返回首页时滚动条在顶部的问题
  18. elementUI Tree组件实现双击事件
  19. Django的语言模板
  20. python-InputWhile基础知识

热门文章

  1. QRCode.js 生成二维码详细API和参数
  2. Android Sqlite 简单SQL语句
  3. 基于jQuery实现的 “滑动门”
  4. 携职教育:软考哪个科目更容易过?70%考生都选这几个科目
  5. 重磅发布 | 黑镜调查:深渊背后的真相之「DDoS 威胁与黑灰产业调查报告」
  6. c# 收取邮件 解析_c# 使用NetStream接收 解析邮件
  7. android设备登录过微信,安卓系统手机查询近来登录过的微信号?
  8. 港联证券|熊市牛市出现的原因?出现时应该怎么办?
  9. codesys 获取系统时间
  10. BZOJ P3609 [Heoi2014]人人尽说江南好