1 条题解

  • 0
    @ 2024-9-6 13:52:59

    容易想到一个贪心策略:在最优解中,对于每个天数 tt,在保证不卖出过期商品的前提下,尽量卖出利润前 tt 大的商品,因此我们可以依次考虑每个商品,动态维护一个满足上述性质的方案。

    把商品按照过期时间排序,建立一个初始为空的小根堆(节点权值为商品利润),然后扫描每个商品:

    1、当前商品的过期时间 tt 等于当前堆中的商品个数,说明在目前方案中,前 tt 天已经安排了 tt 个商品卖出。此时,若当前商品的利润大于堆顶权值(已经安排的 tt 个商品中的最低利润),则替换掉堆顶(用当前商品替换掉原方案中利润最低的商品)

    2、当前商品过期时间 tt 大于当前堆中商品个数,直接把商品插入堆中。

    最终,堆中所有商品就是我们要卖出的商品,利润之和就是答案。

    #include <bits/stdc++.h>
    const int N = 100010;
    typedef std::pair<int, int> PII;
    std::priority_queue<int, std::vector<int>, std::greater<int>> heap;
    
    int main()
    {
    	int n;
    	std::cin >> n;
    	PII p;
    	std::vector<PII> ve(n);
    	for (int i = 1; i <= n; i ++) {
    		std::cin>> p.second>> p.first;
    		ve.push_back(p);
    	}
    	std::sort(ve.begin(), ve.end());
    	for(auto v : ve)
    	{
    		heap.push(v.second);
    		if (heap.size() > v.first) heap.pop();
    	}
    	int ans = 0;
    	while (heap.size()) ans += heap.top(), heap.pop();
    	std::cout << ans << std::endl;
    	return 0;
    }
    
    • 1

    信息

    ID
    100
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    递交数
    32
    已通过
    7
    上传者