[USACO09OPEN] 工作调度Work Scheduling (贪心/堆)
[USACO09OPEN] 工作调度Work Scheduling
题意翻译
约翰有太多的工作要做。为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间。 他的工作日从0时刻开始,有10^9个单位时间。在任一时刻,他都可以选择编号1~N的N(1 <= N <= 10^6)项工作中的任意一项工作来完成。 因为他在每个单位时间里只能做一个工作,而每项工作又有一个截止日期,所以他很难有时间完成所有N个工作,虽然还是有可能。 对于第i个工作,有一个截止时间D_i(1 <= D_i <= 10^9),如果他可以完成这个工作,那么他可以获利P_i( 1<=P_i<=10^9 ). 在给定的工作利润和截止时间下,约翰能够获得的利润最大为多少.
题目描述
Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make money on the jobs he does, each one of which takes just one time unit.
His work day starts at time 0 and has 1,000,000,000 time units (!). He currently can choose from any of N (1 <= N <= 100,000) jobs
conveniently numbered 1..N for work to do. It is possible but
extremely unlikely that he has time for all N jobs since he can only work on one job during any time unit and the deadlines tend to fall so that he can not perform all the tasks.
Job i has deadline D_i (1 <= D_i <= 1,000,000,000). If he finishes job i by then, he makes a profit of P_i (1 <= P_i <= 1,000,000,000).
What is the maximum total profit that FJ can earn from a given list of jobs and deadlines? The answer might not fit into a 32-bit integer.
输入输出格式
输入格式:
Line 1: A single integer: N
Lines 2..N+1: Line i+1 contains two space-separated integers: D_i and P_i
输出格式:
- Line 1: A single number on a line by itself that is the maximum possible profit FJ can earn.
输入输出样例
输入样例#1:
3
2 10
1 5
1 7
输出样例#1:
17
说明
Complete job 3 (1,7) at time 1 and complete job 1 (2,10) at time 2 to maximize the earnings (7 + 10 -> 17).
Solution
很容易想到按时间从小到大排序,因为它的截止时间越长,我们就可以了把前面的时间去留给其他物品,然后贪心的去选,但是这样会WA,为什么?
看一下这种情况,我们在前面选了价值较小的工作,但是当前有一个价值比它大得多的工作我们却没法选,这个时候就会后悔,所以这是一道可以反悔的贪心题
那么怎么反悔呢?我们用一个小根堆去维护,每次选的工作,我们把它的价值丢进小根堆,如果碰到一个选不了的工作,我们就拿它和堆顶比较,如果比它大,那么堆顶其实没必要选的,所以把它弹出去
具体情况是什么样子的呢?(以下我们用\(t\)表示工作的截止时间)
因为一个单位时间只能做一个工作,所以堆的元素个数表示至少已经过了\(size\)个单位时间
1. 当前工作的t小于堆的元素个数,说明当前工作已经截止了,不用管
2. 当前工作的t大于堆内元素个数,直接插入
3. 当前工作的t等于堆内元素个数,与堆顶比较,看谁更优
Code
#include<bits/stdc++.h>
#define in(i) (i=read())
#define il extern inline
#define rg register
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define lol long long
using namespace std;
const lol N=1e6+10;
lol read() {
lol ans=0, f=1; char i=getchar();
while (i<'0' || i>'9') {if(i=='-') f=-1; i=getchar();}
while (i>='0' && i<='9') ans=(ans<<1)+(ans<<3)+(i^48), i=getchar();
return ans*f;
}
struct Node {
lol day,v;
bool operator < (const Node &a) const {return day<a.day;}
}t[N];
priority_queue<lol,vector<lol>,greater<lol> >q;
int main()
{
lol n,ans=0; in(n);
for (lol i=1;i<=n;i++)
in(t[i].day), in(t[i].v);
sort(t+1,t+1+n);
for (lol i=1;i<=n;i++) {
if(t[i].day<q.size()) continue;
else if(t[i].day==q.size()) {
if(t[i].v>q.top()) {
ans-=q.top(); q.pop();
q.push(t[i].v); ans+=t[i].v;
}
}else ans+=t[i].v,q.push(t[i].v);
}cout<<ans<<endl;
}
[USACO09OPEN] 工作调度Work Scheduling (贪心/堆)的更多相关文章
- LUOGU P2949 [USACO09OPEN]工作调度Work Scheduling (贪心)
解题思路 明明一道比较简单的贪心结果挂了好几次23333,就是按照时间排序,然后拿一个小根堆维护放进去的,如果时间允许就入队并且记录答案.如果不允许就从堆里拿一个最小的比较. #include< ...
- 题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】
P2949 [USACO09OPEN]工作调度Work Scheduling 题目标签是单调队列+dp,萌新太弱不会 明显的一道贪心题,考虑排序先做截止时间早的,但我们发现后面可能会出现价值更高却没有 ...
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解
P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling
P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...
- [luoguP2949] [USACO09OPEN]工作调度Work Scheduling(贪心 + 优先队列)
传送门 这个题类似于建筑抢修. 先按照时间排序. 如果当前时间小于任务截止时间就选, 否则,看看当前任务价值是否比已选的任务的最小价值大, 如果是,就替换. 可以用优先队列. ——代码 #includ ...
- luogu P2949 [USACO09OPEN]工作调度Work Scheduling
题目描述 Farmer John has so very many jobs to do! In order to run the farm efficiently, he must make mon ...
- P2949 [USACO09OPEN]工作调度Work Scheduling
题目描述 约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^8个单位时间.在任一时刻,他都可以选择编号1~N的N(1 <= ...
- 洛谷P2949 工作调度Work Scheduling [USACO09OPEN] 贪心
正解:贪心+并查集(umm不用并查集也成qwq 解题报告: 水题?主要感觉想到了俩方法然后还只实现了一个,怕忘了所以想着开个新坑记录下qwq 然后先放下传送门QAQ(哦这题和supermarket,双 ...
- bzoj1572 [Usaco2009 Open]工作安排Job【贪心 堆】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1572 尽管这一题没有看题解,但是耗时还是比本应耗费的时间要长,所以还是写一下,以提升经验 这 ...
随机推荐
- 笔试题:C++打印队列
题目:打印队列 题目介绍:现在用打印机打印队列,已知打印任务有9个优先级(1-9),现在给出一系列任务,求输出打印顺序(任务下标,从0开始). 例: 输入:9,3,5,4,7,1 输出:0,4,2,3 ...
- 基于Docker Compose构建的MySQL MHA集群
Docker MySQL MHA 基于Docker 1.13.1之上构建的MySQL MHA Docker Compose Project 可快速启动GTID模式下的MasterHA集群, 主用于My ...
- Python 内置函数介绍
作者博文地址:http://www.cnblogs.com/spiritman/ Python Built-in Functions
- DB2 9.5 数据库分区管理及应用实践
DB2 数据库分区是 DB2 企业版 DPF(Data Partitioning Feature)选件提供的,它主要用来为大规模数据处理.高并发数据访问提供支持.DB2 数据库分区采用 Share-n ...
- python获取前几天的时间
days的参数就是你想获取前多少天的数据,如果是昨天的话,则days=1 import datetime today=datetime.date.today() oneday=datetime.tim ...
- 【leetcode】62.63 Unique Paths
62. Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the di ...
- 【BioCode】Elm格式中提取位点信息
说明: ①Elm格式: PLMD ID Uniprot Accession Position Type Sequence Species PMIDsPlMD编号 ...
- CodeForces Round #527 (Div3) D2. Great Vova Wall (Version 2)
http://codeforces.com/contest/1092/problem/D2 Vova's family is building the Great Vova Wall (named b ...
- yum 安装php环境
centos下安装php环境 | 浏览:3831 | 更新:2014-11-04 17:01 1 2 3 分步阅读 在网上看了很多,很多都不能用,所以就把能用的实践下,过程记录下,方便自己和网友以后查 ...
- Linux 重定向输出到多个文件中
转自:http://codingstandards.iteye.com/blog/833695 用途说明 在执行Linux命令时,我们可以把输出重定向到文件中,比如 ls >a.txt,这时我们 ...