当前位置: 首页 > 工具软件 > Berry > 使用案例 >

Berry Picking(思维+模拟)

公西姚石
2023-12-01

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.

样例输入 Copy

5 4
3 6 8 4 2

 

 

样例输出 Copy

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.
题解:此问题关键在于如何分配能使四个篮子更平均。
由于我们每棵树的果子数最大是1000.
所以我们可以考虑暴力模拟来解决问题。
每次枚举篮子能放最大的果子数,1-1000;设为i
如果k个篮子都能放i。答案更新i*k/2;
如果有篮子放不了i。那么此时每棵树最多有i-1个果子。
剩余篮子从大往小放就行。答案更新后面较小的k/2个数的和。
关于为i时,最多能放几个篮子,显然是sum(b[j]/i);
emm
多写题解。
多思考
多补题。
#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;
}

 

 类似资料: