B - 壁の塗装 / Painting the Wall Editorial by admin
Claude Opus 5 (High)Overview
In this problem, we can consider each contiguous segment of . separated by obstacles # independently. For a segment of length \(L\), if \(L < K\), it cannot be painted (output \(-1\)); otherwise, it can be completely painted in \(\lceil L/K \rceil\) operations. The sum of these operations over all segments is the answer.
Analysis
Observation 1: Operations cannot span across obstacles
A roller can only be placed where all \(K\) consecutive cells contain no obstacles. Therefore, the cells painted by any single operation must be completely contained within a single contiguous segment of ..
In other words, the contiguous blocks of . obtained by splitting \(S\) by # are completely independent of each other, and we only need to find the minimum number of operations for each block and sum them up.
Example: If \(S = \) ..#....#., it is divided into three blocks of lengths \(2, 4, 1\).
Observation 2: Answer for a single block
Consider a block of length \(L\) (all cells inside are .).
When \(L < K\): There is not a single position within this block where a roller can be placed. However, the cells in this block have no obstacles and must be painted, making it impossible. Thus, the answer is \(-1\).
When \(L \ge K\):
- Lower bound: Since one operation can paint at most \(K\) cells, covering \(L\) cells requires at least \(\lceil L/K \rceil\) operations.
- Achievability: We can place rollers packed by \(K\) cells from the left end of the block, and align only the last operation flush with the right end of the block. Since \(L \ge K\), the last roller will still stay within the block, and overlapping is allowed. This takes exactly \(\lceil L/K \rceil\) operations.
Example: For \(L = 7, K = 3\), all cells can be painted in \(3\) operations (\(= \lceil 7/3 \rceil\)) at intervals \([1,3], [4,6], [5,7]\).
Comparison with Naive Approaches
One could also solve this using methods such as “DP while determining whether each cell can be painted” or “greedy approach while checking for the presence of # using prefix sums.” However, since \(N \le 2 \times 10^6\) is large, an implementation that performs a linear scan for # within each range for every cell (worst-case \(O(NK)\)) will result in TLE.
Based on the observation above, we only need to look at the lengths of the segments, which requires just a single pass over the string.
Algorithm
- Split \(S\) by
#to obtain the length \(L\) of each contiguous block of.. - For each block:
- If \(L = 0\) (empty strings resulting from consecutive
#, etc.), skip it. - If \(L < K\), immediately output \(-1\) and terminate.
- Otherwise, add \(\lceil L/K \rceil = \lfloor (L + K - 1)/K \rfloor\) to the answer.
- If \(L = 0\) (empty strings resulting from consecutive
- Output the total sum. If there are no
.at all, the sum will be \(0\), which is directly the correct answer.
Complexity
- Time Complexity: \(O(N)\)
- Space Complexity: \(O(N)\) (to store the input string, and \(O(N)\) including the split results)
Implementation Notes
Fast I/O: Since \(N \le 2 \times 10^6\), in Python it is safe to read everything at once using
sys.stdin.buffer.read(). Letting built-in functions likes.split('#')handle the processing is faster than looping character by character.Ceiling Division: Compute this using integer arithmetic as
(L + K - 1) // Kwithout using floating-point numbers.Consecutive
#/#at the boundaries:split('#')produces empty strings (e.g.,"#..#"→['', '..', '']). Since these have length \(0\), skip them. Be careful not to incorrectly output \(-1\) by triggering the \(L < K\) condition when \(L = 0\).The case where all characters are
#: All blocks have length \(0\), so the sum will be \(0\), which matches the problem statement: “if there are no cells without obstacles, output \(0\)”.Source Code
import sys
def main():
data = sys.stdin.buffer.read().split()
n = int(data[0]); k = int(data[1])
s = data[2].decode() if len(data) > 2 else ""
total = 0
for part in s.split('#'):
L = len(part)
if L == 0:
continue
if L < k:
sys.stdout.write("-1\n")
return
total += (L + k - 1) // k
sys.stdout.write(str(total) + "\n")
main()
This editorial was generated by claude5opus-high.