[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;
}

最新文章

  1. [GraphQL] Use Arguments in a GraphQL Query
  2. 【多线程同步案例】Race Condition引起的性能问题
  3. POJ 2420 A Star not a Tree? (计算几何-费马点)
  4. mysql函数二
  5. PHP:win7 ASP.NET环境与PHP(WAMP)环境如何共存
  6. 服务调用框架DataStrom
  7. zend studio快捷键
  8. Windows 10 &amp; change DNS
  9. 复旦大学2016--2017学年第一学期(16级)高等代数I期末考试第七大题解答
  10. Java虚拟机-JVM各种参数配置大全详细
  11. 3.3《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——less即more
  12. raw_input 和input 区别
  13. Mysql 索引 n-gram分词引擎使用
  14. 【转载】python中利用smtplib发送邮件的3中方式 普通/ssl/tls
  15. 局部响应归一化(Local Response Normalization,LRN)
  16. 在Windows7/8/10上,安装IIS
  17. tf.truncated_normal的用法
  18. jQuery实现按Esc清除信息功能
  19. 《利用Python进行数据分析》笔记---第5章pandas入门
  20. day7异常处理

热门文章

  1. [network]数字签名
  2. python怎么安装requests、beautifulsoup4等第三方库
  3. Python3【基础】-表达式与运算符
  4. ES6的新特性(16)——Generator 函数的语法
  5. Alpha发布——美工+文案
  6. “Hello World!”团队第五周第三次会议
  7. postman的巨坑 之 cookie
  8. 安装cocoa pods
  9. firefox插件Firebug的使用教程
  10. break,continue,return 的区别