百度202093

本文介绍了一种基于优先队列和映射数据结构的算法,用于解决在限定价格和重量条件下,如何从不同价值和重量的纪念品中选择最多数量的问题。通过逆向遍历物品并检查是否满足条件来优化解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

纪念品


#include <vector>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

int main()
{
    int n, m, k;
    cin>>n>>m>>k;
    map<int, map<int, priority_queue<int, vector<int>, greater<>>>> mp;
    while(n>0) {
        n--;
        int price, weight, v;
        cin>>price>>weight>>v;
        mp[v][price].push(weight);
    }
    auto it = mp.rbegin();
    int sumk = 0;
    int sumw = 0;
    int res_count = 0;
    for(;it != mp.rend(); it++){ //v l->s
        if(sumk == k || sumw == m) {
            cout<<res_count<<endl;
            return 0;
        }
        for(auto prices : it->second){ // price s->l
            if(sumk + prices.first > k) {
                break;
            }else{
                if(sumk + it->first <= k && sumw + prices.second.top() <= m){ //put weight s->l
                    res_count++;
                    sumk += it->first;
                    sumw += prices.second.top();
                    prices.second.pop();
                } else {
                    break;
                }
            }
        }
    }
    cout<<res_count<<endl;
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值