洛谷 P6619 冰火战士 做题笔记

Published on

题意

有冰火两种战士,每个战士有温度和能量两个属性。战场也有一个温度。
一个冰战士能够参赛,当且仅当战场温度不低于自身温度。
一个火战士能够参赛,当且仅当战场温度不高于自身温度。
开战时,冰系战士按自身温度从低到高排序,火系战士按自身温度从高到低排序,温度相同时能量大的战士排在前面。首先,双方的第一位战士之间展开战斗,两位战士消耗相同的能量,能量少的战士将耗尽能量退出比赛,而能量有剩余的战士将继续和对方的下一位战士战斗(能量都耗尽则双方下一位战士之间展开战斗)。如此循环,直至某方战士队列为空,比赛结束。
初始时没有一个战士。
现在你需要维护 $Q$ 个操作,每次操作可以加入或删除一个战士。
每次操作后,你需要求出 能使冰火双方消耗总能量最高的 最大温度。

题解

先定义两个变量,方便描述。
$ice$ 表示所有能够参赛的冰战士的总能量。
$fire$ 表示所有能够参赛的火战士的总能量。

容易得到,战争消耗的能量为 $2 \times \min\left\{ice, fire\right\}$。

根据冰火战士的参赛性质,我们知道:随着战场温度的增加,$ice$ 单调不减,且 $fire$ 单调不增。如下图

显然 $2 \times \min\left\{ice, fire\right\}$ 在交叉点($ice = fire$)最大。
用树状数组维护差分,倍增就可以找到交叉点了。

时间复杂度 $\mathcal O\left(Q \log n\right)$。


Problem statement

There are two types of warriors: Ice Warriors and Fire Warriors. Each warrior has two attributes: temperature and energy. The battlefield also has a temperature.
An Ice Warrior can participate in the battle only if the battlefield temperature is not lower than its own temperature.
A Fire Warrior can participate in the battle only if the battlefield temperature is not higher than its own temperature.
At the beginning of the battle, the Ice Warriors are sorted in ascending order by their temperatures, and the Fire Warriors are sorted in descending order by their temperatures. If two warriors have the same temperature, the one with more energy will come first. The battle starts with the first warrior from both sides. They consume the same amount of energy, and the one with less energy will drop out of the battle. The one with remaining energy will continue to battle with the next warrior from the other side (if both warriors have no energy left, the next two warriors will start battling). The battle continues in this way until one of the sides has no warriors left.
Initially, there are no warriors.
Now, you need to maintain $Q$ operations. Each operation can add or remove a warrior. After each operation, you need to find the maximum temperature that can make both Ice and Fire sides consume the maximum total energy.

Solution

Let's define two variables to make the description easier.
$ice$ represents the total energy of all Ice Warriors who can participate in the battle.
$fire$ represents the total energy of all Fire Warriors who can participate in the battle.

It can be easily seen that the energy consumed in the battle is $2 \times \min{ice, fire}$.

According to the participation rules of Ice and Fire Warriors, we know that: as the battlefield temperature increases, $ice$ is monotonically non-decreasing, and $fire$ is monotonically non-increasing. As shown in the figure below:

Obviously, $2 \times \min\left\{ice, fire\right\}$ is maximum at the intersection point where $ice = fire$. We can use a Fenwick tree to maintain the difference between Ice and Fire Warriors' energies, and binary search to find the intersection point.

The time complexity is $\mathcal O(Q \log n)$.


No comments

Post a comment