跳到正文

题解

题解:AT_abc440_e [ABC440E] Cookies

N1tr0us_Acid约 4 分钟
原文
文章信息与版本

文章信息

发布
2026年1月13日
收录
2026年5月28日
更新
2026年10月27日
篇幅
约 4 分钟
获赞
7

版本

  1. v1当前

先排序。去重。

发现最大值就是全选最大的饼干。然后思考将最大的饼干替换成较小的,那么很好想的是换成次大饼干。显然把当前的一块换成后面的任何的饼干损失都不如换成下一块。

把问题普通化。假设我现在选了若干个第 ii 块,那么有两种选择,把第 i−1i-1 种饼干的其中一个换成第 ii 种,或把第 ii 种的一块换成第 i+1i+1 种。显然换多了肯定总和不优,每次换一个即可。然后把这两种情况扔进堆,每次取出堆顶输出并操作即可。

注意要统计上一种饼干和这一种饼干选了几个,如果这一种已经选的数量超过了上一种的数量,那么转移不合法。如果没有这一种饼干,就不能换成下一种。记录差值每次加上就可以实现方案的转移。

C++
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
int n, k, x;
const int N = 55;
int a[N], cha[N];
struct node {
    int now, las, cnt, lim;
    bool operator < (const node &W) const {
        return now < W.now;
    }
};
priority_queue <node, vector <node>, less <node> > q;
signed main(void) {
    ios :: sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> k >> x;
    for (int i = 1; i <= n; i ++) cin >> a[i];
    sort(a + 1, a + n + 1);
    reverse(a + 1, a + n + 1);
    int flag = 0;
    for (int i = 1; i < n; i ++) cha[i] = a[i] - a[i + 1];
    flag = accumulate(cha + 1, cha + n, 0ll);
    if(!flag) {
        for (int i = 1; i <= x; i ++) cout << k * a[1] << endl;
        return 0;
    }
    cout << k * a[1] << endl;
    x --;
    if(x) {
        int pos = 1, summ = k * a[1];
        while (pos < n && cha[pos] == 0) pos ++;
        if(pos < n) q.push({summ - cha[pos], pos, 1, k});
        while (x && q.size()) {
            auto u = q.top();
            q.pop();
            cout << u.now << endl;
            if(u.cnt + 1 <= u.lim) q.push({u.now - cha[u.las], u.las, u.cnt + 1, u.lim}); // 只有当前种的数量小于刚转移过来时的上一种选的数量时才可以转换。
            int nxt = u.las + 1;
            while (nxt < n && cha[nxt] == 0) nxt ++;
            if(nxt < n) q.push({u.now - cha[nxt], nxt, 1, u.cnt}); // 把 u.cnt 塞进去,表示下一种饼干最多只能转换这么多。
            x --;
        }
    }
    return 0;
}
原文
存档评论2 条抓取于