公式

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

Claude Opus 5 (High)

Summary

This problem asks for the minimum number of operations required to paint all squares using a roller of width \(K\), considered over each contiguous segment (run) of . separated by obstacles #. For each run of length \(L\), if \(L < K\), it is impossible; otherwise, \(\lceil L/K \rceil\) operations are needed, and the answer is the sum of these values over all runs.

Analysis

Observation 1: Each run can be considered independently

Since the roller can only be placed on a “contiguous segment of \(K\) squares without obstacles”, each operation must be contained entirely within a single run (a maximal contiguous segment of .). An operation can never cross an obstacle.

Therefore, the problem for the entire wall decomposes into “painting each run independently”, and the answer is the sum of the number of operations required for each run.

Example: If \(N=10\), \(K=2\), and \(S=\) ..#....#.., there are three runs of lengths \(2\), \(4\), and \(2\).

Observation 2: Number of operations needed for a run of length \(L\)

First, if \(L < K\), there is no place to put the roller inside the run. However, since the run contains at least one ., that square can never be painted. Thus, the answer is \(-1\).

Now consider the case where \(L \geq K\). Since each operation paints exactly \(K\) squares, at least \(\lceil L/K \rceil\) operations are required to paint all \(L\) squares (lower bound).

Conversely, this can always be achieved in \(\lceil L/K \rceil\) operations. Let the left end of the run be \(l\) and the right end be \(r\) (with length \(L = r-l+1\)): - Place the roller packed from the left at positions \(l, l+K, l+2K, \dots\), - and only for the last operation, align it with the right end by placing it at position \(r-K+1\).

Because \(L \geq K\), the operation aligned to the right end will still lie entirely within the run without sticking out. The key point is that overlapping intervals are allowed, so any fractional remainder can be absorbed by “aligning flush with the right end”.

Example: When \(L=5\) and \(K=2\), it takes \(\lceil 5/2 \rceil = 3\) operations. Placing it at positions \(1\) and \(3\) paints squares \(1..4\), and finally placing it at position \(4\) paints squares \(4\) and \(5\) (square \(4\) is painted twice, which is completely fine).

Comparison with a Naive Approach

It is possible to solve this by simulating a greedy approach: “repeatedly find the leftmost unpainted square and place a roller there.” However, a naive implementation that tracks painted squares individually might take \(O(N)\) to \(O(NK)\), and a DP approach would introduce too many states to comfortably handle \(N \le 2\times 10^6\). With the insights above, a single simple pass that merely counts the length of each run suffices.

Algorithm

  1. Scan the string \(S\) once from left to right, counting the length of contiguous . as run.
  2. When reaching a # (and at the end of the string), if run > 0:
    • If run < K, mark it as impossible and terminate (output \(-1\)).
    • Otherwise, add \(\lceil \texttt{run}/K \rceil = \lfloor(\texttt{run}+K-1)/K\rfloor\) to the answer.
    • Reset run to \(0\).
  3. Once the entire string has been processed, output the answer. If there are no . at all, the answer will be \(0\) (which happens automatically).

Complexity

  • Time Complexity: \(O(N)\)
  • Space Complexity: \(O(N)\) (to store the input string; the traversal itself uses \(O(1)\) auxiliary space)

Implementation Notes

  • Be careful not to miss the trailing run: After exiting the loop, make sure to process the last run if it hasn’t been handled yet (e.g., when the input does not end with #, like ....).

  • Ceiling division: \(\lceil L/K \rceil\) can be computed using integer arithmetic as (L + K - 1) / K. Avoid floating-point operations due to the risk of precision issues.

  • Overflow: Since the answer is at most around \(N \le 2\times 10^6\), a 32-bit integer is sufficient, but using long long is safer.

  • Fast I/O: Since \(N \le 2\times 10^6\), in C++ use scanf / fixed-size buffer, or set ios::sync_with_stdio(false) with cin. In Python, read via sys.stdin. Avoid reading the string character by character with cin >> ch.

  • When there are 0 .s: The answer is \(0\). A run of length \(0\) is excluded from addition, so the above implementation naturally handles this (due to the run > 0 condition).

  • In practice, appending # as a sentinel to the end of \(S\) makes the implementation simpler, as the trailing run can be handled uniformly within the loop.

    Source Code

#include <bits/stdc++.h>
using namespace std;

int main(){
    // fast read
    int N; long long K;
    {
        // read using scanf-like fast method
    }
    static char buf[2000006 + 64];
    if(scanf("%d %lld", &N, &K)!=2) return 0;
    if(scanf("%s", buf)!=1) return 0;
    long long ans = 0;
    long long run = 0;
    bool ok = true;
    for(int i=0;i<N;i++){
        if(buf[i]=='.') run++;
        else {
            if(run>0){
                if(run < K){ ok=false; break; }
                ans += (run + K - 1)/K;
            }
            run = 0;
        }
    }
    if(ok && run>0){
        if(run < K) ok=false;
        else ans += (run + K - 1)/K;
    }
    if(!ok) printf("-1\n");
    else printf("%lld\n", ans);
    return 0;
}

This editorial was generated by claude5opus-high.