公式

B - 壁の塗装 / Painting the Wall 解説 by admin

Gemini 3.8 Flash (High)

Overview

In this problem, we can treat each contiguous segment of empty cells . separated by obstacles # independently, and find the total minimum number of operations required to completely paint each segment using a roller of length \(K\).

Analysis

1. Independence of Segments Separated by Obstacles

The roller cannot paint cells containing obstacles #, nor can it paint across obstacles. Therefore, the wall is completely divided by obstacles # into several “contiguous segments of .”. Since these segments do not affect each other, we can consider each segment independently.

2. Painting Strategy and Feasibility for Each Segment

Consider a contiguous segment of . of length \(L\).

  • When \(L < K\) Since the length of the roller is \(K\), it cannot even be placed within this segment. Therefore, it is impossible to paint this segment, meaning the overall goal cannot be achieved and the answer is -1.

  • When \(L \ge K\) Since a single operation can paint at most \(K\) cells, at least \(\lceil L / K \rceil\) operations are required to paint all cells in a segment of length \(L\) (where \(\lceil x \rceil\) represents the ceiling function, i.e., the smallest integer greater than or equal to \(x\)). In fact, by painting \(K\) cells at a time starting from the left end and aligning the last operation with the right end (allowing overlaps with already painted cells), we can paint all cells in the segment in exactly \(\lceil L / K \rceil\) operations.

    • Concrete Example: When \(L = 5, K = 3\) Let the cells in the segment be \(1, 2, 3, 4, 5\):
      • 1st operation: Paint cells \(1, 2, 3\)
      • 2nd operation: Paint cells \(3, 4, 5\) (cell \(3\) is painted twice)

    This paints all cells in a total of \(\lceil 5 / 3 \rceil = 2\) operations.

3. Overall Answer

If there is even one contiguous segment of . with a length less than \(K\), the answer is -1. If all segments have a length of at least \(K\), the answer is the sum of the required operations \(\lceil L / K \rceil\) across all segments. Note that if there are no . cells from the beginning, the total number of operations is \(0\), which satisfies the problem conditions.

Algorithm

  1. Traverse the string \(S\) from left to right, counting the number of consecutive . characters (the segment length \(L\)).
  2. When encountering a # or reaching the end of the string, evaluate the length \(L\) of the preceding segment:
    • If \(L = 0\): Do nothing.
    • If \(1 \le L < K\): It is impossible to paint, so immediately output -1 and terminate.
    • If \(L \ge K\): Add the required number of operations \(\lceil L / K \rceil\) to the total answer, and reset the count \(L\) to \(0\).
  3. After finishing the traversal, output the total answer.

Note: The ceiling division \(\lceil L / K \rceil\) can be computed using integer division as (L + K - 1) // K.

Complexity

  • Time Complexity: \(O(N)\)
    • Since we only traverse the string \(S\) once from left to right, it can be processed in \(O(N)\) time. Since \(N \le 2 \times 10^6\), this runs well within the time limit.
  • Space Complexity: \(O(N)\)
    • It requires \(O(N)\) memory to store the input string.

Implementation Details

  • Handling the Segment at the End of the String: After finishing the loop over the string, an unprocessed segment of . may remain at the end. Make sure to perform the same check and addition outside the loop as well.

  • Fast I/O: Since \(N\) can be as large as \(2 \times 10^6\), in Python you can use sys.stdin.read to read all input at once to minimize I/O overhead.

    Source Code

import sys

def main():
    input = sys.stdin.read
    data = input().split()
    if not data:
        return
    N = int(data[0])
    K = int(data[1])
    S = data[2]

    ans = 0
    cur = 0
    for ch in S:
        if ch == '.':
            cur += 1
        else:
            if cur > 0:
                if cur < K:
                    print(-1)
                    return
                ans += (cur + K - 1) // K
                cur = 0
    if cur > 0:
        if cur < K:
            print(-1)
            return
        ans += (cur + K - 1) // K

    print(ans)

if __name__ == '__main__':
    main()

This editorial was generated by gemini-3.8-flash-high.