题解:P16544 [EGOI 2026] 蛋糕 / Cakes

· · 题解

题意

有 N 种配料,每种 a_i 个。需全部分到 K 个蛋糕,且每个蛋糕的“美味度”(出现最多的配料次数)相同,判断可行性。

思路

设美味度为 M。则:

M \cdot K \ge \max \{a_i\} \quad K \le \sum_{i=1}^N \left\lfloor \frac{a_i}{M} \right\rfloor \triangleq\ \operatorname{cap}(M) 只需检查**最小可行的** $M = \lceil \max\{a_i\} / K\rceil$ 是否满足 $K \le \operatorname{cap}(M)$。 ### 预处理 - 统计 $cnt_x$:数值 $x$ 的出现次数。 - 前缀和 $\sum_{i=1}^{\max\{a_i\}} pre_i = pre_{i-1} + cnt_i$。 - 对每个 $M$,枚举商 $k$: $$ \operatorname{cap}(M) = \sum_{k=1}^{\lfloor \max a_i / M\rfloor} k \cdot \bigl( \text{pre}[\min((k+1)M-1,\max\{a_i\})] - \text{pre}[k \cdot M-1] \bigr). $$ ### 输出 - 若 $(K > \text{总个数})$ 则 `NO`。 - 否则 $(L = \lceil \max a_i / K\rceil)$,若 $(K \le \operatorname{cap}(L))$ 则 `YES`,否则 `NO`。 ### 代码 ```cpp #include<bits/stdc++.h> using namespace std; #define int long long const int N=1e5+5; int n,q,cnt[N],pre[N],tot,mx,sum[N],K; signed main() { ios::sync_with_stdio(0);cin.tie(0);cout.tie(0); cin>>n>>q; for(int i=1,x;i<=n;++i)cin>>x,cnt[x]++,tot+=x,mx=max(mx,x); for(int i=1;i<=mx;++i)pre[i]=pre[i-1]+cnt[i]; for(int M=1;M<=mx;++M){ for(int k=1;k*M<=mx;++k){ int l=k*M,r=min((k+1)*M-1,mx); sum[M]+=k*(pre[r]-pre[l-1]); } } while(q--){ cin>>K; if(K>tot){cout<<"NO\n";continue;} int now=(mx+K-1)/K; cout<<(sum[now]>=K?"YES\n":"NO\n"); } return 0; } ```