DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Uncool Grid

Back to problem

Editorial

Let the resulting grid be BBB.

The key condition is

Bi,j+1≠Bi+1,j(1≤i<N, 1≤j<M).B_{i,j+1} \neq B_{i+1,j}\qquad(1\leq i<N,\ 1\leq j<M).Bi,j+1​=Bi+1,j​(1≤i<N, 1≤j<M).

If this condition fails at some (i,j)(i,j)(i,j), take the same path up to (i,j)(i,j)(i,j), then use Right-Down on one path and Down-Right on the other, and use the same suffix from (i+1,j+1)(i+1,j+1)(i+1,j+. The only different visited cells have equal characters, so the two paths produce the same string.

Conversely, consider two distinct shortest paths and the first point where their moves differ. Their next cells are some (i,j+1)(i,j+1)(i,j+1) and (i+1,j)(i+1,j)(i+1,j). The condition makes these two characters different, so the two path strings are different.

Thus the grid is not cool if and only if the condition holds everywhere.

Cells with the same value of i+ji+ji+j form an anti-diagonal. The condition says that each anti-diagonal must alternate between 000 and 111. Each anti-diagonal therefore has exactly two valid patterns, and different anti-diagonals are independent.

Consider one anti-diagonal of length LLL. Use Pi,j=(i−1) mod 2P_{i,j}=(i-1)\bmod 2Pi,j​=(i−1)mod as a reference pattern, and let be the number of cells on this anti-diagonal where differs from . Choosing the reference pattern costs flips, while choosing its complement costs flips.

The minimum cost on this anti-diagonal is

m=min⁡(c,L−c),m=\min(c,L-c),m=min(c,L−c),

and switching to the more expensive choice adds

w=∣L−2c∣.w=|L-2c|.w=∣L−2c∣.

Let SSS be the sum of all minimum costs. Since the anti-diagonal lengths sum to NMNMNM, the maximum possible cost is NM−SNM-SNM−S.

It remains to show that every integer between these two bounds is attainable. Sort the anti-diagonals by nondecreasing length. For an anti-diagonal of length LLL, the value www has the same parity as LLL and satisfies 0≤w≤L0\leq w\leq L0≤w≤L. Before the first anti-diagonal of length L≥2L\geq2, there are two anti-diagonals of each smaller length. Length contributes , and every odd length contributes at least . Therefore, if is the sum of previous weights, then . Later anti-diagonals of the same length only increase . Hence

w≤P+1w\leq P+1w≤P+1

always holds.

If the previous weights can make every subset sum from 000 through PPP and the new weight satisfies w≤P+1w\leq P+1w≤P+1, then after adding it every value from 000 through P+wP+w is attainable. Induction from the first length- anti-diagonal proves that the subset sums of all weights form one continuous interval.

Therefore, a solution with exactly KKK flips exists if and only if

S≤K≤NM−S.S\leq K\leq NM-S.S≤K≤NM−S.

To construct the choices, store prefix sums of the sorted weights and set R=K−SR=K-SR=K−S. Process the weights in reverse order. If the current RRR is larger than the sum of all previous weights, the current weight must be selected; switch this anti-diagonal to its more expensive pattern and subtract www from RRR. Otherwise keep the cheaper pattern. The interval property guarantees that this ends with .

One scan of the grid computes LLL and ccc for every anti-diagonal, followed by sorting N+M−1N+M-1N+M−1 anti-diagonals. The time complexity is O(NM+(N+M)log⁡(N+M))O(NM+(N+M)\log(N+M))O, and the auxiliary memory complexity excluding the output grid is .

Solution written by GPT5.6

1
)
2
ccc
AAA
PPP
ccc
L−cL-cL−c
L≥
2
111
w=1w=1w=1
111
PPP
P≥L−1P\geq L-1P≥L−1
PPP
P
+
w
111
R=0R=0
R=0
(
N
M
+
(N+
M)log(N+
M))
O(N+M)O(N+M)O(N+M)