ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #610 (Div. 2) B2. K for the Price of One (Hard Version) (DP)

2019-12-25 20:57:08  阅读:437  来源: 互联网

标签:Vasya goods Price Hard Codeforces buy coins Dp he


链接:

https://codeforces.com/contest/1282/problem/B2

题意:

This is the hard version of this problem. The only difference is the constraint on k — the number of gifts in the offer. In this version: 2≤k≤n.

Vasya came to the store to buy goods for his friends for the New Year. It turned out that he was very lucky — today the offer "k of goods for the price of one" is held in store.

Using this offer, Vasya can buy exactly k of any goods, paying only for the most expensive of them. Vasya decided to take this opportunity and buy as many goods as possible for his friends with the money he has.

More formally, for each good, its price is determined by ai — the number of coins it costs. Initially, Vasya has p coins. He wants to buy the maximum number of goods. Vasya can perform one of the following operations as many times as necessary:

Vasya can buy one good with the index i if he currently has enough coins (i.e p≥ai). After buying this good, the number of Vasya's coins will decrease by ai, (i.e it becomes p:=p−ai).
Vasya can buy a good with the index i, and also choose exactly k−1 goods, the price of which does not exceed ai, if he currently has enough coins (i.e p≥ai). Thus, he buys all these k goods, and his number of coins decreases by ai (i.e it becomes p:=p−ai).
Please note that each good can be bought no more than once.

For example, if the store now has n=5 goods worth a1=2,a2=4,a3=3,a4=5,a5=7, respectively, k=2, and Vasya has 6 coins, then he can buy 3 goods. A good with the index 1 will be bought by Vasya without using the offer and he will pay 2 coins. Goods with the indices 2 and 3 Vasya will buy using the offer and he will pay 4 coins. It can be proved that Vasya can not buy more goods with six coins.

Help Vasya to find out the maximum number of goods he can buy.

思路:

Dp(i,0)表示i这个点单独买,一共买了i个花的最少钱。
Dp(i,1)表示i这个点一次买k个,一共买了i个花的最少钱。
对于i<k的点,两者相等,直接累计,i >= k的点,Dp(i,0) = val[i]+min(Dp(i-1,0),Dp(i-1,1)),Dp(i,1) = val[i]+min(Dp(i-k,0),Dp(i-k,1))

代码:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5+10;

int val[MAXN], Dp[MAXN][2];
int n, p, k;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        memset(Dp, 0, sizeof(Dp));
        cin >> n >> p >> k;
        for (int i = 1;i <= n;i++)
            cin >> val[i];
        sort(val+1, val+1+n);
        for (int i = 1;i <= n;i++)
        {
            if (i < k)
            {
                Dp[i][0] = Dp[i-1][0] + val[i];
                Dp[i][1] = Dp[i][0];
            }
            else
            {
                Dp[i][0] = val[i]+min(Dp[i-1][0], Dp[i-1][1]);
                Dp[i][1] = val[i]+min(Dp[i-k][0], Dp[i-k][1]);
            }
        }
        int ans = 0;
        for (int i = 1;i <= n;i++)
        {
            if (p >= min(Dp[i][0], Dp[i][1]))
                ans = max(ans, i);
        }
        cout << ans << endl;
    }

    return 0;
}

标签:Vasya,goods,Price,Hard,Codeforces,buy,coins,Dp,he
来源: https://www.cnblogs.com/YDDDD/p/12098816.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有