DOJ logoDOJ
Problems
Recently added problemsSaved problems
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

Ishigami Does Not Want to See Couples

Back to problem

Editorial

Swapping the two genders does not change the number of meetings. Let s=min⁡(B,G)s=\min(B,G)s=min(B,G), choose the sss cells occupied by the less numerous gender, and fill the other cells with the other gender. If B=GB=GB=G, either gender may be chosen. If s=0s=0s=0, fill the whole grid with one gender.

For s>0s>0s>0, the minimum is the smaller of the following two values:

C1=⌈2s⌉,C2={Nif s is divisible by N,N+1otherwise.C_1=\left\lceil2\sqrt{s}\right\rceil,\qquad C_2=\begin{cases}N&\text{if }s\text{ is divisible by }N,\\N+1&\text{otherwise.}\end{cases}C1​=⌈2s

We can construct an arrangement corresponding to each value, so compare them and output the construction for the smaller one.

The first construction forms an almost square shape in the upper-left corner. Let p=⌊s⌋p=\lfloor\sqrt{s}\rfloorp=⌊s​⌋ and r=s−p2r=s-p^2r=. First fill a square in that corner. Next, fill the top cells of the column immediately to its right. If , also fill the leftmost cells of the row immediately below the square. Since , this fills exactly cells.

Compute C1C_1C1​ without using the fractional part of a square root: it is 2p2p2p when r=0r=0r=0, 2p+12p+12p when , and when . Find the integer satisfying .

The second construction fills sss cells by processing rows from top to bottom and cells within each row from left to right. It consists of several full rows, followed by at most one partially filled row.

Choose the first construction when C1<C2C_1<C_2C1​<C2​, and the second otherwise. The number of selected cells in each row can be calculated directly, so generate and output one row at a time. There are N2N^2N output cells, giving a time complexity of . Storing one output row requires space.

Proof

Think of the sss cells chosen for one gender as colored. The boundary length is the number of edges between cells of different colors, which equals the number of meetings.

First move all colored cells in each row to its left end. If a row contains both colors, it originally has at least one horizontal boundary edge; afterward, it has exactly one. If two adjacent rows have xxx and yyy colored cells, there are originally at least ∣x−y∣|x-y|∣x−y∣ boundary edges between them. After the move, there are exactly . Thus, this transformation cannot increase the boundary length.

Solution written by GPT6

​
⌉
,
C2​
=
{NN+1​if s is divisible by N,otherwise.​
s
−
p2
p×pp\times pp×p
min⁡(r,p)\min(r,p)min(r,p)
r>pr>pr>p
r−pr-pr−p
0≤r≤2p0\leq r\leq2p0≤r≤2p
sss
+
1
1≤r≤p1\leq r\leq p1≤r≤p
2p+22p+22p+2
p<r≤2pp<r\leq2pp<r≤2p
ppp
p2≤s<(p+1)2p^2\leq s<(p+1)^2p2≤s<(p+1)2
2
O(N2)O(N^2)O(N2)
O(N)O(N)O(N)
∣x−y∣|x-y|
∣x−y∣

Next sort the rows by their numbers of colored cells in descending order. The number of horizontal boundary edges stays the same. The number of vertical boundary edges is the sum of the absolute differences between adjacent row counts. In any order, this sum is at least the difference between the largest and smallest counts. Descending order attains that difference, so sorting cannot increase the boundary length either.

Write the sorted row counts as ℓ1≥ℓ2≥⋯≥ℓN\ell_1\geq\ell_2\geq\cdots\geq\ell_Nℓ1​≥ℓ2​≥⋯≥ℓN​, and let qqq be the number of rows satisfying 0<ℓi<N0<\ell_i<N0<ℓi​<N. The boundary length is

E=q+ℓ1−ℓN.E=q+\ell_1-\ell_N.E=q+ℓ1​−ℓN​.

Consider the following four cases.

  • If ℓN=0\ell_N=0ℓN​=0 and ℓ1<N\ell_1<Nℓ1​<N, every nonempty row is partially colored. Hence s≤qℓ1s\leq q\ell_1s≤qℓ1​, so E=q+ℓ1≥2sE=q+\ell_1\geq2\sqrt{s}E=q+ℓ1​≥2s and therefore E≥C1E\geq C_1E≥C1​.
  • If ℓN>0\ell_N>0ℓN​>0 and ℓ1=N\ell_1=Nℓ1​=N, swapping the colors and rotating the grid by degrees reduces the arrangement to the preceding case. There are now colored cells, so .
  • If ℓN=0\ell_N=0ℓN​=0 and ℓ1=N\ell_1=Nℓ1​=N, then E=. If , then is divisible by . Therefore, when is divisible by , and otherwise. In either case, .
  • If ℓN>0\ell_N>0ℓN​>0 and ℓ1<N\ell_1<Nℓ1​<N, then every row is partially colored, so q. If , all row counts are equal and is divisible by . As in the preceding case, .

Every arrangement can be transformed into this form without increasing its boundary length. Consequently, every original arrangement also has boundary length at least min⁡(C1,C2)\min(C_1,C_2)min(C1​,C2​).

In the first construction, the height plus width of the colored shape equals C1C_1C1​, and its row counts are nonincreasing. Its boundary length is therefore at most C1C_1C1​. This bound remains valid if the shape touches the edge of the grid, removing some boundary edges.

In the second construction, the boundary length equals C2C_2C2​ when s≥Ns\geq Ns≥N. When 0<s<N0<s<N0<s<N, only part of the first row is colored, and the boundary length is s+1≤N+1=C2s+1\leq N+1=C_2s+1≤N+1=C2​. Thus, this construction always has boundary length at most C2C_2C2​.

Choosing the construction with the smaller bound gives a boundary length at most min⁡(C1,C2)\min(C_1,C_2)min(C1​,C2​), while the lower bound proves that every arrangement has at least this boundary length. The chosen arrangement is therefore optimal.

​
180180
180
N2−s≥sN^2-s\geq sN2−s≥s
E≥⌈2N2−s⌉≥C1E\geq\lceil2\sqrt{N^2-s}\rceil\geq C_1E≥⌈2N2−s​⌉≥C1​
N+qE=N+q
E=N+q
q=0q=0q=0
sss
NNN
E≥NE\geq NE≥N
sss
NNN
E≥N+1E\geq N+1E≥N+1
E≥C2E\geq C_2E≥C2​
=Nq=N
q=N
ℓ1=ℓN\ell_1=\ell_Nℓ1​=ℓN​
sss
NNN
E=N+ℓ1−ℓN≥C2E=N+\ell_1-\ell_N\geq C_2E=N+ℓ1​−ℓN​≥C2​