洛谷 P2949 [USACO09OPEN]工作调度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 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.
约翰有太多的工作要做。为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间。 他的工作日从0时刻开始,有1^8个单位时间。在任一时刻,他都可以选择编号1~N的N(1 <= N <= 10^6)项工作中的任意一项工作来完成。 因为他在每个单位时间里只能做一个工作,而每项工作又有一个截止日期,所以他很难有时间完成所有N个工作,虽然还是有可能。 对于第i个工作,有一个截止时间D_i(1 <= D_i <= 10^9),如果他可以完成这个工作,那么他可以获利P_i( 1<=P_i<=10^9 ). 在给定的工作利润和截止时间下,约翰能够获得的利润最大为多少.
输入输出格式
输入格式:
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.
输入输出样例
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).
思路:用一个优先对列进行贪心。
错因:优先队列的优先级是先大后小,我以为是先小后大。
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n;
long long ans,time;
struct nond{
long long end,val;
}v[];
priority_queue<long long,vector<long long>,greater<long long> >que;
int cmp(nond a,nond b){
return a.end<b.end;
}
int main(){
cin>>n;
for(int i=;i<=n;i++)
cin>>v[i].end>>v[i].val;
sort(v+,v++n,cmp);
for(int i=;i<=n;i++){
if(time<v[i].end){
time++;
ans+=v[i].val;
que.push(v[i].val);
}
else if(time==v[i].end&&que.top()<v[i].val){
ans-=que.top();
ans+=v[i].val;
que.pop();
que.push(v[i].val);
}
}
cout<<ans;
}
洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling的更多相关文章
- 洛谷 P2949 [USACO09OPEN]工作调度Work Scheduling 题解
P2949 [USACO09OPEN]工作调度Work Scheduling 题目描述 Farmer John has so very many jobs to do! In order to run ...
- 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 <= ...
- LUOGU P2949 [USACO09OPEN]工作调度Work Scheduling (贪心)
解题思路 明明一道比较简单的贪心结果挂了好几次23333,就是按照时间排序,然后拿一个小根堆维护放进去的,如果时间允许就入队并且记录答案.如果不允许就从堆里拿一个最小的比较. #include< ...
- 题解 P2949 【[USACO09OPEN]工作调度Work Scheduling】
P2949 [USACO09OPEN]工作调度Work Scheduling 题目标签是单调队列+dp,萌新太弱不会 明显的一道贪心题,考虑排序先做截止时间早的,但我们发现后面可能会出现价值更高却没有 ...
- [USACO09OPEN] 工作调度Work Scheduling (贪心/堆)
[USACO09OPEN] 工作调度Work Scheduling 题意翻译 约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^ ...
- 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling
题目描述 Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes onl ...
- [luoguP2949] [USACO09OPEN]工作调度Work Scheduling(贪心 + 优先队列)
传送门 这个题类似于建筑抢修. 先按照时间排序. 如果当前时间小于任务截止时间就选, 否则,看看当前任务价值是否比已选的任务的最小价值大, 如果是,就替换. 可以用优先队列. ——代码 #includ ...
- 洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
洛谷 2953 [USACO09OPEN]牛的数字游戏Cow Digit Game 题目描述 Bessie is playing a number game against Farmer John, ...
随机推荐
- poi判断一行是隐藏的getZeroHeight()
poi判断一行是隐藏的 getZeroHeight() boolean isZeroHeight = row.getZeroHeight(); if(isZeroHeight){ // 如果为隐藏行就 ...
- ubuntu12.04安装翻译软件stardict及卸载
下载: 1.打开软件中心.搜索stardict,星际译王,即ubuntu下的翻译软件. 点击下载就可以. 2.打开终端,输入 $sudo apt-get install stardict 按提示就可以 ...
- [Android]RecyclerView的简单演示样例
去年google的IO上就展示了一个新的ListView.它就是RecyclerView. 下面是官方的说明,我英语能力有限,只是我大概这么理解:RecyclerView会比ListView更具有拓展 ...
- Methods Collection of Enumerating Com Port in Windows, by C
According to this stack overflow thread, PJ Naughter has implemented 9 methods to emunerate com port ...
- [POI 2007] 旅游景点
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1097 [算法] 首先,用Dijkstra算法求出2-k+1到每个点的最短路 然后,我 ...
- 4.matlab基础
1 函数句柄 clear all; f1=@cos t=:pi/:pi f1(t) f2=@complex f2(,) clear all %函数句柄 f1=@char %函数句柄转换为字符串 s1= ...
- 如何用写js弹出层 ----2017-03-29
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Flask-上传文件和访问上传的文件
1.1.上传文件和访问上传的文件 upload_file_demo.py from flask import Flask,request,render_template import os from ...
- infludb语法--官网
https://docs.influxdata.com/influxdb/v0.8/api/query_language/ InfluxDB features a SQL like query lan ...
- 清北集训Day1T3 LYK loves jumping(期望DP)
题目描述 LYK在玩一个魔法游戏,叫做跳跃魔法. 有n个点,每个点有两个属性hi和ti,表示初始高度,和下降高度.也就是说,它初始时高度为hi,一旦LYK踩在这个点上,由于重力的影响,这个点的高度会下 ...