OJ 26217 :Work Scheduling(贪心+优先队列)
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.
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
For each case, output one line : A single number on a line by itself that is the maximum possible profit FJ can earn.
3
2 10
1 5
1 7
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).
这道题的贪心的策略就是:
先把所有的价值都加进去.然后同步统计一个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(贪心+优先队列)的更多相关文章
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- 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 ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- 贪心+优先队列 HDOJ 5360 Hiking
题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
- Painting The Fence(贪心+优先队列)
Painting The Fence(贪心+优先队列) 题目大意:给 m 种数字,一共 n 个,从前往后填,相同的数字最多 k 个在一起,输出构造方案,没有则输出"-1". 解题思 ...
- CF140C New Year Snowmen(贪心+优先队列)
CF140C 贪心+优先队列 贪心策略:每次取出数量最多的三种球,合成一个答案,再把雪球数都-1再插回去,只要还剩下三种雪球就可以不断地合成 雪球数用优先队列维护 #include <bits/ ...
- [luoguP2949] [USACO09OPEN]工作调度Work Scheduling(贪心 + 优先队列)
传送门 这个题类似于建筑抢修. 先按照时间排序. 如果当前时间小于任务截止时间就选, 否则,看看当前任务价值是否比已选的任务的最小价值大, 如果是,就替换. 可以用优先队列. ——代码 #includ ...
随机推荐
- LaTeX 控制图片的位置
加感叹号来忽略“美学”标准. \begin{figure}[!htb] \usepackage{float}\begin{figure}[H]插到你代码相应的位置. 1,插入并列的子图 \usepac ...
- Python程序退出方式(sys.exit() os._exit() os.kill() os.popen(...))
对于如何结束一个Python程序或者用Python操作去结束一个进程等,Python本身给出了好几种方法,而这些方式也存在着一些区别,对相关的几种方法看了并实践了下,同时也记录下. 参考: Pytho ...
- springBoot 案例
一.工具 JDK1.7 Eclipse Maven 这里Eclipse集成Maven的这一步就省了! 二.编码 新建Maven Project 命名为:SpringBootDemo 选项如图 2.修改 ...
- ReactNative安装配置
1.安装jdk1.8,配置好path, javac,java -version 2.安装设置Android sdk a. 解压:D:\www\sdk\adt-bundle-windows-x86_64 ...
- ROS Learning-021 learning_tf-05(编程) now() 和 Time(0) 的区别 (Python版)
ROS Indigo learning_tf-05 now() 和 Time(0)的区别 (Python版) - waitForTransform() 函数 我使用的虚拟机软件:VMware Work ...
- VSTO的简单用法
一直听说vsto这个名词,还真不知道什么意思,今天了解了一下,原来他的全程是Visual Studio Tools For Office,说他是VBA的替身(VBA俺也不是很懂),刚才上网查询做了个例 ...
- Jsp入门第二天
1. JSP 指令: JSP指令(directive)是为JSP引擎而设计的. 它们并不直接产生任何可见输出, 而只是告诉引擎如何处理JSP页面中的其余部分. 2. 在目前的JSP 2.0中,定义了p ...
- Samy Kamka、吴石黑客信息
Samy Kamka 10年前他就曾成功利用AJAX蠕虫攻击了当时最火的社交网站MySpace.com,2009年的Twitter蠕虫事件和2011年新浪微博蠕虫事件都沿袭了他当时的方法. 2005年 ...
- Android proguard代码混淆
为什么要代码混淆? Android的安装文件是apk格式.APK是AndroidPackage的缩写.是由android sdk编译的工程打包生成的安装程序文件. Apk其实是zip文件,但是后缀名被 ...
- postgre-sql语法
//客户端查询 public void pgsearchclient(HttpContext context, string starttime, string endtime, int page, ...