当前位置: 首页 > news >正文

【C++ 真题】P2920 [USACO08NOV] Time Management S

P2920 [USACO08NOV] Time Management S

题目描述

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 ( 1 ≤ N ≤ 1000 ) N(1\le N\le 1000) N(1N1000) 个工作要做,比如给奶牛挤奶,清洗牛棚,修理栅栏之类的。

为了高效,约翰列出了所有工作的清单。第 i ( 1 ≤ i ≤ N ) i(1\le i\le N) i(1iN) 个工作需要 T i ( 1 ≤ T i ≤ 1000 ) T_i(1\le T_i\le 1000) Ti(1Ti1000) 单位的时间来完成,而且必须在 1 ≤ S i ≤ 1 0 6 1\le S_i\le 10^6 1Si106 或之前完成。现在是 0 0 0 时刻。约翰做一份工作必须直到做完才能停止。

所有的商人都喜欢睡懒觉。请帮约翰计算他最迟什么时候开始工作,可以让所有工作按时完成。(如果始终无法完成全部任务,输出 − 1 -1 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

输入 #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 "bits/stdc++.h"
using namespace std;
const int N = 1e6+7;
int n, a, b;
int g[N], sum = 1, ans, mid;
struct work{int t, T;
};
work que[N];
bool cmp(work a, work b){return a.T > b.T;
}
int main() {cin>>n;for(int i=1;i<=n;++i){cin>>que[i].t>>que[i].T;}sort(que+1, que+n+1, cmp);int less = que[1].T - que[1].t;for(int i=2;i<=n;++i){if(que[i].T <= less){less = que[i].T - que[i].t;}else if(que[i].T > less){less = less - que[i].t;}}if(less<0){cout<<"-1"<<endl;}else{cout<<less<<endl;}return 0;
}
http://www.lryc.cn/news/536148.html

相关文章:

  • pip安装指定版本的包
  • 【pytest】获取所有用例名称并存于数据库
  • Java中原子操作的实现原理
  • 25农村发展研究生复试面试问题汇总 农村发展专业知识问题很全! 农村发展复试全流程攻略 农村发展考研复试真题汇总
  • 一维前缀和与二维前缀和
  • 3×2 MIMO系统和2×2 MIMO系统对比
  • 【MySQL — 数据库基础】深入解析 MySQL 的联合查询
  • 【医院运营统计专题】3.解码医院运营统计:目标、原则与未来蓝图
  • Ubuntu 下 nginx-1.24.0 源码分析 - ngx_atomic_cmp_set 函数
  • CNN-BiLSTM卷积神经网络双向长短期记忆神经网络多变量多步预测,光伏功率预测
  • 【YOLO系列】YOLOv5 NMS源码理解、更换为DIoU-NMS
  • Android RenderEffect对Bitmap高斯模糊(毛玻璃),Kotlin(1)
  • 【linux学习指南】线程同步与互斥
  • JavaScript函数与方法详解
  • 【论文笔记】ZeroGS:扩展Spann3R+GS+pose估计
  • AtCoder - arc058_d Iroha Loves Strings解答与注意事项
  • 企业使用统一终端管理(UEM)工具提高端点安全性
  • Leetcode 算法题 9 回文数
  • 设计模式Python版 命令模式(上)
  • C语言之循环结构:直到型循环
  • 细说STM32F407单片机RTC的备份寄存器原理及使用方法
  • MATLAB计算反映热需求和能源消耗的度数日指标(HDD+CDD)(全代码)
  • J6 X8B/X3C切换HDR各帧图像
  • 09-轮转数组
  • 用vue3写一个好看的wiki前端页面
  • 瑞芯微烧写工具
  • 说下JVM中一次完整的GC流程?
  • Open FPV VTX开源之OSD使用分类
  • 智慧农业-虫害及生长预测
  • Python 识别图片和扫描PDF中的文字