Berry Picking(思维)

题目描述
Bessie and her little sister Elsie are picking berries in Farmer John’s berry patch. Farmer John’s patch has exactly N berry trees (1≤N≤1000); tree i contains exactly Bi berries (1≤Bi≤1000). Bessie has exactly K baskets (1≤K≤1000, K even). Each basket can hold as many berries from a single tree as Bessie wants, but cannot contain berries from two different trees as their flavors will clash with each other. Baskets may remain empty.
Bessie wants to maximize the number of berries she collects. However, Farmer John wants Bessie to share with her little sister, and so Bessie will have to give Elsie the K/2 baskets with the largest number of berries. This means that Elsie may even end up with more berries than Bessie, which is very unfair, but unfortunately, sibling dynamics are not always fair.

Help Bessie figure out the maximum number of berries she can collect.

输入
The first line of input contains space-separated integers N and K.
The second line contains N space-separated integers B1,B2,…,BN.

输出
A single line with the answer.

样例输入
5 4
3 6 8 4 2

样例输出
8

提示
If Bessie fills
·one basket with 6 berries from tree 2
·two baskets, each with 4 berries from tree 3
·one basket with 4 berries from tree 4
then she receives two baskets each with 4 berries, giving her 8 berries in total.

思路
对于每种取法,其都具有一个基础值x,且x在[1,maxb]之间,枚举区间内的每一个x,即可得到其能取到的所有桶数temp,若temp<k/2,那么Bessie能拿到的个数恒等于0,那么就不需要对之后的值进行遍历,若temp>k/2 且 temp<k,这个区间内的值为所取得的(temp-k/2)*x+能用剩下的桶装下的剩余的果子,当temp>=k时,能拿到的个数为x*(k/2)

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include<bits/stdc++.h>
using namespace std;
typedef long 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值