zoukankan      html  css  js  c++  java
  • Codeforces 1282B2 K for the Price of One (Hard Version)

    题目链接:

    1282B2 K for the Price of One (Hard Version)

    思路:

    首先排序,再设dp[i]是index从0买到i为止的最小开销,然后以贪心思想递推;
    最后求满足dp[i]<=p的最大i即可

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    typedef pair<int,int> P;
    typedef long long LL;
    #define fi first
    #define sc second
    #define pb(a) push_back(a)
    #define mp(a,b) make_pair(a,b)
    #define pt(a) cerr<<a<<"---
    "
    #define rp(i,n) for(int i=0;i<n;i++)
    #define rpn(i,n) for(int i=1;i<=n;i++)
    int main(){
    	ios::sync_with_stdio(false); cin.tie(nullptr);
    	int t; cin>>t;
    	while(t--){
    		int n,p,k; cin>>n>>p>>k;
    		vector<int> a(n);
    		rp(i,n) cin>>a[i];
    		sort(a.begin(),a.end());
    		vector<int> dp(n);
    		rp(i,n){
    			if(i>=k-1) dp[i]=(i>=k?dp[i-k]:0)+a[i];
    			else dp[i]=(i?dp[i-1]:0)+a[i];
    		}
    		int ans=-1;
    		rp(i,n) if(dp[i]<=p) ans=i;
    		cout<<ans+1<<'
    ';
    	}
    	return 0;
    }
    
  • 相关阅读:
    初识计算机
    前端html css
    mysql高级
    mysql多表查询
    mysql数据库查询
    mysql表关系
    mysql数据类型
    mysql数据库介绍
    异步回调 协程
    GIL-全局解释器锁
  • 原文地址:https://www.cnblogs.com/yuhan-blog/p/12308723.html
Copyright © 2011-2022 走看看