牛奶调度

题目描述

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.

FJN(1 <= N <= 10,000)头牛要挤牛奶,每头牛需要花费1单位时间。

奶牛很厌烦等待,奶牛i在它的截止时间d_i (1 <= d_i <= 10,000)前挤g(1 <= g_i <= 1000)的奶,否则将不能挤奶。时间t开始时为0,即在时间t=x时,最多可以挤x头奶牛。

请计算FJ的最大挤奶量。

输入输出格式

输入格式:

l Line 1: The value of N.

l 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.

思路

d为关键字由小到大排序;

贪心选择,多加否则加多;

小根堆辅助维护;

代码

 1 #include<queue>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 const int maxn=1e4+10;
 6 int n,ans;
 7 priority_queue<int>q;
 8 struct nate{int d,g;}p[maxn];
 9 bool comp(nate x,nate y){return x.d<y.d;}
10 int main(){
11     scanf("%d",&n);
12     for(int i=1;i<=n;i++) scanf("%d%d",&p[i].g,&p[i].d);
13     sort(p+1,p+n+1,comp);
14     int a;
15     for(int i=1,j=0;i<=n;i++){
16         if(j<p[i].d) q.push(-p[i].g),j++;
17         else{
18             a=-q.top();
19             if(p[i].g>a) a=p[i].g;
20             q.pop();
21             q.push(-a);
22         }
23     }
24     while(!q.empty()) ans-=q.top(),q.pop();
25     printf("%d\n",ans);
26     return 0;
27 }

 

posted @ 2017-10-23 21:52  J_william  阅读(242)  评论(0编辑  收藏  举报