先排序。去重。
发现最大值就是全选最大的饼干。然后思考将最大的饼干替换成较小的,那么很好想的是换成次大饼干。显然把当前的一块换成后面的任何的饼干损失都不如换成下一块。
把问题普通化。假设我现在选了若干个第 块,那么有两种选择,把第 种饼干的其中一个换成第 种,或把第 种的一块换成第 种。显然换多了肯定总和不优,每次换一个即可。然后把这两种情况扔进堆,每次取出堆顶输出并操作即可。
注意要统计上一种饼干和这一种饼干选了几个,如果这一种已经选的数量超过了上一种的数量,那么转移不合法。如果没有这一种饼干,就不能换成下一种。记录差值每次加上就可以实现方案的转移。
#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;
}