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

Tasks and Deadlines(Sorting and Searching)

题目描述

You have to process n tasks. Each task has a duration and a deadline, and you will process the tasks in some order one after another. Your reward for a task is d-f where d is its deadline and f is your finishing time. (The starting time is 0, and you have to process all tasks even if a task would yield negative reward.)
What is your maximum reward if you act optimally?

输入

The first input line has an integer n(1 ≤ n ≤ 2\times10^5): the number of tasks.
After this, there are n lines that describe the tasks. Each line has two integers a and d(1 ≤ a,d ≤ 10^6): the duration and deadline of the task.

输出

Print one integer: the maximum reward.

样例输入
3
6 10
8 15
5 12
样例输出
2
思路分析

贪心、排序。

本题排序原则可以用数学表达式解释。

设起始时间T。

对于任务a和任务b,先a后b的情况下reward为a.deadline-T+b.deadline-(T+a.duration);

先b后a的情况下reward为b.deadline-T+a.deadline-(T+b.duration)。

化简约分下来发现,如果b.duration>a.duration,则a排在b前面。

代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,a,d,ans;
struct node{ll duration,deadline;
};
vector<node>task;
bool cmp(node&a,node&b){return b.duration>a.duration;
}
int main(){ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);cin>>n;for(int i=0;i<n;i++){cin>>a>>d;task.push_back({a,d});}sort(task.begin(),task.end(),cmp);ll t=0;for(node i:task){t+=i.duration;ans+=i.deadline-t;}cout<<ans;return 0;
}

http://www.lryc.cn/news/610691.html

相关文章:

  • 云手机和实体手机之间的区别
  • 【springcloud的配置文件不生效】
  • AI的第一次亲密接触——你的手机相册如何认出你的猫?
  • 深入浅出 RabbitMQ-交换机详解与发布订阅模型实战
  • 华为云云产品的发展趋势:技术创新驱动数字化未来
  • 查看部署在K8S服务的资源使用情况
  • 蓝桥杯----DS1302实时时钟
  • Could not load the Qt platform plugin “xcb“ in “无法调试与显示Opencv
  • 【升级打怪实录】uniapp - android 静态声明权限和动态请求权限的区别
  • AI+OA原生应用 麦当秀AIPPT
  • 用 PyTorch 实现一个简单的神经网络:从数据到预测
  • lesson32:Pygame模块详解:从入门到实战的2D游戏开发指南
  • 阿里云招Java研发咯
  • day 46 神经网络-简版
  • 从零用java实现小红书springboot_vue_uniapp(15)评论和im添加图片
  • vue和react的框架原理
  • Elasticsearch向量库
  • React18 严格模式下的双重渲染之谜
  • 使用maven-shade-plugin解决es跨版本冲突
  • DHTMLX重磅发布React Scheduler组件,赋能日程管理开发!
  • PDF 文本提取技术深度对比:基于规则与基于模型的两种实现
  • 数学建模-线性规划。
  • 2025国赛数学建模C题详细思路模型代码获取,备战国赛算法解析——层次分析法
  • Java+Redis+SpringBoot定时器-定时发布商品
  • UNet改进(30):SageAttention在UNet中的4-Bit量化实现详解
  • 多参数状态监测集成终端设备怎么选
  • 日常反思总结2025.8.5
  • 2025金九银十Java后端面试攻略
  • 关于为什么ctrl c退不出来SecureCRT命令行的原因及其解决方法:
  • 变频器实习DAY21 区分BU和SUB 区分BJT和MOS 体二极管