K for the Price of One
Posted cadcadcad
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K for the Price of One相关的知识,希望对你有一定的参考价值。
B. K for the Price of One (Hard Version)
赛时失手推错了规律...
这个题不是单调递增的
但是它有一个规律:当买同样多的东西时,优先买便宜的
所以我们可以求出买 i 个东西时最便宜的价格
sort(a+1,a+n+1); for(int i=1;i<=k-1;++i) dp[i]=a[i]+dp[i-1]; for(int i=k;i<=n;++i) dp[i]=dp[i-k]+a[i];
因为考虑到 n 只有2e5的范围,所以把每一个(dp[i])都遍历一遍找到最大值即可
代码:
// Created by CAD on 2020/1/13.
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2e5+5;
ll a[maxn];
ll dp[maxn];
ll n,p,k;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin>>t;
while(t--)
{
cin>>n>>p>>k;
for(int i=1;i<=n;++i)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=k-1;++i)
dp[i]=a[i]+dp[i-1];
for(int i=k;i<=n;++i)
dp[i]=dp[i-k]+a[i];
int ans=0;
for(int i=1;i<=n;++i)
if(dp[i]<=p)
ans=max(ans,i);
cout<<ans<<endl;
}
return 0;
}
以上是关于K for the Price of One的主要内容,如果未能解决你的问题,请参考以下文章
CF #610Div2 B2.K for the Price of One (Hard Version) (dp解法 && 贪心解法)
错误处理:one of the variables needed for gradient computation has been modified by inplace operation(代码片
Warning: Permanently added the RSA host key for IP address ‘13.250.177.223‘ to the list of known(代码片
[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}(代码片
一道题目- Find the smallest range that includes at least one number from each of the k lists
you need (at least one of) the RELOAD privilege(s) for this operation