Editorial for OTHS Coding Competition 4 P3 - Photo


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Authors: Ivan_Li, htoshiro

Subtask 1

Sorting everyone by their height from short to tall gives the optimal arrangement, so we can count how many people are taller than the person before them after sorting.

Time Complexity: \mathcal{O}(RC\log{RC})

Subtask 2

In the first row, place as many people with height 1 as possible. In the second row, place as many people with height 2 as possible. Anyone who has not been placed yet will not be visible in the photo, so the answer will be the number of people we have placed so far.

Time Complexity: \mathcal{O}(RC)

Subtask 3

Let F_i be the number of people with a height of i. We can get all these frequencies using a hash table. By the pigeonhole principle, at most C people with the same height can be visible at once, so the answer will be the sum of \min(F_i, C) over all valid i.

Time Complexity: \mathcal{O}(RC), using constant time hash table lookups.

Alternate Solution

Sort everyone by their height from short to tall and place them in row-major order. The subtask 3 solution can give intuition on why this is optimal. We can then count how many people are visible, only needing to compare heights with the person in the row immediately before, since we sorted everyone.

Time Complexity: \mathcal{O}(RC\log{RC})


Comments


  • 0
    ocean_water  commented on Aug. 5, 2025, 9:49 p.m. edited

    Greedy Solution:

    Sort everyone by height in ascending order and place them in the grid row by row—i.e., fill row 0, then row 1, then row 2, and so on, from left to right. Then, count how many people are visible.

    Why does this approach work? Here's a mathematical explanation:

    Assume there exists a valid solution represented as grid[R][C]. We can show that the solution is equivalent to the greedy one through a series of transformations.

    Row 0: Since each column contains R people, we can shuffle the columns without affecting the number of visible people. By rearranging the columns, we can sort Row 0 in ascending order. This does not change the visibility count.

    Row 1: Now fix Row 0 and consider Row 1. Each new column contains R-1 people excluding Row 0, so we can again shuffle the columns. By rearranging the columns, we can sort Row 1 in ascending order while maintaining visibility.

    Here's why: suppose in Row 1, person i is taller than person j (i.e., grid[1][i] > grid[1][j]). We want to swap columns i and j. We know:

    grid[1][i] > grid[0][i] , grid[1][j] > grid[0][j] (both are visible), and grid[0][j] > grid[0][i], So after swapping:

    grid[1][j] > grid[0][i] and grid[1][i] > grid[0][j], which means both people remain visible in their new positions. Thus, visibility count remains unchanged.

    Repeat for Rows 2, 3, ..., R-1: Apply the same logic iteratively: fix all previous rows, shuffle columns to sort the current row while maintaining visibility.

    By the end of this process, each row is sorted in ascending order, and the final grid matches the one produced by the greedy algorithm.

    by Ocean_water @cinco ranch