P3045 [USACO12FEB] Cow Coupons G ( 反悔堆

#include <bits/stdc++.h>
using namespace std;
using VI = vector<int>;
using ll = long long;
using PII = pair<int,int>;
const int mod = 19650827;
int n,k;
ll m;
int a[50010];
int b[50010];
int del[50010];

int main(){
    cin>>n>>k>>m;
    priority_queue<PII,vector<PII>,greater<PII>>p1,p2;
    priority_queue<int,vector<int>,greater<int>>p3;
    for(int i=1;i<=n;i++) {
        cin>>a[i]>>b[i];
        p1.push({a[i],i});
        p2.push({b[i],i});
    }

    for(int i=1;i<=k;i++) p3.push(0);
    ll res = 0;
    int ct = 0;
    while (ct < n){
        while(del[p1.top().second] == 1) p1.pop();
        while(del[p2.top().second] == 1) p2.pop();
        auto x = p1.top();
        auto y = p2.top();
        auto z = p3.top();
        if(x.first > y.first + z){
            del[y.second] = 1;
            p2.pop();
            int d = a[y.second] - b[y.second];
            p3.pop();
            p3.push(d);
            res += y.first + z;
        }else{
            del[x.second] = 1;
            p1.pop();
            res += x.first;
        }
        if(res > m){
            break;
        }else{
            ct++;
        }
    }

    cout<<ct;
}

利用差值来表示更换优惠卷的操作

比如你现在已经用了k张优惠卷,如果你想要用更换优惠卷,就用补差价,

如果补的差价+物品的优惠值 <= 直接再买一个(和前面的物品并不是同一个)  那就直接不用优惠卷买

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值