K for the Price of One (DP Greedy)CodeForces - 1282B1/2

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 "kk of goods for the price of one" is held in store.

Using this offer, Vasya can buy exactly kk 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 aiai — the number of coins it costs. Initially, Vasya has pp 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 ii if he currently has enough coins (i.e paip≥ai). After buying this good, the number of Vasya's coins will decrease by aiai, (i.e it becomes p:=paip:=p−ai).
  • Vasya can buy a good with the index ii, and also choose exactly k1k−1 goods, the price of which does not exceed aiai, if he currently has enough coins (i.e paip≥ai). Thus, he buys all these kk goods, and his number of coins decreases by aiai (i.e it becomes p:=paip:=p−ai).

Please note that each good can be bought no more than once.

For example, if the store now has n=5n=5 goods worth a1=2,a2=4,a3=3,a4=5,a5=7a1=2,a2=4,a3=3,a4=5,a5=7, respectively, k=2k=2, and Vasya has 66 coins, then he can buy 33 goods. A good with the index 11 will be bought by Vasya without using the offer and he will pay 22 coins. Goods with the indices 22 and 33 Vasya will buy using the offer and he will pay 44 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.

Input

The first line contains one integer tt (1t1041≤t≤104) — the number of test cases in the test.

The next lines contain a description of tt test cases.

The first line of each test case contains three integers n,p,kn,p,k (2n21052≤n≤2⋅105, 1p21091≤p≤2⋅109, 2kn2≤k≤n) — the number of goods in the store, the number of coins Vasya has and the number of goods that can be bought by the price of the most expensive of them.

The second line of each test case contains nn integers aiai (1ai1041≤ai≤104) — the prices of goods.

It is guaranteed that the sum of nn for all test cases does not exceed 21052⋅105.

Output

For each test case in a separate line print one integer mm — the maximum number of goods that Vasya can buy.

Example

Input

8
5 6 2
2 4 3 5 7
5 11 2
2 4 3 5 7
3 2 3
4 2 6
5 2 3
10 1 3 9 2
2 10000 2
10000 10000
2 9999 2
10000 10000
4 6 4
3 2 3 2
5 5 3
1 2 2 1 2

Output

3
4
1
1
2
0
4
5
这道题意思是说你购买东西有两种可能——买一个付一个的钱,买一个送k - 1个,其中k - 1的价格都要小于这个。
我们买东西要买的数量最多,那我们肯定挑便宜的买,
所以我们这道题可以等价于按价格排完序后从前往后最多能带走多少个东西(前面的如果带不走,后面的更贵,怎么可能带着走)。
由于买一送k - 1,我们对价格排个序,我们知道,如果买一个东西,送k - 1个,你肯定要送的k - 1是从买的那个东西往前k - 1个吧(因为能免费的带走的只能是不比它贵的,那么肯定是带走之前的,使得带走的价格最大化)。所以我们可以把k个看成一部分,对于买了不送的方式,买贵的肯定不划算,那肯定是买便宜的,那么我们就可以先考虑买了不送的个数,然后接下来k个直接拿这k个中最贵的买下来,就能买完这些,我们可以一直累加到价格超过p,就说明我们不能买了,然后我们对这些情况找出其中的最大值。
注意,众所周知,如果用买了不送的方法买到k个,还不如直接买第k个划算,等价于买了不送的方法买0个,接下来全是买1得k的方法,所以我们枚举买了不送的方法只要从0遍历到k - 1即可。
AC代码:
#include <bits/stdc++.h>
using namespace std;
int a[200005];
int main(void)
{
    int t;
    int n, p, k;
    scanf("%d", &t);
    for(int cnt = 0; cnt < t; cnt++)
    {
        int maxn = 0;
        scanf("%d %d %d", &n, &p, &k);
        for(int i = 0; i < n; i++)
        {
            scanf("%d", &a[i]);
        }
        sort(a, a + n);
        int pre = 0;//pre记录用买了不送的方法买了前面的j个所要的花费
        for(int j = 0; j < k; j++)
        {
            int num = j;//买的个数
            int sum = pre;//前j个采用买了不送的方法,其余采用送k-1的价格
            if(sum > p)//可能出现用买了不送的方法买k个之内都能使得价格大于p,此时就一定不会最多(因为已经不符合题意了sum都大于p)直接break
                break;
            for(int z = j + k - 1; z < n; z += k)
            {
                if(a[z] + sum <= p)
                {
                    num += k;
                    sum += a[z];
                }
                else
                    break;
            }
            maxn = max(num, maxn);
            pre += a[j];
        }
        printf("%d\n", maxn);
    }
    return 0;
}

关于DP方法,我们可以考虑dp[i]为买前i种的最小花费,易得dp[i] = min(dp[i - 1] + a[i] , dp[i - k] + a[i]);代码思路详见:https://www.cnblogs.com/pixel-Teee/p/12098715.html

 
posted @ 2020-02-19 21:05  funforever  阅读(103)  评论(0编辑  收藏  举报