洛谷 P3093 牛奶调度Milk Scheduling

版权声明:转载注明出处就好啦~ https://blog.csdn.net/qq_36693533/article/details/78482933

https://www.luogu.org/problemnew/show/3093

思路

详见代码注释

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
int n,maxn,now,ans;
struct poins
{
    int val,pos;
}l[1000010];
bool operator < (poins a,poins b)
{
    return a.val<b.val;
} 
priority_queue<poins>q;
bool cmp(poins a,poins b)
{
    return a.pos>b.pos;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&l[i].val,&l[i].pos);
        maxn=max(maxn,l[i].pos);//记录最大截止时间 
    }
    sort(l+1,l+n+1,cmp);//时间从大到小排序 
    now=1;
    for(int i=maxn;i>=1;i--)
    {
        while(l[now].pos==i)//按截止时间从大到小加入,堆中元素一定合法 
        {
            q.push(l[now]);
            now++;
        }
        if(!q.empty()) 
        {
            ans+=q.top().val;//如果当前可以挤奶,就选择挤奶量最多的一头牛去挤奶
            q.pop();
        }
    }
    printf("%d",ans);
    return 0;
} 

猜你喜欢

转载自blog.csdn.net/qq_36693533/article/details/78482933