Luogu P2920 时间管理【二分答案】

news/2024/10/23 8:32:23/

二分答案水题。

(像我这么蒻的人都能十几分钟A掉)

https://www.luogu.org/problemnew/show/P2920

 

开始时间一定在从0到min(t[i]-s[i])的一段区间上,因此我们可以愉快地二分答案。

在二分答案之前,我们贪心地把结束时间从小到大排一遍序,于是就可以二分了。

最后如果结果是负数,证明无解,不能完成任务。输出-1。

Check函数里,模拟累加一下当前的时间加上最靠近ddl任务所花的时间,看能不能完成,进行判断。

code

 1 #include<cstdio>
 2 #include<algorithm>
 3 #define inf 0x3f3f3f3f
 4 
 5 using namespace std;
 6 int n,ans;
 7 int l,r=inf;
 8 struct task{
 9     int t,s;
10 }a[2000];
11 bool cmp(task x,task y)
12 {
13     return x.s<y.s;
14 }
15 bool check(int x)
16 {
17     int now=x;
18     for(int i=1;i<=n;i++)
19     {
20          if(now+a[i].t<=a[i].s) now+=a[i].t;
21          else return false;
22     }
23     return true; 
24 }
25 
26 int main()
27 {
28     scanf("%d",&n);
29     for(int i=1;i<=n;i++)
30     {
31         scanf("%d%d",&a[i].t,&a[i].s);
32         r=min(r,a[i].s-a[i].t);
33     }
34     sort(a+1,a+1+n,cmp);
35     while(l<r)
36     {
37         int mid=(l+r+1)>>1;
38         if(check(mid)) l=mid;
39         else r=mid-1;
40     }
41     ans=l ? l : -1;
42     printf("%d",ans);
43     return 0;
44 }
View Code

 

转载于:https://www.cnblogs.com/nopartyfoucaodong/p/9330073.html


http://www.ppmy.cn/news/505743.html

相关文章

P2920 [USACO08NOV]时间管理Time Management

https://www.luogu.org/problemnew/show/P2920 #include <ctime> #include <cmath> #include <cstdio> #include <string> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm>//头文件准备 us…

eclipse导入tomcat 8.0x源码

1、安装Ant Ant下载地址&#xff1a;http://ant.apache.org/bindownload.cgi 下载完成以后&#xff0c;解压到相应目录&#xff0c;例如我解压到了D:\open-soft\apache-ant-1.9.6文件夹 然后配置Ant的环境变量&#xff0c;增加 ANT_HOME 为D:\open-soft\apache-ant-1.9.6\ 然后…

poj 2920 Mine Map【BFS】

&#xfeff;&#xfeff; Mine Map Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 1023 Accepted: 493 题目大意&#xff1a; 给一个n*n的金库&#xff0c;金库中有地雷‘*’和空格?&#xff0c;现在你从金库中间出发&#xff0c;刚刚开始时&#xff0c;【遍历…

LUOGU P2920 [USACO08NOV]时间管理Time Management

题目描述 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). …

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

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

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

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

CenOS 7.x的高级管理工具systemd介绍

文章目录 一、系统启动流程回顾二、CentOS 5.x,6.x,7.x的init程序区别三、systemd工具介绍3.1、systemd工具概述3.2、systemctl工具概述3.3、service以及chkconfig工具使用回顾3.4、systemd中的unit介绍3.5、systemctl工具实现service和chkconfig的功能3.6、systemctl实现运行级…

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

这个题不难&#xff0c;但我是蒟蒻。。。 先看题目 题目&#xff1a; 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 co…