• B2. K for the Price of One (Hard Version)


    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: 2kn2≤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 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
    Copy
    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
    Copy
    3
    4
    1
    1
    2
    0
    4
    5
    
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define pb              push_back
    #define mk              make_pair
    using namespace std;
    int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
    int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1000000007;
    const int N = 1005;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    
    struct node
    {
        int p, n;
    };
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            ll n, p, k;
            cin >> n >> p >> k;
            vector<int> a(n+1),dp(n+1,0);
            for (int i = 1; i <= n; i++)
            {
                cin >> a[i];
            }
            int res = 0;
            sort(a.begin()+1, a.end());
            for (int i = 1; i <= n; i++)
            {
                if (i < k)
                    dp[i] = dp[i - 1] + a[i];
                else
                    dp[i] = dp[i - k] + a[i];
            }
            rep(i, 1, n)
            {
                if (dp[i] <= p)
                    res = i;
            }
            cout << res << endl;
        }
        return 0;
    }
  • 相关阅读:
    Javascript 正确用法 二
    c# 未能载入文件或程序集
    Linux系统备份
    环保创业的可行之道——Leo鉴书上66
    Oracle的序列
    UVA 10574
    网页内容的html标签补全和过滤的两种方法
    使用POI来实现对Excel的读写操作
    OVER(PARTITION BY)函数介绍
    Kill 正在执行的存储过程
  • 原文地址:https://www.cnblogs.com/dealer/p/12824869.html
Copyright © 2020-2023  润新知