约翰有太多的工作要做。为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间。 他的工作日从0时刻开始,有10^8个单位时间。在任一时刻,他都可以选择编号1~N的N(1 <= N <= 10^6)项工作中的任意一项工作来完成。 因为他在每个单位时间里只能做一个工作,而每项工作又有一个截止日期,所以他很难有时间完成所有N个工作,虽然还是有可能。 对于第i个工作,有一个截止时间D_i(1 <= D_i <= 10^9),如果他可以完成这个工作,那么他可以获利P_i( 1<=P_i<=10^9 ). 在给定的工作利润和截止时间下,约翰能够获得的利润最大为多少.
Description

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 Njobs 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 Di (1 ≤ Di ≤ 1,000,000,000). If he finishes job i by then, he makes a profit of Pi (1 ≤ Pi ≤ 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.

Input

Multipel test cases. For each case :

* Line 1: A single integer: N

* Lines 2..N+1: Line i+1 contains two space-separated integers: Di and Pi

Output

For each case, output one line : A single number on a line by itself that is the maximum possible profit FJ can earn.

Sample Input

3
2 10
1 5
1 7

Sample Output

17

Hint

Complete job 3 (1, 7) at time 1 and complete job 1 (2, 10) at time 2 to maximize the earnings (7 + 10 → 17).

这道题的贪心的策略就是:

先把所有的价值都加进去.然后同步统计一个now统计时间.

如果当前时间已经超过了实现,那么我们就减掉价值最小的.

然后这个价值最小的用一个堆就可以维护.

AC代码:

#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define ll long long
#define MAX 100008
struct nod
{
ll d;
ll p;
}a[MAX];
bool cmp (nod a,nod b)
{
if(a.d!=b.d)
return a.d<b.d;
return a.p<b.p;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
priority_queue<int,vector<int>,greater<int> >q;
for(int i= ; i<n ; i++)
scanf("%d%d",&a[i].d,&a[i].p);
sort(a,a+n,cmp);
ll sum=;
ll now=;
for(int i= ; i<n ; i++)
{
now++;
sum+=a[i].p;
q.push(a[i].p);
if(now>a[i].d)
{
sum-=q.top();
q.pop();
now--;
}
}
printf("%lld\n",sum);
}
}

OJ 26217 :Work Scheduling(贪心+优先队列)的更多相关文章

  1. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

  2. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  3. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  4. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  5. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  6. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

  7. Painting The Fence(贪心+优先队列)

    Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...

  8. CF140C New Year Snowmen(贪心+优先队列)

    CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...

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

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

随机推荐

  1. Android开发 开启闪光灯 关键代码

    在AndroidManifest中注册响应的权限: <uses-permission android:name="android.permission.FLASHLIGHT" ...

  2. MyBatis总结三:使用动态代理实现dao接口

    由于我们上一篇实现MyBatis的增删改查的接口实现类的方法都是通过sqlsession调用方法,参数也都类似,所以我们使用动态代理的方式来完善这一点 MyBatis动态代理生成dao的步骤: 编写数 ...

  3. eclipse中安装git插件

    1 安装及配置git插件,问度娘即可 点击前往 2 eclipse 中怎么同步到 本地git仓库 和 码云远程仓库 点击前往

  4. 杭电acm 1021题

    题意是要求能被3整除的数所以为了避免大数据的产生,直接对每个数据求余,然后相加 #include "iostream" using namespace std; int main( ...

  5. DR客户端一直连接服务器....(6)

    DR客户端一直连接服务器....(非未选择网卡)解决方法: 控制面板-->网络和 Internet-->网络连接,打开本地连接(以太网)属性,将IPV4协议前面的对勾去掉,重启DR 这样D ...

  6. java中下面这些引入都代表什么意思啊?

    import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; import java.uti ...

  7. bzoj1735 [Usaco2005 jan]Muddy Fields 泥泞的牧场

    传送门 分析 我们知道对于没有障碍的情况就是将横轴点于纵轴点连边 于是对于这种有障碍的情况我们还是分横轴纵轴考虑 只不过对于有障碍的一整条分为若干个无障碍小段来处理 然后将标号小段连边,跑最大匹配即可 ...

  8. 深、浅copy

    深.浅copy多用于列表 浅copy:第一层中不变的数据是独立的,可变类型元素指向同一块内存地址 l1 = [1,2,3,["a","b"]] l2 = l1. ...

  9. 形式化验证工具(PAT)Reader-Writers Problem学习

    经过前几次的学习,我们应该对PAT有一点点的了解了,我们加下来就直接看例子中的一个问题,这个问题比较简单. 看代码: //The classic Readers/Writers Example mod ...

  10. SSH框架(四) struts2+spring3.0的登陆示例

    (一)关键理念及需要注意的地方: 使用struts2+spring3.0的框架搭建web程序,就是使用spring来进行依赖注入(依赖注入请参考baidu上面的解释:http://baike.baid ...