Swapping the two genders does not change the number of meetings. Let , choose the cells occupied by the less numerous gender, and fill the other cells with the other gender. If , either gender may be chosen. If , fill the whole grid with one gender.
For , the minimum is the smaller of the following two values:
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 and . 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 without using the fractional part of a square root: it is when , when , and when . Find the integer satisfying .
The second construction fills 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 , 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 output cells, giving a time complexity of . Storing one output row requires space.
Proof
Think of the 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 and colored cells, there are originally at least boundary edges between them. After the move, there are exactly . Thus, this transformation cannot increase the boundary length.
Solution written by GPT6
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 , and let be the number of rows satisfying . The boundary length is
Consider the following four cases.
Every arrangement can be transformed into this form without increasing its boundary length. Consequently, every original arrangement also has boundary length at least .
In the first construction, the height plus width of the colored shape equals , and its row counts are nonincreasing. Its boundary length is therefore at most . 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 when . When , only part of the first row is colored, and the boundary length is . Thus, this construction always has boundary length at most .
Choosing the construction with the smaller bound gives a boundary length at most , while the lower bound proves that every arrangement has at least this boundary length. The chosen arrangement is therefore optimal.