题解 | #Supermarket#

Supermarket

https://ac.nowcoder.com/acm/contest/1011/A

思路:按时间贪心,每次更新前面几天能卖出的最大利润,用堆可以自动排序,每次筛出堆顶元素最小的那个,所以用小根堆就可以了。

#include<bits/stdc++.h>
using namespace std;
const int N = 100010;

pair<int,int> a[N];

int main()
{
    int n;
    while(cin>>n){
        for(int i=0;i<n;i++){
            cin>>a[i].second>>a[i].first;
        }
        sort(a,a+n);
        priority_queue<int,vector<int>,greater<int>> h;
        for(int i=0;i<n;i++){
            h.push(a[i].second);
            if(h.size()>a[i].first) h.pop();
        }
        long long ans=0;
        while(h.size()){
            ans+=h.top();
            h.pop();
        }
        cout<<ans<<endl;
    }
    return 0;
}
全部评论

相关推荐

美团笔试只有我觉得难吗?
天灰灰会不会:我也觉得难,选择题一堆大模型题目乱蒙,编程题拼尽全力依然只a2
投递美团等公司6个岗位 > 美团求职进展汇总
点赞 评论 收藏
分享
穿件外套出门:这简历一眼太水了,前面有的没的直接删,写项目亮点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务