EOJ 3215 奶牛优惠券「优先队列」

题目简介


约翰需要买更多的奶牛!市场上有 N 头奶牛待售,第 i 头奶牛的原价是 Pi 元。约翰只有 M 元,不过他还有 K 张优惠券。如果他在买第 i 头牛的时候使用一张优惠券,那么就可以享受一个折扣价 Ci。每头牛只能使用一次优惠券,请问约翰最多能买多少头奶牛回家?

说明


用STL优先队列写会略微方便一些,不过还是挺麻烦的。优惠券当然是用在价格差尽可能大的牛身上啦。

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct cow {int ori, discnt;};
vector<cow> v;
priority_queue<int, vector<int>, greater<int> > q;

int cmp1(cow a, cow b)
{
    if (a.discnt != b.discnt) return a.discnt > b.discnt;
    return a.ori < b.ori;
}

int cmp2(cow a, cow b)
{
    return a.ori < b.ori;
}

int main()
{
    int n, m, ans = 0;
    ll money;
    cin >> n >> m >> money;
    while (n--) {
        cow tmp;
        cin >> tmp.ori >> tmp.discnt;
        v.push_back(tmp);
    }
    sort(v.begin(), v.end(), cmp1);
    while (m--) {
        money -= v.back().discnt;
        if (money >= 0) {
            q.push(v.back().ori - v.back().discnt);
            v.pop_back();
            ++ans;
        }else {cout << ans << endl; return 0;}
    }
    sort(v.begin(), v.end(), cmp2);
    for (vector<cow>::iterator it = v.begin(); it != v.end(); ++it) {
        if ((*it).ori - (*it).discnt < q.top()) money -= (*it).ori;
        else {
            money -= q.top() + (*it).discnt;
            q.pop();
            q.push((*it).ori - (*it).discnt);
        }
        if (money >= 0) ++ans;
        else break;
    }
    cout << ans << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值