当前位置: 首页 > 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.dtcms.com/a/316454.html

相关文章:

  • 【人工智能-18】机器学习:决策树、随机森林
  • 什么情况下浮动IP(Floating IP)会“漂移”(Drift)
  • 浮动IP(Floating IP)的删除通常需要满足什么条件
  • 小程序点击菜单栏实现样式动态切换
  • 对于包含大量文件的程序的便捷makefile操作
  • RK3568 Linux驱动学习——字符设备驱动开发
  • windows内核研究(软件调试-内存断点)
  • 永磁同步电机无速度算法--具有电流测量误差鲁棒性的永磁同步电机无传感器控制的自适应广义复矢量观测器
  • 二叉树算法
  • 02-算法
  • Java后端高频面试题
  • EP02:【DL 第二弹】张量的索引、分片、合并以及维度调整
  • 如何选择正确的体育/电竞数据接口服务商?
  • 力扣148:排序链表
  • Android 开发中,HandlerThread、IntentService 和 AsyncTask区别对比
  • Pytorch基础入门2
  • C++面向对象编程基础:从类定义到封装机制详解
  • 【Linux网络编程】socket基础
  • 风丘助力混合动力汽车工况测试:精准采集整车信号解决方案
  • Datawhale AI夏令营 第三期 task2 稍微改进
  • P1026 [NOIP 2001 提高组] 统计单词个数
  • 计算机网络:详解路由器如何转发子网数据包
  • Java JDBC连接池深度解析与实战指南
  • SAP PP CK466
  • 解决docker load加载tar镜像报json no such file or directory的错误
  • jQuery中Ajax返回字符串处理技巧
  • Window.structuredClone() 指南
  • 基于深度学习钢铁表面缺陷检测系统(yolov8/yolov5)
  • 《算法导论》第 3 章 - 函数的增长
  • 本地配置运行https协议