Codeforces Round 987 (Div. 2)
D
Penchick and Desert Rabbit
Codeforces
1700
原题
← CE →
翻译中…

Dedicated to pushing himself to his limits, Penchick challenged himself to survive the midday sun in the Arabian Desert!

翻译中…

While trekking along a linear oasis, Penchick spots a desert rabbit preparing to jump along a line of palm trees. There are $n$ trees, each with a height denoted by $a_i$.

翻译中…

The rabbit can jump from the $i$-th tree to the $j$-th tree if exactly one of the following conditions is true:

翻译中…
  • $j \lt i$ and $a_j \gt a_i$: the rabbit can jump backward to a taller tree.
  • $j \gt i$ and $a_j \lt a_i$: the rabbit can jump forward to a shorter tree.
翻译中…

For each $i$ from $1$ to $n$, determine the maximum height among all trees that the rabbit can reach if it starts from the $i$-th tree.

翻译中…

Input

翻译中…

The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^5$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($1 \leq n \leq 5 \cdot 10^5$) — the number of trees.

翻译中…

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — the height of the trees.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, output $n$ integers. The $i$-th integer should contain the maximum height among all trees that the rabbit can reach if it starts from the $i$-th tree.

翻译中…

Note

翻译中…

In the first test case, the initial heights of trees are $a = [2, 3, 1, 4]$.

翻译中…
  • If the rabbit starts from the first tree, it can jump to the third tree as $3 \gt 1$ and $1 \lt 2$. Then, the rabbit can jump to the second tree as $2 \lt 3$ and $3 \gt 1$. It can be proved that the rabbit cannot reach the fourth tree; hence, the maximum height of the tree that the rabbit can reach is $a_2 = 3$.
  • If the rabbit starts from the fourth tree, it does not need to jump anywhere as it is already at the highest tree.
翻译中…

In the second test case, the rabbit can jump to the first tree regardless of which tree it starts from.

翻译中…

In the fifth test case, if the rabbit starts from the fifth tree, it can jump to the fourth tree. Then the rabbit can jump to the seventh tree and finally reach the sixth tree. Therefore, the maximum height of the tree that the rabbit can reach is $8$.

翻译中…

样例

Input #1
5
4
2 3 1 4
5
5 4 3 2 1
4
2 1 1 3
4
1 1 3 1
8
2 4 1 6 3 8 5 7
Output #1
3 3 3 4
5 5 5 5 5
2 2 2 3
1 1 3 3
8 8 8 8 8 8 8 8
InputCopy OutputCopy
题目信息
题号
2031D
时间限制
3 s
内存限制
256 MB
标签
二分
数据结构
DFS 及类似
动态规划
并查集
贪心
模拟
双指针

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。