[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 尽管这一题没有看题解,但是耗时还是比本应耗费的时间要长,所以还是写一下,以提升经验 这 ...
随机推荐
- android点击事件的四种方式
android点击事件的四种方式 第一种方式:创建内部类实现点击事件 代码如下: package com.example.dail; import android.text.TextUtils; im ...
- struts常量<constant>说明
1.<constant name="struts.action.extension" value="do" />这个时候访问action都必须加.d ...
- Python 变量和常量及数据类型
一.变量的命名 变量由字母.数字和下划线组成.变量的第1个字符必须是字母或下划线. 二.变量的赋值 例: x = 1 三.局部变量 局部变量只能在函数或者代码段内使用. 四.全局变量 在函数之外定义的 ...
- CocoaPods 创建私有仓库
这里有个坑首先需要注意,创建私有cocoapods仓库需要两个git仓库,即代码仓库,Specs文件仓库. 一.创建私有库 1.创建自己源码仓库,假设是A.git; 2.对A仓库: git add . ...
- 0324操作系统cmd功能的扩展
需求:1.实现清屏功能 2.实现不区分大小写功能 3.添加功能能添加新的命令符 设计:1.使用system("cls")清屏. 2.使用strlwr()函数把大写都变成小写 3.( ...
- Alpha 冲刺7
队名:日不落战队 安琪(队长) 今天完成的任务 完善回收站. 学习okhttp. 明天的计划 继续研究okhttp. 尝试登录的数据对接. 还剩下的任务 个人信息对接. 遇到的困难 今天白天焊接,晚上 ...
- Alpha事后诸葛(团队)
[设想和目标] Q1:我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? "小葵日记"是为了解决18-30岁年轻用户在记录生活时希望得到一美体验友好 ...
- PAT L1-015 跟奥巴马一起画方块
https://pintia.cn/problem-sets/994805046380707840/problems/994805124398956544 美国总统奥巴马不仅呼吁所有人都学习编程,甚至 ...
- php获取指定div内容
<?php $p="http://127.0.0.1:8080/website/index.html"; $ch = curl_init(); curl_setopt($ch ...
- 关于java读取excle文件的相关方法 ;
1.读取Excle文件内容的方法 拿过来可以直接用 : 2.参照 http://www.anyrt.com/blog/list/importexcel.html#6 更多知识请参考:http://ww ...