#P1761. Visible Squares
Visible Squares
Visible Squares
We are given n squares of distinct sizes. The i-th square has side length si. They are placed one by one in the first quadrant with a fixed 45° rotation. In the placement, each square is positioned so that it touches the x-axis at exactly one point (its bottom vertex) and the entire square lies in the first quadrant. Moreover, a newly placed square must not overlap any already placed squares and its contact point with the x-axis should have the smallest possible x-coordinate.
When a square of side s is rotated by 45°, if we assume its bottom vertex is at (p,0) then its vertices will be located at:
- Bottom: (p, 0)
- Left: (p - s/\sqrt{2}, s/\sqrt{2})
- Top: (p, s\sqrt{2})
- Right: (p + s/\sqrt{2}, s/\sqrt{2})
Thus, the square’s horizontal span is [p - s/\sqrt{2}, p + s/\sqrt{2}].
The placement rule is defined as follows. For the i-th square (with side si), let pi be the x-coordinate of its touching point with the x-axis. It must satisfy both:
- pi ≥ si/\sqrt{2} (so the whole square lies in the first quadrant), and
- For every square j placed before it, the new square does not overlap the j-th square. One may show that a necessary and sufficient condition is:
pi ≥ pj + \sqrt{2}\,\min(si, sj) for all j < i.
Thus, the minimal valid pi is given by:
pi = \max\Bigl(\frac{si}{\sqrt{2}},\; \max_{1 \le j < i}\Bigl(pj + \sqrt{2}\,\min(si, sj)\Bigr)\Bigr).
Once all squares are placed, we view them from above (i.e. in the negative y-direction). Since squares are stacked in the order they are placed (with later ones drawn over earlier ones), a square is considered visible if there exists at least one point in its horizontal span which is not covered by any square placed after it. In other words, let the i-th square’s interval be [Li, Ri</sub]] with
Li = pi - si/\sqrt{2} \quad and \quad Ri = pi + si/\sqrt{2}.
Then, square i is invisible if the union of the intervals of all squares placed after it completely covers [Li, Ri]; otherwise, it is visible. Your task is to output the indices (1-indexed) of all squares that are visible when viewed from above, in increasing order.
inputFormat
The input consists of two lines. The first line contains an integer n (n > 0) representing the number of squares. The second line contains n positive integers, where the i-th integer is the side length si of the i-th square.
outputFormat
Output a single line containing the indices (1-indexed) of all squares that are visible when viewed from above. The indices should be printed in increasing order and separated by a single space.
sample
5
1 2 3 2 5
1 2 3 4 5