洛谷P3093 [USACO13DEC]牛奶调度Milk Scheduling(贪心)

题目描述

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;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值