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

news/2024/10/23 12:25:16/

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).

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时刻.约翰做一份工作必须直到做完才能停 止.

所有的商人都喜欢睡懒觉.请帮约翰计算他最迟什么时候开始工作,可以让所有工作按时完 成.

输入输出格式

输入格式:

 

  • 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.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
struct nond{int v,f;
}work[1005];
int n,ans;
bool cmp(nond x,nond y){return x.f>y.f;
}
int main(){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&work[i].v,&work[i].f);sort(work+1,work+1+n,cmp);ans=work[1].f;for(int i=1;i<=n;i++){if(ans<=work[i].f)    ans-=work[i].v;else    ans=work[i].f-work[i].v;}if(ans<0)    printf ("-1\n");else    printf ("%d\n",ans);return 0;
}

 

 

转载于:https://www.cnblogs.com/cangT-Tlan/p/7678379.html


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

相关文章

Spark GraphX 聚合操作

package Spark_GraphXimport org.apache.spark.{SparkConf, SparkContext} import org.apache.spark.graphx._ import org.apache.spark.graphx.util.GraphGenerators/*** 计算每一个用户的追随者数量和追随者的平均年龄*/ object Graphx_聚合操作 {def main(args: Array[Strin…

[二分答案] P2920 Time Management

推导贪心条件 排序 二分 输出 //#pragma GCC optimize(2) #include <cstdio> #include <iostream> #include <cstdlib> #include <cmath> #include <cctype> #include <string> #include <cstring> #include <algorithm> #…

Spring Boot 中自定义数据校验注解

Spring Boot 中自定义数据校验注解 在 Spring Boot 中&#xff0c;我们可以使用 JSR-303 数据校验规范来校验表单数据的合法性。JSR-303 提供了一些常用的数据校验注解&#xff0c;例如 NotNull、NotBlank、Size 等。但是&#xff0c;在实际开发中&#xff0c;我们可能需要自定…

【莫比乌斯反演】BZOJ2920-YY的GCD

【题目大意】 给定N, M,求1<x<N, 1<y<M且gcd(x, y)为质数的(x, y)有多少对。 【思路】 太神了这道题……蒟蒻只能放放题解&#xff1a;戳&#xff0c;明早再过来看看还会不会推导过程…… 实用的结论&#xff1a; 嗯…… /***************************************…

2920集五福_支付宝集五福攻略 ▏顺便学点营销活动传播套路

到今天为止&#xff0c;你一共收齐了几张福卡了呢&#xff1f; 本月5号&#xff0c;支付宝在微信公众号里发了《新年俗 集五福》的推文&#xff0c;“2月6日0点&#xff0c;支付宝里见&#xff0c;有你们就有福”。 同时&#xff0c;一名叫“青春、已不在”的网友立马在下方评论…

2920X

演讲不是讲自己的话&#xff0c;而是将听众的话&#xff0c;还记得那些演讲失败者&#xff0c;离开舞台的最后一段话&#xff0c;他们的感言就是我们失败的原因历史总是惊人的相似举例子是为了增加信服性历史是如此的悠久&#xff0c;还有什么故事不能符合我任何贪婪的意愿我的…

java多线程简明笔记(5)线程礼让 yield

关键字&#xff1a;yield 官方文档就不说了&#xff0c;简单理解&#xff0c;礼让 线程礼让 yield正在执行的线程暂停&#xff0c;不阻塞 示例代码&#xff1a; public class ThreadTest7 implements Runnable{public static void main(String[] args) {ThreadTest7 tnew Th…

雅思词汇真经单词共3672个

雅思词汇真经 / Vocabulary for IELTS / 学为贵 赢未来 / 英语真经派学习法 一本书精通雅思词汇 / 刘洪波 编著 / 涵盖&#xff1a;雅思必备核心词汇刘洪波老师原创雅思考点词库 逻辑词群记忆法&#xff0c;一群一群记单词&#xff0c;快速备考无负责 时尚插图&#xff0c;趣味…