Official

B - 壁の塗装 / Painting the Wall Editorial by admin Editorial for printing

Gemini 3.8 Flash (High)

Overview

The obstacles # divide the wall into several “contiguous segments of empty cells .”. Since each segment can be processed independently, we find the minimum number of operations required for each segment and sum them up. If the length of a segment is strictly less than \(K\), it cannot be painted, so the answer is -1. A segment of length \(L \geq K\) can be painted in \(\lceil L / K \rceil\) operations using ceiling division.

Analysis

1. Independence of Each Segment

The roller cannot be placed on any cell containing an obstacle #. Therefore, it is impossible to paint across an obstacle. From this, we can consider each contiguous segment of . (connected component) separated by obstacles # independently. The total minimum number of operations is the sum of the minimum number of operations required to paint each individual segment.

2. Impossibility Conditions

Consider a segment whose length \(L\) satisfies \(1 \leq L < K\). Since the roller has a width of \(K\), attempting to place the roller within this segment will inevitably cause it to extend beyond the wall boundaries or onto an obstacle #. Therefore, if there is even a single segment of length at least \(1\) and strictly less than \(K\), there is no way to paint that segment, and the answer is -1.

3. Minimum Number of Operations for Each Segment

Consider painting a segment of length \(L \geq K\). A single operation can paint at most \(K\) cells. Thus, painting all cells in a segment of length \(L\) requires at least \(\lceil L / K \rceil\) operations (rounding up \(L / K\)).

This lower bound (minimum value) is always achievable. - Concrete Example: A segment with \(K = 3, L = 5\) - 1st operation: Align with the left end to paint cells \(1 \sim 3\). - 2nd operation: Align with the right end to paint cells \(3 \sim 5\) (cell \(3\) is painted twice / overlapped). - In total, all cells can be painted in \(\lceil 5 / 3 \rceil = 2\) operations.

In general, by advancing \(K\) cells at a time from the left end and aligning only the final operation with the right end so that it overlaps, the entire segment can always be covered without extending outside in exactly \(\lceil L / K \rceil\) operations.

Algorithm

  1. Prepare a variable ans = 0 to store the answer and a variable cur_len = 0 to track the current length of consecutive .’s.
  2. Iterate through the string \(S\) from beginning to end.
    • If the character is .: Increment cur_len by \(1\).
    • If the character is # (or at the end of the string):
      • If cur_len > 0 and cur_len < K, it is impossible to paint, so output -1 and terminate.
      • If cur_len >= K, add \(\lceil \text{cur\_len} / K \rceil\) to ans, and reset cur_len to \(0\).
  3. After scanning the entire string, output ans.
    • Note that if there are no .’s from the beginning, ans = 0 will be correctly output as is.

Complexity

  • Time Complexity: \(O(N)\) We only scan the string \(S\) once from start to end, which comfortably runs within the time limit even under the constraint \(N \leq 2 \times 10^6\).
  • Space Complexity: \(O(N)\) \(O(N)\) memory is used to store the input string \(S\).

Implementation Notes

  • Handling the End (Sentinel Method): By running the loop from 0 to N and forcibly handling \(i = N\) as a “segment boundary”, you eliminate the need to write duplicate code for processing any remaining segment after the loop terminates.

  • Ceiling Division: When computing \(\lceil A / B \rceil\) using integer arithmetic for positive integers \(A, B\), the standard idiom is to write (A + B - 1) / B.

    Source Code

#include <iostream>
#include <string>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n, k;
    if (!(cin >> n >> k)) return 0;

    string s;
    cin >> s;

    long long ans = 0;
    int cur_len = 0;

    for (int i = 0; i <= n; ++i) {
        if (i < n && s[i] == '.') {
            cur_len++;
        } else {
            if (cur_len > 0) {
                if (cur_len < k) {
                    cout << -1 << "\n";
                    return 0;
                }
                ans += (cur_len + k - 1) / k;
                cur_len = 0;
            }
        }
    }

    cout << ans << "\n";
    return 0;
}

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

posted:
last update: