洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling(贪心)
来源:互联网 发布:it盈利模式 编辑:程序博客网 时间:2024/05/19 06:36
题目描述
Farmer John has N cows that need to be milked (1 <= N <= 10,000), each of which takes only one unit of time to milk.
Being impatient animals, some cows will refuse to be milked if Farmer John waits too long to milk them. More specifically, cow i produces g_i gallons of milk (1 <= g_i <= 1000), but only if she is milked before a deadline at time d_i (1 <= d_i <= 10,000). Time starts at t=0, so at most x total cows can be milked prior to a deadline at time t=x.
Please help Farmer John determine the maximum amount of milk that he can obtain if he milks the cows optimally.
FJ有N(1 <= N <= 10,000)头牛要挤牛奶,每头牛需要花费1单位时间。
奶牛很厌烦等待,奶牛i在它的截止时间d_i (1 <= d_i <= 10,000)前挤g(1 <= g_i <= 1000)的奶,否则将不能挤奶。时间t开始时为0,即在时间t=x时,最多可以挤x头奶牛。
请计算FJ的最大挤奶量。
输入输出格式
输入格式:
Line 1: The value of N.Lines 2..1+N: Line i+1 contains the integers g_i and d_i.
输出格式:
Line 1: The maximum number of gallons of milk Farmer John can obtain.
输入输出样例输入样例#1:
4
10 3
7 5
8 1
2 1
输出样例#1:
25
说明
There are 4 cows. The first produces 10 gallons of milk if milked by time 3, and so on.Farmer John milks cow 3 first, giving up on cow 4 since she cannot be milked by her deadline due to the conflict with cow 3. Farmer John then milks cows 1 and 2.
思路:首先按截止时间从大到小排序,然后在时间内维护一个大根堆。(也就是从大时间向小时间去扫一遍)。如果当前可以挤奶,就选择挤奶量最多的一头牛去挤奶,因为是从大事件向小时间扫。所以当前的堆中数据一定是合法的。这个显然。
代码如下:
#include<iostream>#include<cstdio>#include<cstdlib>#include<algorithm>#include<queue>using namespace std;const int maxn=200005;struct dqs{ int jz,nl;}hh[maxn];bool cmp(dqs a,dqs b){ return a.jz>b.jz;}priority_queue<dqs>q;bool operator <(dqs a,dqs b){ return a.nl<b.nl;}int main(){ int n,ans=0,maxx=-1,now=1; scanf("%d",&n); for(int i=1;i<=n;i++) { scanf("%d%d",&hh[i].nl,&hh[i].jz); maxx=max(maxx,hh[i].jz); } sort(hh+1,hh+n+1,cmp); for(int i=maxx;i>=1;i--) { while(hh[now].jz==i) { q.push(hh[now]); now++; } if(!q.empty()) { ans+=q.top().nl; q.pop(); } } printf("%d\n",ans); return 0;}
- 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling(贪心)
- 洛谷 P3093 [USACO13DEC]牛奶调度Milk Scheduling
- P3093 [USACO13DEC]牛奶调度Milk Scheduling
- 洛谷 P3093 牛奶调度Milk Scheduling
- 洛谷 3093 [USACO13DEC]牛奶调度Milk Scheduling
- 【洛谷 3093】[USACO13DEC]牛奶调度Milk Scheduling
- 【USACO13DEC】洛谷3093 Milk Scheduling
- |洛谷|贪心|P1208 [USACO1.3]混合牛奶 Mixing Milk
- 【贪心】洛谷 P1208 [USACO1.3]混合牛奶 Mixing Milk
- bzoj 4096: [Usaco2013 dec]Milk Scheduling 贪心
- 【贪心】Mixing Milk 混合牛奶 milk (Usaco_Training 1.3)
- 普及练习场 贪心 [USACO1.3]混合牛奶 Mixing Milk
- [BZOJ 4096] Usaco2013 dec Milk Scheduling · 贪心
- 贪心区间调度 Greedy interval scheduling problem
- Mixing Milk混合牛奶
- 牛奶模式Milk Patterns
- 调度域(Scheduling Domain)
- 任务调度(Task Scheduling)
- centos 上传下载
- Unity Shader-遮挡处理
- 初识Spring_05
- impala遇到的坑
- 安卓微信浏览器刷新
- 洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling(贪心)
- java.lang.NoClassDefFoundError: Lorg/apache/logging/log4j/Logger解决办法
- lua学习笔记——元表(Metatable)
- csvn下载地址
- proxy下载地址
- linux下vi命令整理
- springmvc的controller层如何获取到请求的数据
- 安装node-sass提示没有vendor目录的解决办法
- Ajax中Get请求与Post请求的区别