题解 [USACO20JAN] Berry Picking

· · 题解

假设 Elsie 拿的篮子里面果子数最小值为 m,那么最好情况是她拿的篮子里全都是 m 个果子。

我们可以从 1 到 \max_{i = 1}^{N}{B_i}枚举这个 m,令能装满的(也就是装了 m 个果子的)篮子数为 L。

分类讨论:

时间复杂度 O(\max_{i = 1}^{N}{B_i} \cdot N \log N)。

Code:

// template <typename typ> 
// inline bool up(typ &x, const typ &y) {
// return x < y ? x = y, 1 : 0; }

const int maxn = 1005;
int n, k, a[maxn], mx, mod, ans;

inline bool cmp(int p, int q) {
    return p % mod > q % mod;
}

signed main() {
    orh::useios();

    cin >> n >> k;
    for (rg int i = 1; i <= n; ++i) {
        cin >> a[i];
        up(mx, a[i]);
    }

    for (rg int t = 1; t <= mx; ++t) {
        int full = 0;
        for (rg int i = 1; i <= n; ++i)
            full += a[i] / t;
        if (full < (k >> 1)) break;
        if (full >= k) {
            up(ans, t * k >> 1);
            continue;
        }

        mod = t;
        sort(a + 1, a + n + 1, cmp);
        int tmp = (full - (k >> 1)) * t;
        for (rg int i = 1; i <= n && i + full <= k; ++i)
            tmp += a[i] % t;
        up(ans, tmp); 
    }

    cout << ans << endl;

    return 0;
}