题解:AT_abc440_e [ABC440E] Cookies
题目大意
有
思路
最直接的思路是枚举所有组合:用
我们需要找到一种方法,按总和从大到小的顺序逐个生成方案,并且只生成前
因为每种饼干无限多,显然总和最大的方案就是全部
从最大方案出发,想得到总和稍微小一点的方案,直觉上应该是把一块美味值较高的饼干换成美味值较低的饼干。
具体来说,从最大方案(全部选
我们可以推广这个思路得到:任何一个方案,都可以通过把其中一块饼干替换成美味值更低的饼干,得到一个总和更小的方案。
如果每次只替换一块饼干,我们可以得到一个"状态转移"的图:
每个状态是一个方案(
从状态
转移后的总和减少
这样,从初始状态
一个方法是:在生成时,记录上一次替换的是哪种饼干(记为
我们要按总和从大到小的顺序输出方案,自然能想到用最大堆(优先队列)来维护当前已生成但还未输出的状态。
每次取出堆顶(总和最大的状态),输出它,然后生成它的后继状态并加入堆中。
重复
复杂度
Code
#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define y0 __Y0
#define y1 __Y1
#define fi first
#define se second
#define re return
#define pii pair<int,int>
#define pll pair<ll,ll>
#define FILE(x) freopen(x".in","r",stdin);freopen(x".out","w",stdout);
#define file(x) freopen(x".in ","r",stdin);
using namespace std;
int n, k, x, a[55];
struct node {
int s, cnt[55], r;
bool operator<(const node& x) const {
return s < x.s;
}
};
priority_queue<node> pq;
bool cmp(int x, int y) {
return x > y;
}
signed main() {
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, cmp);
node y;
memset(y.cnt, 0, sizeof(y.cnt));
y.s = k*a[1];
y.cnt[1] = k;
y.r = 1;
pq.push(y);
for(int i = 0; i < x; i++) {
node q = pq.top(); pq.pop();
cout << q.s << "\n";
for(int j = q.r; j < n; j++) {
if(q.cnt[j] > 0) {
node t = q;
t.cnt[j]--; t.cnt[j+1]++;
t.s = q.s - a[j] + a[j+1];
t.r = j;
pq.push(t);
}
}
}
return 0;
}