CF1282B2 K for the Price of One (Hard Version)

洛谷链接:

K for the Price of One (Hard Version) - 洛谷 

(本题还有一个easy version,用这个代码也能过)

思路:

思路是贪心和dp递推。

不难发现,要买数量最多的商品,最优解必定是拿走最便宜一部分商品(包括买的和送的),所以从便宜的到贵的逐个遍历,如果当前数量<k,必须全买,如果>=k,挑最贵的免单。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5+5;
int t, n, p, k; //询问次数,商品数,硬币数,捆绑购买数
int cost[maxn];
int dp[maxn]; //dp[i]表示买下前i个物品需要的最少钱数
int main(){
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin >> t;
    while(t--){
        cin >> n >> p >> k;
        for(int i=1; i<=n; i++)
            cin >> cost[i];
        sort(cost+1, cost+n+1);
        for(int i=1; i<=n; i++){
            //如果商品数 < k,无法免单,必须全买
            if(i<k) dp[i] = dp[i-1] + cost[i];
            //如果商品数 >= k,可以免单,贪心地选最贵的k-1个免单
            else dp[i] = dp[i-k] + cost[i];
        }
        int ans = 0;
        for(int i=1; i<=n; i++){ //扫描一遍,找出能买的最多的物品数
            if(dp[i] <= p) ans = i;
        }
        cout << ans << '\n';
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值