This is the hard version of this problem. The only difference is the constraint on kk — the number of gifts in the offer. In this version: 2≤k≤n2≤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 "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 p≥aip≥ai). After buying this good, the number of Vasya's coins will decrease by aiai, (i.e it becomes p:=p−aip:=p−ai).
- Vasya can buy a good with the index ii, and also choose exactly k−1k−1 goods, the price of which does not exceed aiai, if he currently has enough coins (i.e p≥aip≥ai). Thus, he buys all these kk goods, and his number of coins decreases by aiai (i.e it becomes p:=p−aip:=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.
The first line contains one integer tt (1≤t≤1041≤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 (2≤n≤2⋅1052≤n≤2⋅105, 1≤p≤2⋅1091≤p≤2⋅109, 2≤k≤n2≤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 (1≤ai≤1041≤ai≤104) — the prices of goods.
It is guaranteed that the sum of nn for all test cases does not exceed 2⋅1052⋅105.
For each test case in a separate line print one integer mm — the maximum number of goods that Vasya can buy.
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
3 4 1 1 2 0 4 5
题目大意:
刚开始有 p 块钱,商店有 n 件物品,你每次可以只买一件付那一件的钱,也可以买 k 件支付最贵那件的钱,问你最多能买几件
首先我们要明确,如果你买了这一件商品,那么你一定买了比这件商品价格低的所有商品,因为这样买花的钱才会更少,方法才是最优的。
解法一:
这道题用 背包dp 解应该是最直观的,先排序一下,状态要么由前一件转移过来,要么由前 k 件转移过来,即 dp[i] = min(dp[i-1]+a[i],dp[i-k]+a[i])
然后我们只要找到买哪一件之前花的钱 <=p 就好了。
1 #include <iostream>
2 #include <algorithm>
3 using namespace std;
4 const int N = 2e5 + 5;
5 int t, n, p, k, ans, a[N], dp[N];
6 int main()
7 {
8 ios::sync_with_stdio(false);
9 cin >> t;
10 while (t--)
11 {
12 cin >> n >> p >> k;
13 for (int i = 1; i <= n; i++)
14 cin >> a[i];
15 sort(a + 1, a + n + 1);
16 ans = 0;
17 for (int i = 1; i <= n; i++)
18 {
19 dp[i] = dp[i - 1] + a[i];
20 if (i >= k)
21 dp[i] = min(dp[i], dp[i - k] + a[i]);
22 if (dp[i] <= p)
23 ans = max(ans, i);
24 }
25 cout << ans << '\n';
26 }
27 return 0;
28 }
// 93ms
解法二:
其实很多人最开始想到的都是贪心,也是可以用贪心解的。我们不难发现,尽量使用第二个技能永远是最佳的,一旦第二个技能不能再使用,剩下的钱我们就一个一个去买前面价格低的。所以我们只要枚举前 k-1 个单买( >=k 个的话我们只要用第二个技能买一定更划算),后面的我们全部用第二个技能买就行了,维护可以买到的数量最大值就好了。
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int N = 2e5 + 5; 5 int t, n, p, k, a[N]; 6 int main() 7 { 8 ios::sync_with_stdio(false); 9 cin >> t; 10 while (t--) 11 { 12 cin >> n >> p >> k; 13 for (int i = 0; i < n; i++) 14 cin >> a[i]; 15 sort(a, a + n); 16 int now = 0, pre = 0, ans = 0, cnt = 0; 17 for (int i = 0; i < k; i++) 18 { 19 now = pre; 20 cnt = i; 21 if (now > p) 22 break; 23 for (int j = i + k - 1; j < n; j += k) 24 { 25 now += a[j]; 26 if (now <= p) 27 cnt += k; 28 else 29 break; 30 } 31 ans = max(ans, cnt); 32 pre += a[i]; 33 } 34 cout << ans << '\n'; 35 } 36 return 0; 37 }
// 108ms