C - 道路の段差補修 / Road Bump Repair 解説 by admin
Claude 4.6 Opus (Thinking)概要
\(N\) 枚のブロックから最大 \(K\) 枚を交換して高さを自由に設定し直すことで、隣接ブロック間の「危険箇所」(高さの差が閾値 \(T\) を超える箇所)の数を最小化する問題です。\(N \leq 10\) という小さな制約を活かし、交換するブロックの全組合せを試します。
考察
重要な気づき①:交換するブロックの全探索
\(N \leq 10\) なので、交換するブロックの選び方は高々 \(2^{10} = 1024\) 通りです。全ての組合せを試すことができます。
重要な気づき②:交換後の最適な高さの決定
交換するブロックを決めた後、残った「固定ブロック」に着目します。固定ブロック間に挟まれた交換ブロックの高さをどう設定すれば良いでしょうか?
例: 位置 \(a\) と \(b\)(\(a < b\))に固定ブロックがあり、間の \(b - a - 1\) 枚は全て交換対象とします。この区間には \(b - a\) 個のステップ(隣接ペア)があります。
- 各ステップで安全に変化できる高さは最大 \(T\) です
- \(b - a\) ステップで安全にカバーできる最大高低差は \((b - a) \times T\)
- もし \(|D_a - D_b| \leq (b-a) \times T\) なら、滑らかに補間でき、危険箇所 0
- もし \(|D_a - D_b| > (b-a) \times T\) なら、1箇所だけ大きなジャンプを許容すれば残りを安全にできるので、危険箇所 1
具体例: \(D_a = 0\), \(D_b = 100\), \(T = 10\), 間に2枚の交換ブロック(3ステップ) - 安全に橋渡しできる差: \(3 \times 10 = 30 < 100\) → 足りない - 高さを \(0, 10, 90, 100\) と設定 → 差は \(10, 80, 10\) → 危険箇所は1つだけ
重要な気づき③:端の処理
最初の固定ブロックより左、最後の固定ブロックより右の交換ブロックは、隣の固定ブロックに合わせて自由に高さを設定できるため、危険箇所は 0 です。
アルゴリズム
- \(K \geq N\) の場合、全ブロック交換可能なので答えは \(0\)
- サイズ \(0\) から \(K\) までの全部分集合について:
- 交換しない「固定ブロック」の位置を列挙
- 固定ブロックが0個なら危険箇所は \(0\)
- 連続する固定ブロックのペア \((a, b)\) について:
- \(|D_a - D_b| \leq (b-a) \times T\) なら危険箇所 \(0\)
- そうでなければ危険箇所 \(1\) を加算
- 全組合せの中で最小値を出力
計算量
- 時間計算量: \(O\left(\sum_{k=0}^{K} \binom{N}{k} \cdot N\right) = O(2^N \cdot N)\)(\(N \leq 10\) より最大約 \(10240\) 回の演算)
- 空間計算量: \(O(N)\)
実装のポイント
itertools.combinationsを使うことで、交換するブロックの全組合せを簡潔に列挙できる固定ブロックが隣接している場合(\(b - a = 1\))も同じ式で正しく処理される:\(|D_a - D_b| > 1 \times T\) なら危険箇所1、そうでなければ0(これは元の判定と一致)
「1回の危険ジャンプで任意の差を埋められる」という性質が、この問題の本質的なポイント
ソースコード
from itertools import combinations
def solve():
N, T, K = map(int, input().split())
D = list(map(int, input().split()))
if K >= N:
print(0)
return
best = float('inf')
# Try all subsets of indices to replace (size 0 to K)
for k in range(K + 1):
for replaced in combinations(range(N), k):
replaced_set = set(replaced)
# For the replaced blocks, we need to choose heights to minimize dangerous pairs
# The non-replaced blocks have fixed heights.
# We need to find optimal heights for replaced blocks.
# Since N <= 10, we can think of this as segments separated by fixed blocks.
# Between fixed blocks, we can set replaced blocks to any integer value.
# Key insight: for a segment of consecutive replaced blocks bounded by fixed blocks,
# we can always interpolate to avoid dangerous pairs within the segment AND
# at the boundaries, as long as the boundary difference can be bridged.
# Let's think about it differently. We have positions 0..N-1.
# Fixed positions have known heights. Replaced positions have free heights.
# We want to minimize the number of adjacent pairs where |h[i] - h[i+1]| > T.
# Since N <= 10, we can use DP or even brute force on the replaced blocks.
# But the heights can be arbitrary integers...
# For each pair of adjacent blocks (i, i+1):
# - If both are fixed: danger is determined.
# - If at least one is replaced: we can potentially make |h[i]-h[i+1]| <= T.
# Between two fixed blocks at positions a and b (a < b, all between are replaced),
# we have (b - a) steps. Fixed heights are D[a] and D[b].
# We can bridge any gap: in (b-a) steps, we can change by T per step,
# so we can cover a difference of (b-a)*T.
# If |D[a] - D[b]| <= (b-a)*T, we can make all pairs safe (0 dangerous).
# Otherwise, we need ceil(|D[a]-D[b]| / T) - (b-a) ... no wait.
# Actually minimum dangerous pairs = max(0, ceil(|D[a]-D[b]|/T) - (b-a))
# Hmm, but we need exactly (b-a) steps, each can change by at most T to be safe.
# So if |D[a]-D[b]| <= (b-a)*T, 0 dangerous pairs.
# Otherwise, we can't bridge it. The minimum dangerous pairs is ceil(|D[a]-D[b]|/T) - (b-a)...
# Actually no. Each step either contributes T towards bridging (safe) or we must have a dangerous step.
# Min dangerous = max(0, ceil(|D[a]-D[b]| / T) - (b-a))...
# Wait: with (b-a) steps, max safe bridging = (b-a)*T. If diff > (b-a)*T,
# we need at least 1 dangerous pair (which can bridge unlimited). So min dangerous = 1 if diff > (b-a)*T, else 0.
# Actually with 1 dangerous pair we can make an arbitrary jump. So yes, at most 1 dangerous pair needed.
# But actually: min dangerous pairs = 0 if |diff| <= (b-a)*T, else 1.
# Because one dangerous step can cover any remaining difference.
# For boundary segments (before first fixed or after last fixed), 0 dangerous pairs always.
fixed = [i for i in range(N) if i not in replaced_set]
if len(fixed) == 0:
# All replaced, 0 dangerous
best = min(best, 0)
continue
danger = 0
# Between consecutive fixed blocks
for idx in range(len(fixed) - 1):
a, b = fixed[idx], fixed[idx + 1]
gap = b - a # number of steps
diff = abs(D[a] - D[b])
if diff > gap * T:
danger += 1
# Before first fixed and after last fixed: 0 danger (we can set freely)
# Between consecutive fixed with no replaced in between: just check directly
# Wait, that's already handled: if b - a == 1, gap=1, diff=|D[a]-D[b]|, danger if diff > T.
best = min(best, danger)
print(best)
solve()
この解説は claude4.6opus-thinking によって生成されました。
投稿日時:
最終更新: