D - 回転スタンプ / Rotating Stamp Editorial by admin
Gemini 3.8 Flash (High)Summary
By observing the correspondence between cells when rotating the \(N \times N\) grid by 90 degrees, the cells can be decomposed into mutually independent “cycles of size 4 (size 1 for the center cell when \(N\) is odd)”. For each cycle, we can use brute-force search to find the maximum score for each number of flipped cells (cost), and then apply knapsack DP (Dynamic Programming) to find the maximum number of dark-colored cells achievable with at most \(K\) flips.
Analysis
1. Condition for a Cell on the Paper to Become “Dark-Colored”
Let us consider 0-indexed coordinates (row \(0 \le r < N\), column \(0 \le c < N\)). According to the rotation operation in the problem statement, where each stamp cell is transferred to on the paper is as follows:
- First time (no rotation): Cell \((r, c)\) of the stamp overlaps with cell \((r, c)\) on the paper.
- Second time (rotated 90 degrees clockwise): Cell \((r, c)\) of the stamp overlaps with cell \((c, N - 1 - r)\) on the paper.
A cell \((R, C)\) on the paper becomes dark-colored when “ink is applied on both the first and second stampings”. - On the first stamping, the stamp cell that overlaps with \((R, C)\) on the paper is \((R, C)\). - On the second stamping, the stamp cell that rotates to \((R, C)\) on the paper is the one that maps there, which is \((N - 1 - C, R)\).
Therefore, the condition for cell \((R, C)\) on the paper to become dark-colored is that both cell \((R, C)\) and cell \((N - 1 - C, R)\) on the stamp have ink.
2. Decomposing Cells into “Cycles”
Consider the transformation \(f(r, c) = (c, N - 1 - r)\). Whether a cell \((r, c)\) has ink only affects the cells that transition into one another under this transformation.
Tracing the transitions of cells under \(f\): - General cells: form a cycle of length 4: \((r, c) \to (c, N - 1 - r) \to (N - 1 - r, N - 1 - c) \to (N - 1 - c, r) \to (r, c)\). - Center cell (the center \(( \frac{N-1}{2}, \frac{N-1}{2} )\) when \(N\) is odd): transitions to itself, forming a cycle of length 1.
3. Independence of Each Cycle
The crucial point is that cells in different cycles do not affect each other at all. The entire grid is partitioned into approximately \(N^2 / 4\) independent cycles.
- Cycle of length 4 \((v_0, v_1, v_2, v_3)\): Since each cell \(v_i\) rotates onto \(v_{i+1}\) (where \(v_4 = v_0\)), cell \(v_i\) on the paper becomes dark-colored if and only if “both \(v_i\) and \(v_{i-1}\) (\(v_{-1} = v_3\)) on the stamp have ink”.
- Cycle of length 1 \((v_0)\): If cell \(v_0\) on the stamp has ink, it overlaps on both the first and second stampings, so cell \(v_0\) on the paper becomes dark-colored.
There are at most 4 cells in each cycle. Therefore, there are at most \(2^4 = 16\) possible ink patterns for the cells within a cycle.
Thus, for each cycle, we can precompute the “maximum score (number of dark-colored cells) achievable by changing the ink state of \(c\) cells (cost \(c\))” for all possible values of \(c\).
4. Reduction to Knapsack DP
We can regard each cycle as an “item group”: - Item group \(i\): for cost \(c \in \{0, 1, \dots, L\}\), we obtain value \(S_i[c]\). - Upper bound on the total usable cost: \(K\).
This is a typical grouped knapsack problem. By combining all cycles using DP, we can find the maximum value achievable when the total cost is at most \(K\).
Algorithm
Extracting Cycles:
- Maintain a visited flag array
visitedand iterate through the \(N \times N\) grid. - Starting from unvisited cells, follow the transition \(f(r, c) = (c, N - 1 - r)\) to obtain the list of coordinates contained in each cycle.
- Maintain a visited flag array
Precomputing Maximum Scores for Each Cycle:
- Let \(L\) be the cycle length (\(L \in \{1, 4\}\)).
- Perform bit brute-force search over all \(2^L\) ink configurations.
- The flip cost \(c\) is the number of cells whose state differs from the initial configuration.
- The score is the number of adjacent cells (or the cell itself if \(L=1\)) that both have ink.
- For each cost \(c\) (\(0 \le c \le L\)), record the maximum achievable score
max_s[c].
Dynamic Programming (DP):
- If \(K > N^2\), capping at \(K = \min(K, N^2)\) is sufficient since flipping every cell requires at most \(N^2\) flips.
- \(dp[w]\): the maximum score when the total flip cost consumed so far is \(w\).
- Initial values: \(dp[0] = 0\), and \(-\infty\) for all other states.
- Process the cycles one by one and update the table: $\(next\_dp[w + c] = \max(next\_dp[w + c], dp[w] + \text{max\_s}[c])\)$
- After processing all cycles, \(\max_{0 \le w \le K} dp[w]\) is the answer.
Complexity
- Time Complexity: \(O(N^2 \cdot \min(K, N^2))\)
- The number of cycles \(M\) is at most \(\lceil \frac{N^2}{4} \rceil \approx 1600\).
- Precomputation for each cycle examines at most \(2^4 = 16\) configurations, taking \(O(N^2)\) in total.
- In the DP transitions, each cycle performs updates across length \(L \le 4\). The number of states is at most \(\min(K, N^2) \le 6400\).
- Therefore, the number of DP operations is at most \(M \times \min(K, N^2) \times 4 \approx 1600 \times 6400 \times 4 \approx 4 \times 10^7\) simple operations, which easily runs within the time limit.
- Space Complexity: \(O(N^2 + K)\)
- Storing the grid and managing cycles requires \(O(N^2)\) memory, and the DP table requires \(O(K)\) memory.
Implementation Notes
Pruning the DP Upper Bound: The maximum cost to consider in the DP can be bounded by the minimum of the sum of the lengths of the cycles processed so far (\(\sum L\)) and \(K\). Limiting the search range to the current total size significantly reduces the constant factor in execution time.
Index Correspondence: When the elements in a cycle are arranged as \((v_0, v_1, v_2, v_3)\), cell \(v_{i-1}\) rotates to the position of \(v_i\). Therefore, the condition to increment the score is that “both \(v_i\) and \(v_{(i + L - 1) \bmod L}\) are 1” (when \(L=1\), simply whether \(v_0 = 1\)). Writing this as
(i + L - 1) % Lis safe to avoid negative modulo.Source Code
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int N, K;
if (!(cin >> N >> K)) return 0;
vector<vector<int>> A(N, vector<int>(N));
for (int i = 0; i < N; ++i) {
for (int j = 0; j < N; ++j) {
cin >> A[i][j];
}
}
vector<vector<bool>> visited(N, vector<bool>(N, false));
vector<vector<int>> cycle_scores;
for (int r = 0; r < N; ++r) {
for (int c = 0; c < N; ++c) {
if (visited[r][c]) continue;
vector<pair<int, int>> cyc;
int cr = r, cc = c;
while (!visited[cr][cc]) {
visited[cr][cc] = true;
cyc.push_back({cr, cc});
int nr = cc;
int nc = N - 1 - cr;
cr = nr;
cc = nc;
}
int L = cyc.size();
vector<int> max_s(L + 1, -1);
for (int mask = 0; mask < (1 << L); ++mask) {
int cost = 0;
for (int i = 0; i < L; ++i) {
int b = (mask >> i) & 1;
if (b != A[cyc[i].first][cyc[i].second]) {
cost++;
}
}
int score = 0;
if (L == 1) {
score = (mask & 1);
} else {
for (int i = 0; i < L; ++i) {
int b1 = (mask >> i) & 1;
int b2 = (mask >> ((i + L - 1) % L)) & 1;
if (b1 && b2) {
score++;
}
}
}
max_s[cost] = max(max_s[cost], score);
}
cycle_scores.push_back(max_s);
}
}
K = min(K, N * N);
vector<int> dp(1, 0);
int current_max_w = 0;
for (const auto& S : cycle_scores) {
int L = S.size() - 1;
int next_max_w = min(K, current_max_w + L);
vector<int> next_dp(next_max_w + 1, -1);
for (int w = 0; w <= current_max_w; ++w) {
if (dp[w] < 0) continue;
for (int c = 0; c <= L; ++c) {
if (w + c <= next_max_w) {
next_dp[w + c] = max(next_dp[w + c], dp[w] + S[c]);
}
}
}
current_max_w = next_max_w;
dp = move(next_dp);
}
int ans = 0;
for (int w = 0; w <= current_max_w; ++w) {
ans = max(ans, dp[w]);
}
cout << ans << "\n";
return 0;
}
This editorial was generated by gemini-3.8-flash-high.