当前位置: 首页 > 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.xdnf.cn/news/1248319.html

相关文章:

  • Mysql-事务
  • Nginx入门:高性能Web服务器详解
  • 【图像算法 - 09】基于深度学习的烟雾检测:从算法原理到工程实现,完整实战指南
  • Claude Code实战体验:AI智能编程助手如何重塑开发工作流?
  • 2. JS 有哪些数据类型
  • Linux的NFS与Autofs配置指南
  • nodejs 编程基础01-NPM包管理
  • 最优化中常见的优化理论
  • Shader开发(七)创建第一个Shader项目
  • 游戏画面总是卡顿怎么办 告别延迟畅玩游戏
  • DDoS 防护的未来趋势AI 如何改变安全行业
  • MySQL 5.7 和 8.0 离线安装教程(图文版适合电脑小白)
  • C++返回值优化(RVO):高效返回对象的艺术
  • 【基础】第八篇 Java 位运算符详解:从基础到实战应用
  • Unknown initial character set index ‘255’,Kettle连接MySQL数据库常见错误及解决方案大全
  • nuxt学习笔记
  • 什么是mysql的垂直分表,理论依据是什么,如何使用?
  • LeetCode 刷题【31. 下一个排列】
  • Apache OFBiz Scrum 组件命令注入漏洞
  • 力扣148:排序链表
  • 不可变集合
  • 笔记学习杂记
  • nordic通过j-link rtt viewer打印日志
  • Linux网络编程:TCP初体验
  • 永磁同步电机的矢量控制
  • Python包安全工程实践:构建安全可靠的Python生态系统
  • Redis类型之String
  • Python深度学习:从入门到进阶
  • ELK是什么
  • 分布式微服务--Nacos持久化