No.3238 Shadow
Problem Statement
You are given a permutation $(P_1,P_2,\ldots,P_N)$ of $(1,2,\ldots,N)$. Place $N$ points $(i,P_i)$ on the coordinate plane.
When $2$ points $A=(a_1,a_2),B=(b_1,b_2)$ satisfy $a_1\lt b_1$ and $a_2\lt b_2$, we say that $B$ is in the shadow of $A$.
Perform the following operation.
- Among the points currently on the plane, let $S$ be the set of points that are not in the shadow of any point.
- Output the minimum $x$-coordinate and the minimum $y$-coordinate of the points in $S$.
- Remove the points in $S$ from the plane.
- If there are no points on the plane, terminate; otherwise, return to 1.
Constraints
- $1\le N\le 2\times 10^5$
- $(P_1,P_2,\ldots,P_N)$ is a permutation of $(1,2,\ldots,N)$
- All inputs are integers
Input
$N$ $P_1$ $P_2$ $\cdots$ $P_N$
Output
Let $M$ be the number of outputs, and let $x_i, y_i$ be the minimum $x$-coordinate and the minimum $y$-coordinate, respectively, in the $i$-th output. Print in the following format.
$x_1$ $y_1$ $x_2$ $y_2$ $\vdots$ $x_M$ $y_M$
Sample
Sample 1
Input
5 2 4 3 1 5
Output
1 1 2 3 5 5
Place the $5$ points $(1,2), (2,4), (3,3), (4,1), (5,5)$. The set of points that are not in the shadow of any point is $S=\{(1,2),(4,1)\}$. Output 1 1.
After removing the points in $S$, the remaining points are $(2,4), (3,3), (5,5)$. At this point, $S=\{(2,4),(3,3)\}$. Output 2 3.
After removing the points in $S$ again, the remaining point is $(5,5)$. Output 5 5.
After that, there are no points left, so the process terminates.
Sample 2
Input
4 4 3 2 1
Output
1 1
Sample 3
Input
9 9 6 4 2 8 5 3 1 7
Output
1 1 5 3 9 7
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。