跳到正文

题解

[ABC440E] Cookies

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

文章信息

收录
2026年1月12日
更新
2026年1月12日
篇幅
约 4 分钟

版本

  1. v2当前
  2. v1

题解

题目传送门

思路

先将数组从大到小排序,显然 s1=k×a1s_1=k \times a_1。 可以证明每一种状态一定可以通过最初的 kk 个 a1a_1 得到。 使用优先队列,每个状态中记录当前从 k×a1k \times a_1 里被扣除的数 sumsum,从最初状态开始,改变的数的个数 totaltotal,以及上次改变的数的编号 lastlast。 转移时,只要 totaltotal 不为 kk,接下来改变的数就可以是 last+1,…,nlast+1,\dots,n,最终取 sumsum 最小的前 xx 个数,输出时输出 k×a1−sumk \times a_1-sum 即可。

AC Code

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	int x = 0,p = 1;
	char c = getchar();
	while(c > '9' || c < '0')	p = c == '-' ? -1 : 1,c = getchar();
	while(c >= '0' && c <= '9')	x = (x << 1) + (x << 3) + (c ^ 48),c = getchar();
	return p * x;
}
template<typename _Tp> inline void _write(_Tp x) { if(x > 9) _write(x / 10); putchar(x % 10 | 48); }
template<typename _Tp> inline void write(_Tp x) { if(x < 0) putchar('-'),x = -x; _write(x); }
template<typename _Tp> inline void writeln(_Tp x) { write(x); puts(""); }
struct node { int sum,total,last; };
bool operator < (node x,node y) { return x.sum > y.sum; }
int a[60],d[60];
signed main()
{
    int n = read(),k = read(),x = read();
    for(int i = 1;i <= n;i++)	a[i] = read();
    sort(a + 1,a + n + 1,greater<int>());
    if(n == 1)
	{
        for(int i = 1;i <= x;i++)	writeln(k * a[1]);
        return 0;
    }
    for(int i = 1;i <= n - 1;i++)	d[i] = a[1] - a[i + 1];
    priority_queue<node> pq; pq.push({0,0,0});
    vector<int> v; v.reserve(x);
    while(v.size() < x)
	{
        node cur = pq.top(); pq.pop();
        v.push_back(cur.sum);
        if(cur.total < k)
		{
            if(cur.last >= 1)	pq.push({cur.sum + d[cur.last],cur.total + 1,cur.last});
            for(int j = cur.last + 1;j <= n - 1;j++)
                pq.push({cur.sum + d[j],cur.total + 1,j});
        }
    }
    int base = k * a[1];
    for(int i = 0;i < v.size();i++)	writeln(base - v[i]);
    return 0;
}

Atcoder提交记录

原文