CF1539D. PriceFixed —— 贪心

这篇博客讨论了一种商品购买问题,其中每种商品原价2元,达到一定数量后可享受折扣。博主提出了一个排序和累计计数的方法来最小化购买成本,通过遍历商品并考虑其优惠条件,确定最优购买方案。

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

​​​​​​codeforces

题意:

市场上有n种商品,每种商品的价格都是2。现在你需要买第 i 种商品 a[ i ] 件。但是对于第 i 种商品有一个属性 b ,意味着如果你已经买了 b 件商品(不一定是这一种商品),那么此商品打折,价格会降到 1。

你需要最小化你的代价。

思路:

把每种物品按照b从小到大排序,并记录物品总和一共有cnt件。ans记录当前有多少件优惠。然后从最后一项开始考虑, 显然 cnt-当前物品的b值 是当前的优惠件数上限。 如果ans <  cnt - 当前物品的b, 那么有两种情况 1.它的a件商品可以全部优惠 2.它的a件商品不能全部优惠(因为如果ans+a大于当前允许优惠的件数,就不能优惠了)。 所以ans = min( ans + p[i].a, cnt - p[i].b) 分别对应2种情况。 最后的答案即为cnt*2-ans。

// Decline is inevitable,
// Romance will last forever.
#include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <deque>
#include <vector>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define vi vector<int>
#define endl '\n'
#define int long long
using namespace std;
const int P = 1e9 + 7;
const int maxn = 1e5  + 10;
const int maxm = 2e6 + 10;
const int INF = 0x3f3f3f3f;
template<class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
template<class T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
struct node {
    int a, b;
    bool operator < (const node &x) const {
        return b < x.b;
    }
}p[maxn];
int cnt;
void solve() {
    int n;
    cin >> n;
    for(int i  = 1; i <= n; i++) {
        cin >> p[i].a >> p[i].b;
        cnt += p[i].a;
    }
    int ans = 0;
    sort(p + 1, p + n + 1);
    for(int i = n; i >= 1; i--) {
        if(ans < cnt - p[i].b) {
            ans = min(cnt - p[i].b, ans + p[i].a);
        }
    }
    cout << (cnt << 1) - ans << endl;
}
signed main() {
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//    int T; cin >> T;while(T--)
    solve();
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值