码迷,mamicode.com
首页 > 其他好文 > 详细

洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling

时间:2016-10-03 23:29:55      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:

 

题目描述

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.

 

倒序循环时间,把最后期限晚于当前时间的加入堆,贪心每次取一个最大值。

 

 1 /*by SilverN*/
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<cmath>
 7 #include<queue>
 8 using namespace std;
 9 const int mxn=10001;
10 int read(){
11     int x=0,f=1;char ch=getchar();
12     while(ch<0 || ch>9){if(ch==-)f=-1;ch=getchar();}
13     while(ch>=0 && ch<=9){x=x*10+ch-0;ch=getchar();}
14     return x*f;
15 }
16 int n;
17 struct node{
18     int d,g;
19 }a[mxn];
20 int cmp(const node a,const node b){
21     return a.d>b.d;
22 }
23 priority_queue<int>tp;
24 int main(){
25     n=read();
26     int i,j;
27     for(i=1;i<=n;i++){
28         a[i].g=read();
29         a[i].d=read();
30     }
31     sort(a+1,a+n+1,cmp);
32     int hd=1;
33     int ans=0;    
34     for(i=a[1].d;i;--i){
35         while(a[hd].d>=i){
36             tp.push(a[hd].g);
37             hd++;
38         }
39         if(tp.empty())continue;
40         ans+=tp.top();
41         tp.pop();
42     }
43     cout<<ans<<endl;
44     return 0;
45 }

 

洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling

标签:

原文地址:http://www.cnblogs.com/SilverNebula/p/5929769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!