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

Fujiwara Wants to See Couples

Back to problem

Editorial

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

Define the degree of a cell as the number of cells sharing an edge with it. Interior cells have degree 444, boundary cells other than corners have degree 333, and corners have degree 222.

Color the grid as a checkerboard. No two cells of the same color share an edge. If we choose sss cells of just one color, the number of meetings is exactly the sum of their degrees. For each color, choose its sss cells of largest degree, and use whichever color gives the larger sum. Either color is allowed in a tie. Each color contains at least ⌊N2/2⌋≥s\lfloor N^2/2\rfloor\geq s⌊N2/2⌋≥s cells, so both candidates are feasible.

No sorting is necessary. Count the cells of degrees 4,3,24,3,24,3,2 in each color, and determine how many to select in that order. After choosing a color and the counts for each degree, scan the grid again and place the less numerous gender in the required number of cells. Fill all remaining cells with the other gender. This also handles s=0s=0s=0 by filling the entire grid with one gender.

The grid is scanned a constant number of times, giving O(N2)O(N^2)O(N2) time. Constructing and outputting one row at a time uses O(N)O(N)O(N) space.

Proof

We must show that restricting the chosen cells to one checkerboard color can attain a global optimum. In an arbitrary arrangement, call the cells occupied by the less numerous gender selected. Let FFF be the number of meetings, uuu the number of selected interior cells, ccc the number of selected corners, and eee the number of edges whose two endpoints are selected. Summing the degrees of selected cells and subtracting such internal edges twice gives

F=3s+u−c−2e.F=3s+u-c-2e.

Solution written by GPT6

F=
3s+
u−
c−
2e.

Since every degree is at most 444, we also have F≤4sF\leq4sF≤4s.

Let a=⌈(N−2)2/2⌉a=\lceil(N-2)^2/2\rceila=⌈(N−2)2/2⌉. The largest set of pairwise nonadjacent cells in the interior (N−2)×(N−2)(N-2)\times(N-2)(N−2)×(N−2) grid has size aaa. To see the upper bound, follow a snake path through all interior cells, reversing direction in each row. No two consecutive cells on this path can both be chosen, so at most aaa can be chosen. The larger checkerboard color attains aaa. For N=2N=2N=2, the interior is empty and a=0a=0a=0.

Let eIe_IeI​ count the edges whose endpoints are both selected interior cells. Removing one endpoint of each such edge leaves at least u−eIu-e_Iu−eI​ pairwise nonadjacent interior cells. Therefore u−eI≤au-e_I\leq au−eI​≤a. Since eI≤ee_I\leq eeI​≤e, we obtain

F≤3s+a−c−2e+eI≤3s+a.F\leq3s+a-c-2e+e_I\leq3s+a.F≤3s+a−c−2e+eI​≤3s+a.

First suppose NNN is even. Partition the grid into disjoint 2×22\times22×2 squares. If a square contains xxx selected cells, at most 2min⁡(x,4−x)2\min(x,4-x)2min(x,4−x) of its four edges join different genders. Thus, at least ∣4−2x∣|4-2x|∣4−2x∣ of its edges join the same gender. Summing over the squares shows that the whole grid contains at least N2−2sN^2-2sN2−2s edges joining the same gender. There are 2N(N−1)2N(N-1)2N(N−1) edges in total, so

F≤2s+N(N−2).F\leq2s+N(N-2).F≤2s+N(N−2).

For even NNN, each checkerboard color contains aaa degree-444 cells, 2N−42N-42N−4 degree-333 cells, and two degree-222 cells. Selecting cells in decreasing degree order gives the sum

min⁡(4s, 3s+a, 2s+N(N−2)).\min\bigl(4s,\ 3s+a,\ 2s+N(N-2)\bigr).min(4s, 3s+a, 2s+N(N−2)).

This attains the smallest of the three upper bounds, proving optimality.

Now suppose NNN is odd, and write m=(N2−1)/2m=(N^2-1)/2m=(N2−1)/2, so s≤ms\leq ms≤m. Equality F=3s+aF=3s+aF=3s+a requires c+2e−eI=0c+2e-e_I=0c+2e−eI​=0 in the preceding inequalities. Since eI≤ee_I\leq eeI​≤e, this forces c=e=0c=e=0c=e=0 and hence u=au=au=a.

The interior contains 2a−12a-12a−1 cells. On its snake path, the only way to choose aaa pairwise nonadjacent cells is to choose every other cell, starting from the first. Consequently, the interior has a unique maximum independent set: the checkerboard color containing its corners. This is also the color of the four corners of the whole grid.

Once those interior cells are selected, every boundary cell of the opposite color is adjacent to a selected interior cell. Such cells cannot be added because e=0e=0e=0, and corners cannot be added because c=0c=0c=0. Only the 2N−62N-62N−6 noncorner boundary cells of the same color remain available. Thus, equality F=3s+aF=3s+aF=3s+a implies s≤a+2N−6=m−3s\leq a+2N-6=m-3s≤a+2N−6=m−3. For s≥m−2s\geq m-2s≥m−2, integrality therefore gives the stronger bound

F≤3s+a−1.F\leq3s+a-1.F≤3s+a−1.

The color containing the four corners has a,2N−6,4a,2N-6,4a,2N−6,4 cells of degrees 4,3,24,3,24,3,2, respectively. The other color has a−1,2N−2,0a-1,2N-2,0a−1,2N−2,0 such cells. Selecting in decreasing degree order attains all the relevant bounds:

  • For 0≤s≤a0\leq s\leq a0≤s≤a, the color containing the corners gives F=4sF=4sF=4s.
  • For a<s≤m−3a<s\leq m-3a<s≤m−3, that color gives F=3s+aF=3s+aF=3s+a.
  • For s=m−2s=m-2s=m−2, either color gives F=3s+a−1F=3s+a-1F=3s+a−1.
  • For s=m−1s=m-1s=m−1 or s=ms=ms=m, the color without corners gives F=3s+a−1F=3s+a-1F=3s+a.

When N=3N=3N=3, the second range is empty; the other cases still apply. Hence the better of the two candidates also attains a global upper bound when NNN is odd, completing the proof.

−
1