Official

A - 縞模様の布 / Striped Cloth Editorial by admin Editorial for printing

gpt-6-astra-medium

Overview

We alternate printing red and blue in blocks of \(K\) cells from left to right. We process \(N\) cells in order while keeping track of the current color and how many cells have been painted with that color.

Approach

This problem can be solved by directly simulating the process described in the statement.

We maintain the following two pieces of information:

  • color: the color of the current stripe
  • inStripe: the number of cells already painted in the current stripe

Since we start with red, we initialize color to R and inStripe to \(0\). Before painting the next cell, if inStripe has reached \(K\), we switch the color and reset inStripe to \(0\).

For example, when \(N=8,\ K=3\), we paint as follows:

  • The first \(3\) cells: RRR
  • The next \(3\) cells: BBB
  • The remaining \(2\) cells: RR

Therefore, the output is RRRBBBRR.

Since \(N \leq 100\) in this problem, a naive cell-by-cell approach is sufficiently fast. However, if we always output \(K\) characters at a time, we might exceed \(N\) characters when the last stripe is shorter. We can avoid this issue by outputting one character at a time, exactly \(N\) times.

Algorithm

  1. Read \(N,\ K\).
  2. Initialize color to R and inStripe to \(0\).
  3. Repeat the following process \(N\) times:
    • If inStripe is equal to \(K\), do the following:
      • Switch color to B if it is R, or to R if it is B.
      • Reset inStripe to \(0\).
    • Output the single character color.
    • Increment inStripe by \(1\).
  4. Finally, output a newline.

In this process, the color switches right before processing the next cell after \(K\) cells of the same color have been printed. As a result, each stripe has a length of \(K\), and only the final stripe takes a length corresponding to the remaining number of cells.

Complexity

  • Time complexity: \(O(N)\)
    A constant-time operation is performed once for each cell.
  • Space complexity: \(O(1)\)
    Since the characters are printed directly without storing the string, the number of required variables is constant.

Key Implementation Points

  • Check whether to switch colors before outputting the cell. This ensures that the new color begins from the cell immediately after completing \(K\) cells.

  • After switching colors, always be sure to reset inStripe to \(0\).

  • No special handling is needed even if \(N<K\). Since the loop terminates before the color switches, all cells will be painted red.

  • Note that the provided code is a C++ implementation regardless of the language specified for the code block.

    Source Code

// 問題文から独立に実装: K マス染めるごとに色を切り替える
#include <iostream>
int main() {
    int n, k; std::cin >> n >> k;
    char color = 'R';
    int inStripe = 0;
    for (int i = 0; i < n; ++i) {
        if (inStripe == k) { color = (color == 'R') ? 'B' : 'R'; inStripe = 0; }
        std::cout << color;
        inStripe++;
    }
    std::cout << std::endl;
}

This editorial was generated by gpt-6-astra-medium.

posted:
last update: