[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 (贪心/堆)的更多相关文章

  1. LUOGU P2949 [USACO09OPEN]工作调度Work Scheduling (贪心)

    解题思路 明明一道比较简单的贪心结果挂了好几次23333,就是按照时间排序,然后拿一个小根堆维护放进去的,如果时间允许就入队并且记录答案.如果不允许就从堆里拿一个最小的比较. #include< ...

  2. 题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目标签是单调队列+dp,萌新太弱不会 明显的一道贪心题,考虑排序先做截止时间早的,但我们发现后面可能会出现价值更高却没有 ...

  3. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...

  4. 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling

    P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...

  5. [luoguP2949] [USACO09OPEN]工作调度Work Scheduling(贪心 + 优先队列)

    传送门 这个题类似于建筑抢修. 先按照时间排序. 如果当前时间小于任务截止时间就选, 否则,看看当前任务价值是否比已选的任务的最小价值大, 如果是,就替换. 可以用优先队列. ——代码 #includ ...

  6. 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 ...

  7. P2949 [USACO09OPEN]工作调度Work Scheduling

    题目描述 约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^8个单位时间.在任一时刻,他都可以选择编号1~N的N(1 <= ...

  8. 洛谷P2949 工作调度Work Scheduling [USACO09OPEN] 贪心

    正解:贪心+并查集(umm不用并查集也成qwq 解题报告: 水题?主要感觉想到了俩方法然后还只实现了一个,怕忘了所以想着开个新坑记录下qwq 然后先放下传送门QAQ(哦这题和supermarket,双 ...

  9. bzoj1572 [Usaco2009 Open]工作安排Job【贪心 堆】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1572 尽管这一题没有看题解,但是耗时还是比本应耗费的时间要长,所以还是写一下,以提升经验 这 ...

随机推荐

  1. 445. Cosine Similarity【LintCode java】

    Description Cosine similarity is a measure of similarity between two vectors of an inner product spa ...

  2. 3.Airflow使用

    1. airflow简介2. 相关概念2.1 服务进程2.1.1. web server2.1.2. scheduler2.1.3. worker2.1.4. celery flower2.2 相关概 ...

  3. ecshop以及一些需要注意的

    Deprecated: Assigning the return value of new by reference is deprecated in 定位到出错的那一行: $this->_ol ...

  4. 智能客服 对话实现--python aiml包

    利用了python的aiml包进行应答 什么是AIML? AIML是Richard Wallace开发的. 他开发了一个叫A.L.I.C.E(Artificial Linguistics Intern ...

  5. 对石家庄铁道大学网站UI的分析

         作为我们团队的PM,老师对我们提出了一些额外的要求,所以我发表这篇博客来谈一下对石家庄铁道大学网站UI的分析.      首先,PM 对项目所有功能的把握, 特别是UI.最差的UI, 体现了 ...

  6. 进阶系列(8)——匿名方法与lambda表达式

    一 匿名方法的介绍     匿名方法是为了简化委托的实现,方便调用委托方法而出现的,同时,匿名方法也是学好lambda表达式的基础.在委托调用的方法中,如果方法只被调用一次,这个时候我们就没有必要创建 ...

  7. 周总结<5>

    周次 学习时间 新编写代码行数 博客量(篇) 学到知识点 12 10 100 1 路由器的设置(ospf协议):网页设计:哈夫曼树(C语言数构) Html案例: <!DOCTYPE html P ...

  8. 第11章 认识和学习bash

    认识bash这个shell 硬件.内核和shell 用户操作计算机流程如下: 用户——>用户界面(shell,KDE,application)——>核心(kernel)——>硬件(h ...

  9. SQL 语句(增删改查)

    一.增:有4种方法1.使用insert插入单行数据: --语法:insert [into] <表名> [列名] values <列值> 例:insert into Strden ...

  10. AVAudioPlayer播放音乐

    1:首先创建一个新的项目,继承自UIViewController 2:导入框架AVFoundation.framework 右键工程名,在Build Phases的Link Binary With L ...