题解:P16402 [ECUSTPC 2026 Spring] 烈焰风暴 2
rabbit_mygo
·
·
题解
群伤对所有怪物造成 y 点伤害,代价为 b;单点对单个怪物造成 x 点伤害,代价为 a。求消灭 n 个怪物的最小总代价。
---
对怪物生命值排序。
我们希望先使用群伤,这样可以让群伤作用尽量多的怪物。
从小到大枚举群伤使用次数 $cty$,则消灭的怪物是一段前缀。并且随着 $cty$ 的增加,前缀长度不降。我们现在要对一段后缀使用 $x$ 的次数求和,即求 $\sum_{i=l}^{n}\lceil \dfrac{h_i-cty \cdot y}{x} \rceil$。
先把上取整换成下取整 $\sum_{i=l}^{n}\lfloor \dfrac{h_i-1-cty \cdot y}{x} \rfloor+1$。
套路地,记 $k_i = \lfloor \frac{h_i-1}{x} \rfloor$,$r_i = (h_i-1) \bmod x$;记 $ky = \lfloor \frac{cty \cdot y}{x} \rfloor$,$ry = cty \cdot y \bmod x$。
则上式变成:
\begin{aligned}
\sum_{i=l}^{n}\lfloor \dfrac{hi-1-cty \cdot y}{x} \rfloor+1
&= \sum{i=l}^{n}\lfloor \dfrac{k_ix+ri-(kyx+ry)}{x} \rfloor +1 \
&= \sum{i=l}^{n}k_i-ky+1+\lfloor \dfrac{ri-ry}{x} \rfloor \
&= (\sum{i=l}^{n}ki)+(n-l+1)(-ky+1)-\sum{i=l}^{n}[r_i \lt r_y]
\end{aligned}
其中 $\lfloor \dfrac{r_i-ry}{x} \rfloor = -[r_i \lt ry]$ 的原因是 $r_i,ry \in [0,x)$,所以只有在 $r_i \lt r_y$ 的情况下,前式值为 $-1$。
式子的前两项是好维护的。最后一项用一个树状数组即可。
时间复杂度 $O(V \log n)$。
出于未知原因,我的代码需要开 ```__int128``` 才能通过。
```cpp
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <queue>
#define FOR(i,a,b) for(int i = (a);i <= (b);++i)
#define REP(i,a,b) for(int i = (a);i >= (b);--i)
#define ll __int128
static char stkk[200];
template<typename T>inline void output(T x){
if(!x)return putchar('0'),void();
if(x<0)x = ~x+1,putchar('-');
int top = 0;
for(;x;stkk[++top]=x%10^48,x/=10);
for(;top;putchar(stkk[top--]));
}
template<typename T>inline void readx(T &x){
x = 0;int y = 1;char c = getchar();
for(;c<48||c>58;c = getchar())if(c=='-')y = -1;
for(;c>=48&&c<=58;c = getchar())x = (x<<1)+(x<<3)+(c^48);
x *= y;
}
const int N = 2e6+10;
const ll lnf = 1e18+10;
static int a,b,x,y,n,s[N],r[N],k[N];
inline void ckmin(ll &x,ll y){
x>y&&(x=y);
}
struct D0{
int tr[N];
void my(int p,int d){
for(;p<=x;tr[p]+=d,p+=p&-p);
}
int qy(int p){
int c = 0;for(;p;c+=tr[p],p&=p-1);return c;
}
}t0;
signed main(){
readx(n),readx(x),readx(y),readx(a),readx(b);
FOR(i,1,n)readx(s[i]);
std::sort(s+1,s+1+n);
ll sum = 0;
FOR(i,1,n)sum+=(r[i]=(s[i]-1)/x),k[i] = (s[i]-1)%x;
FOR(i,1,n)t0.my(k[i]+1,1);
int j = 1;
ll ans = lnf;
FOR(ty,0,(s[n]-1)/y+10){
ll v = ty*y;
for(;j<=n&&s[j]<=v;sum-=r[j],t0.my(k[j++]+1,-1));
ll yr = v/x,yk = v%x;
ckmin(ans,1ll*b*ty+1ll*a*(sum-t0.qy(yk)+1ll*(n-j+1)*(-yr+1)));
}
output(ans),putchar(10);
return 0;
}
```