zoukankan      html  css  js  c++  java
  • 优惠买商品(dp、greedy)

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

    题意:给你n件商品,有p元,k优惠。有如下两种购买方式:

    1、直接购买某商品花费该商品价格

    2、买k个商品,只需支付k个中最贵的一个。

    解法:排序,求前缀和,贪心尽可能的使用方式2.

    //#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 ans[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 ;
    }
    
  • 相关阅读:
    update数据从一个表到另外一个表中
    数据泵导出
    导入库
    看函数
    导库中的一个表
    一个表的字段存在或者不存在另一表里
    语句2
    语句
    word 内容被锁定,无法修改
    gridview自带分页
  • 原文地址:https://www.cnblogs.com/nonames/p/12239436.html
Copyright © 2011-2022 走看看