zoukankan      html  css  js  c++  java
  • Codeforces Round #610 (Div. 2) B2. K for the Price of One (Hard Version) (DP)

    链接:

    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;
    }
    
  • 相关阅读:
    修改element ui默认样式
    MyUI是美亚柏科旗下新德汇出品的Web前端一站式项目工程框架。
    细谈 axios和ajax区别
    Window MySQL远程连接不上的解决
    为什么要前后端分离?各有什么优缺点?
    原生js
    node.js安装及环境配置之Windows篇
    IDEA 导入若依管理系统
     Java图形验证码,支持gif、中文、算术等类型,可用于Java Web、JavaSE等项目。
    获取git仓库时更新类型update type 的选择
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12098816.html
Copyright © 2011-2022 走看看