Berry Picking(思维+模拟)
题目描述
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 second line contains N space-separated integers B1,B2,…,BN.
输出
样例输入 Copy
5 4
3 6 8 4 2
样例输出 Copy
8
提示
·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.
#pragma GCC optimize(3 , "Ofast" , "inline")
#include <bits/stdc++.h>
#define rep(i , a , b) for(register int i=(a);i<=(b);i++)
#define rop(i , a , b) for(register int i=(a);i<(b);i++)
#define per(i , a , b) for(register int i=(a);i>=(b);i--)
#define por(i , a , b) for(register int i=(a);i>(b);i--)
using namespace std;
typedef long long ll;
typedef pair<int,int> pi;
const int maxn = 1111;
int n,k;
int b[maxn];
int a[maxn];
template<class T>
inline void read(T &ret) {
char c;
ret=0;
while((c=getchar())<'0'||c>'9');
while(c>='0'&&c<='9') {
ret=ret*10+(c-'0'),c=getchar();
}
}
priority_queue<int>q;
int main () {
read (n);
read (k);
rep (i,1,n) read (b[i]);
int ans = 0;
rep (i,1,1000) {
int sum = 0;
int tmp = 0;
rep (j,1,n) {
sum=sum+b[j]/i;
}
if(sum>=k) {
ans=max (ans,i*k/2);
}
else {
while (!q.empty ()) q.pop ();
rep (j,1,sum) a[j]=i;
rep (j,1,n) {
if(b[j]%i!=0) {
q.push ( b[ j ] % i);
}
}
rep (j,sum+1,k) {
if(!q.empty ()) {
a[j]=q.top ();
q.pop ();
} else a[j]=0;
}
rep (j,k/2+1,k) {
tmp+=a[j];
}
ans=max (ans,tmp);
}
}
cout<<ans<<endl;
return 0;
}