首页 > 其他分享 >[AcWing 145] 超市

[AcWing 145] 超市

时间:2022-08-15 23:56:31浏览次数:57  
标签:145 int pop 超市 过期 products heap 小根堆 AcWing

image
image

贪心 + 小根堆


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;

const int N = 1e6 + 10;

int n;

void solve()
{
    while (cin >> n) {
        vector<PII> products(n);
        for (int i = 0; i < n; i ++) {
            int p, d;
            cin >> p >> d;
            products[i] = {d, p};
        }
        sort(products.begin(), products.end());
        priority_queue<int, vector<int>, greater<int>> heap;
        for (auto [d, p] : products) {
            heap.push(p);
            if (heap.size() > d)
                heap.pop();
        }
        int res = 0;
        while (heap.size()) {
            res += heap.top();
            heap.pop();
        }
        cout << res << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    solve();

    return 0;
}

  1. 按照过期时间从小到大排序,使用小根堆,按照过期时间从小到大的顺序枚举每个商品,每次把商品的利润放到小根堆中,如果小根堆的大小大于过期时间,就把小根堆的堆顶弹出,最后小根堆中存的就是选的商品的利润

标签:145,int,pop,超市,过期,products,heap,小根堆,AcWing
From: https://www.cnblogs.com/wKingYu/p/16590129.html

相关文章

  • AcWing周赛62-64 中比较有意思的小题题解
    AcWing周赛62-64(选讲)感觉比较思维4502.集合操作https://www.acwing.com/problem/content/4505/根据题意,肯定要使得所取的最大值最大,平均值最小。又因为每次放进来的的......
  • [AcWing 4507] 子数组异或和
    异或的性质点击查看代码#include<bits/stdc++.h>usingnamespacestd;typedeflonglongLL;constintN=1e6+10;intn;inta[N];voidsolve(){......
  • AcWing3293.统计单词
    原题链接string.back()、string.pop_back()妙用#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;intmain(){stringstr;......
  • Acwing 第 64 场周赛 C 4507. 子数组异或和(异或+前缀和)
    https://www.acwing.com/problem/content/4510/给定一个长度为n的整数数组a1,a2,…,an。请你统计一共有多少个数组a的非空连续子数组能够同时满足以下所有条件:该......
  • AcWing3391.今年第几天?(日期题)
    原题链接https://www.acwing.com/problem/content/3394/日期题思路满足下面条件之一的是闰年:年份是4的整数倍,而且不是100的整数倍;年份是400的整数倍。处理输......
  • acwing 1228. 油漆面积 扫描线
     X星球的一批考古机器人正在一片废墟上考古。该区域的地面坚硬如石、平整如镜。管理人员为方便,建立了标准的直角坐标系。每个机器人都各有特长、身怀绝技。它们感兴......