Offer to buy goods (dp, greedy)

https://codeforces.com/contest/1282/problem/B2

Meaning of the questions: give you n items, with p-yuan, k concessions. There are two ways to buy:

1, the direct cost of purchasing a commodity that commodity prices

2, the k buy goods, pay only the k most expensive.

Solution: sequencing, seeking prefixes and, greedy use 2 as much as possible.

//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF  0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int a[200009];
int year [200009]

int main ()
{
    int t ;
    scanf("%d" , &t);
    while(t--)
    {
        int n , p , k ;
        scanf("%d%d%d" , &n , &p , &k);
        memset(ans , 0 , sizeof(ans));
        for(int i = 1 ; i <= n ; i++)
        {
            scanf("%d" , &a[i]);
        }
        sort(a + 1 , a + n + 1);
        int num = 0;
        for(int i = 1 ; i <= n ; i++) ans[i] = ans[i-1] + a[i];
        for(int i = k ; i <= n ; i++) ans[i] = ans[i - k] + a[i];
        for(int i = 1 ; i <= n ; i++)
        if(ans[i] <= p)
        num = i ;
        cout << num << endl ;
    }

    return 0 ;
}

 

Guess you like

Origin www.cnblogs.com/nonames/p/12239436.html