CF2035E Monster 题解 / Solution

· · 题解

前言 / Foreword

非常好题目。非常好题目背景。

Great problem, great quote.

扽西 / Analysis

先考虑最优解长什么样子。如果先攻击再增加,并且可以把增加放到攻击前面,那么造成的伤害可以变多 1。因此,一定要尽可能先增加再攻击。故而最优解一定形如,重复 r(r\ge 0) 次“增加 k 次,攻击 1 次”,再增加 a(1\le a\le k) 次,攻击 b(b\ge 1) 次。这样花费了 rkx+ry+ax+by 枚金币,造成了 \dfrac{r(r+1)}{2}k+(rk+a)b 的伤害。

Consider what form the optimal solution can be in. If one firstly attacks the monster then increases d, the damage dealt is 1 less than that if one firstly increases d then attakcs the monster. Therefore, in the optimal solution, the increments must be before attacks as much as possible, which means the optimal solution must be in the following form: repeat "increase d for k times, and attack once" for r(r\ge 0) times, then increase d for a(1\le a\le k) times, then attack b(b\ge 1) times. In this way, damage of \dfrac{r(r+1)}{2}k+(rk+a)b is dealt by costing rkx+ry+ax+by coins.

$t\le 100,x,y,z,k\le 10^8$, which is super sqrt-balancing-ish. Consider a threshold $B$: 若 $k\le B$,因为 $k$ 很小,$a$ 的范围为 $1\sim k$ 也很小,可以直接考虑枚举 $a,r$,计算出最小的 $b$ 更新答案。因为一旦 $\dfrac{r(r+1)}{2}k\ge z$ 就可以不用再枚举了,$r$ 的范围是 $O\left(\sqrt{\dfrac zk}\right)$ 的,因此这时的复杂度是 $O(\sqrt {zk})$。 When $k\le B$, since $k$ is small, and the range of $a$ is $1$ to $k$, one can enumerate $a,r$, then calculate the least possible $b$ to update the answer. When $\dfrac{r(r+1)}{2}k$ already reaches $z$, there's no need to continue the enumeration anymore, so $r$ is in a range of $O\left(\sqrt{\dfrac zk}\right)$, which implies a complexity of $O(\sqrt{zk})$. 若 $k\ge B$,此时 $\dfrac{z}{k}$ 很小。此时枚举 $r$,枚举 $b$ 的范围是 $O\left(\dfrac{z}{rk}\right)$ 的,再计算最小的 $a$ 即可。因为调和级数,这部分的复杂度是 $O\left(\dfrac{z}{k}\log z\right)$ 的。$r=0$ 时需要特殊用数论分块处理,因此总复杂度 $O\left(\dfrac{z}{k}\log z+\sqrt z\right)$。 When $k\ge B$, $\dfrac{z}{k}$ is small. Therefore, $b$ is in a range of $O\left(\dfrac{z}{rk}\right)$. By enumerating $r$ and $b$, one can determine the least possible $a$ and then update the answer. Thanks to the harmonic series, The complexity is $O\left(\dfrac{z}{k}\log z\right)$. However, when $r=0$, one needs to use the trick of "$O(\sqrt z)$ different values for $\left\lceil \dfrac zb\right\rceil$" to enumerate $b$ in a clever way. The total complexity is $O\left(\dfrac{z}{k}\log z+\sqrt z\right)$. 取 $B=O(z^{1/3}\log^{2/3}z)$,每组数据的时间复杂度是 $O(z^{2/3}\log^{1/3}z)$,可以通过。 Take $B=O(z^{1/3}\log^{2/3}z)$, the time complexity per test is $O(z^{2/3}\log^{1/3}z)$, which can pass the problem easily.