• 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;
    }
    
  • 相关阅读:
    kafka整理笔记笔记
    node(一)安装nodejs最新版到debian,ubuntu,mint系统
    ubuntu16.04安装visual-studio-code
    ubuntu16.04更新内核--使用4.6以上的内核会让用A卡的Dell电脑更快--及卸载多余内核
    linux查看主板型号及内存硬件信息,及硬盘测速
    git使用,在ubuntu中
    Ubuntu中文目录文件夹改为英文
    w3m 在ubuntu中的使用
    关于右键属性与du -sh显示的文件大小不一致的解决
    ubuntu16.04安装chrome
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12098816.html
Copyright © 2020-2023  润新知