P6002 [USACO20JAN] Berry Picking S题解

· · 题解

传送门

思路

  1. 因为树上的浆果数 \le1000,所以我们可以枚举浆果数量(设为 m)。

  2. 在每棵树上尽可能多地摘 m 个浆果放在篮子中。

  3. 将每棵树上剩下的浆果数量进行排序,从大到小放进还没有浆果的篮子内。

  4. 枚举 m,记录 Bessie 最多可以收获得浆果的数量,最后取最大值作为结果。

代码:

#include<bits/stdc++.h>
using namespace std;
long long n,m,a[1005],big,ge,b[1005],e,geshu,shao,shu;
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    shao=m/2;
    for(int g=1;g<=n;g++){
        cin>>a[g];
    }
    sort(a+1,a+n+1);
    for(int g=1;g<=1000;g++){
        ge=0;
        for(int h=n;h>=1;h--){
            if(a[h]>=g){
                ge+=a[h]/g;
                b[h]=a[h]%g;
            }
            else b[h]=a[h];
        }
        sort(b+1,b+n+1);
        e=0;
        shu=0;
        if(ge>shao){
            e=min(shao,ge-shao)*g;
            shu=ge-shao;
            if(shu>=shao){
                big=max(e,big);goto ew;
            }
        }
        for(int h=n;h>=1;h--){
            if(ge<shao){
                ge++;
            }
            else{
                shu++;
                e+=b[h];
                if(shu==shao)break;
            }
        }
        big=max(big,e);
        ew:
            big=big;
    }
    cout<<big<<"\n";
}