B - ドミノ倒しの一撃 / A Single Strike of Dominoes Editorial by admin
gpt-5.3-codexOverview
Each pillar is determined to fall or not based on “a direct hit of \(X\)” and “an additional \(1\) if the left neighbor has fallen.”
Since this determination proceeds uniquely from left to right, we can efficiently check “whether all pillars can be toppled for a given \(X\)” and use binary search to find the minimum \(X\).
Analysis
First, consider a fixed value of \(X\).
Looking from left to right, the effective durability of pillar \(i\) is: - Base: \(A_i - X\) - Additionally \(-1\) if the left neighbor has fallen
So the condition for it to fall is: $\(A_i - X - (\text{1 if the left neighbor has fallen, 0 otherwise}) \le 0\)$
The key point of this problem is that toppling is determined sequentially from left to right.
Whether pillar \(i\) falls depends only on whether pillar \(i-1\) has fallen; no information from the right side is needed.
Naively trying \(X=1,2,3,\dots\) in order would require \(X\) up to nearly \(10^9\) in the worst case, which is too slow.
So we exploit the following monotonicity:
- If all pillars can be toppled with a given \(X\), then they can certainly be toppled with \(X+1\) as well (since more damage is dealt, it never becomes disadvantageous).
In other words, “whether all pillars can be toppled” is monotonic with respect to \(X\) (False…False, True…True), so binary search can be applied.
Algorithm
- Define
ok(x):
Determine in \(O(N)\) whether all pillars can be toppled with power \(x\).- Process from left to right
- Maintain
left_fallen(whether the left neighbor has fallen) - For each pillar, compute
hp = A[i] - x - (1 if left_fallen else 0) - If
hp > 0, that pillar survives, so immediately returnFalse - If it falls, chain to the next pillar by setting
left_fallen = True
- Binary search:
- Search range is \([1, \max A_i]\)
- If
ok(mid)is true, shrink the right bound; if false, raise the left bound
- The converged value is the minimum \(X\).
In this code, if the first pillar does not fall, it immediately returns False, so updating left_fallen to always be True is fine (since the function terminates the moment any pillar survives).
Complexity
- Time complexity: \(O(N \log \max A)\)
- Space complexity: \(O(1)\) (excluding the input array)
Implementation Notes
Why the upper bound of binary search can be set to
max(A): If \(X = \max(A)\), at least that much damage is dealt to each pillar, so all pillars will be toppled.Making
ok(x)returnFalseimmediately upon finding a surviving pillar keeps it fast.In Python, since \(N=5\times10^5\), using
sys.stdin.readlineis recommended for safety.Source Code
import sys
def main():
input = sys.stdin.readline
N = int(input().strip())
A = list(map(int, input().split()))
def ok(x: int) -> bool:
left_fallen = False
for i in range(N):
hp = A[i] - x - (1 if left_fallen else 0)
if hp > 0:
return False
left_fallen = True
return True
lo, hi = 1, max(A)
while lo < hi:
mid = (lo + hi) // 2
if ok(mid):
hi = mid
else:
lo = mid + 1
print(lo)
if __name__ == "__main__":
main()
This editorial was generated by gpt-5.3-codex.