Editorial
It is enough to determine the number of blocks in each row independently. The column lasers are not actually needed.
Suppose a row contains blocks. If we fire a laser of strength from left to right in this row, then the following holds.
- If , the laser penetrates all blocks and exits the grid.
- If , the laser stops at the -th block.
Therefore, if the result is , the number of blocks in that row is at most . Otherwise, it is greater than . A single experiment tells us this comparison for every row.
Since , we can find the number of blocks in each row by binary search. The key observation is that in one experiment, we may choose different strengths for all rows simultaneously. Thus, we can run the binary searches for all rows in parallel.
For each row , maintain the possible range , and set as the strength of the row laser. The column laser strengths can be arbitrary, so we set them to .
After the experiment, update as follows.
- If the laser in row exits the grid, then the row has at most blocks, so set .
- Otherwise, the row has more than blocks, so set .
Since , each row has one of at most possible counts, so at most experiments are enough. This is within the limit of .
When for every row, is the number of blocks in row . The answer is
The time complexity is .