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;
    }
    
  • 相关阅读:
    Android摄像头採集的视频数据流怎样通过Socket实时发送到目标服务端
    linux c语言 select函数使用方法
    【CODEFORCES】 B. Dreamoon and Sets
    Cocos2d-x 3.0final 终结者系列教程15-win7+vs2012+adt+ndk环境搭建(无Cygwin)
    error MSB8011: 未能注冊输出。请尝试启用“逐用户重定向”
    Web服务(Web Service)相关概念
    HDU 5186 zhx&#39;s submissions (进制转换)
    GPS项目小结
    VS2008 格式化时候乱码 或者 为全为0
    JAX-WS HandlerChain使用具体解释
  • 原文地址:https://www.cnblogs.com/YDDDD/p/12098816.html
Copyright © 2011-2022 走看看