310566: CF1852E. Rivalries

Memory Limit:512 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

E. Rivalriestime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output

Ntarsis has an array $a$ of length $n$.

The power of a subarray $a_l \dots a_r$ ($1 \leq l \leq r \leq n$) is defined as:

  • The largest value $x$ such that $a_l \dots a_r$ contains $x$ and neither $a_1 \dots a_{l-1}$ nor $a_{r+1} \dots a_n$ contains $x$.
  • If no such $x$ exists, the power is $0$.

Call an array $b$ a rival to $a$ if the following holds:

  • The length of both $a$ and $b$ are equal to some $n$.
  • Over all $l, r$ where $1 \leq l \leq r \leq n$, the power of $a_l \dots a_r$ equals the power of $b_l \dots b_r$.
  • The elements of $b$ are positive.

Ntarsis wants you to find a rival $b$ to $a$ such that the sum of $b_i$ over $1 \leq i \leq n$ is maximized. Help him with this task!

Input

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

The first line of each test case has a single integer $n$ ($1 \leq n \leq 10^5$).

The next line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$).

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

Output

For each test case, output $n$ integers $b_1, b_2, \ldots, b_n$ — a valid rival to $a$ such that $b_1 + b_2 + \cdots + b_n$ is maximal.

If there exist multiple rivals with the maximum sum, output any of them.

ExampleInput
7
5
1 4 1 3 3
5
1 4 1 8 8
5
2 1 1 1 2
8
3 2 3 5 2 2 5 3
8
1 1 1 1 4 3 3 3
10
1 9 5 9 8 1 5 8 9 1
16
1 1 1 1 5 5 5 5 9 9 9 9 7 7 7 7
Output
2 4 2 3 3
3 4 3 8 8
2 1 2 1 2
4 2 4 5 5 2 5 4
1 2 2 1 4 3 2 3
7 9 5 9 8 9 5 8 9 7
1 8 8 1 5 8 8 5 9 9 9 9 7 8 8 7
Note

For the first test case, one rival with the maximal sum is $[2, 4, 2, 3, 3]$.

$[2, 4, 2, 3, 3]$ can be shown to be a rival to $[1, 4, 1, 3, 3]$.

All possible subarrays of $a$ and $b$ and their corresponding powers are listed below:

  • The power of $a[1:1] = [1] = 0$, the power of $b[1:1] = [2] = 0$.
  • The power of $a[1:2] = [1, 4] = 4$, the power of $b[1:2] = [2, 4] = 4$.
  • The power of $a[1:3] = [1, 4, 1] = 4$, the power of $b[1:3] = [2, 4, 2] = 4$.
  • The power of $a[1:4] = [1, 4, 1, 3] = 4$, the power of $b[1:4] = [2, 4, 2, 3] = 4$.
  • The power of $a[1:5] = [1, 4, 1, 3, 3] = 4$, the power of $b[1:5] = [2, 4, 2, 3, 3] = 4$.
  • The power of $a[2:2] = [4] = 4$, the power of $b[2:2] = [4] = 4$.
  • The power of $a[2:3] = [4, 1] = 4$, the power of $b[2:3] = [4, 2] = 4$.
  • The power of $a[2:4] = [4, 1, 3] = 4$, the power of $b[2:4] = [4, 2, 3] = 4$.
  • The power of $a[2:5] = [4, 1, 3, 3] = 4$, the power of $b[2:5] = [4, 2, 3, 3] = 4$.
  • The power of $a[3:3] = [1] = 0$, the power of $b[3:3] = [2] = 0$.
  • The power of $a[3:4] = [1, 3] = 0$, the power of $b[3:4] = [2, 3] = 0$.
  • The power of $a[3:5] = [1, 3, 3] = 3$, the power of $b[3:5] = [2, 3, 3] = 3$.
  • The power of $a[4:4] = [3] = 0$, the power of $b[4:4] = [3] = 0$.
  • The power of $a[4:5] = [3, 3] = 3$, the power of $b[4:5] = [3, 3] = 3$.
  • The power of $a[5:5] = [3] = 0$, the power of $b[5:5] = [3] = 0$.

It can be shown there exists no rival with a greater sum than $2 + 4 + 2 + 3 + 3 = 14$.

Input

题意翻译

Ntarsis 有一个长为 $n$ 的数组 $a$。 定义一个子数组 $a_l \cdots a_r$ 的权重为: - 在整个数组 $a$ 中,只出现在该子数组 $a_l \cdots a_r$中的最大元素 $x$。 - 若不存在这样的 $x$,权重为 $0$。 称数组 $b$ 与 $a$ 数组匹配,当且仅当满足: - $2$ 者长度相等。 - 数组 $a$ 中的每个子数组的权重都与数组 $b$ 对应的子数组的权重相等。 - $b$ 中的数都为正数。 最大化 $b$ 的权值和。 $1 \le n,t \le 10^5$

Output

题目大意:
Ntarsis有一个长度为n的数组a。子数组a_l ... a_r(1≤l≤r≤n)的力定义为:
- x的最大值,使得a_l ... a_r包含x,且a_1 ... a_{l-1}和a_{r+1} ... a_n不包含x。
- 如果不存在这样的x,则力为0。

称数组b为a的对手,如果满足以下条件:
- a和b的长度都等于某个n。
- 对于所有1≤l≤r≤n的l, r,a_l ... a_r的力等于b_l ... b_r的力。
- b的元素是正数。

Ntarsis希望你找到一个对手b到a,使得b_i的总和在1≤i≤n时最大。帮助他完成这个任务!

输入数据格式:
每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤10^5)。接下来是测试用例的描述。
每个测试用例的第一行有一个整数n(1≤n≤10^5)。
下一行包含n个整数a_1, a_2, …, a_n(1≤a_i≤10^9)。
保证所有测试用例的n之和不超过2×10^5。

输出数据格式:
对于每个测试用例,输出n个整数b_1, b_2, …, b_n——一个有效的对手b到a,使得b_1 + b_2 + … + b_n最大。
如果存在多个最大和的对手,输出其中任何一个。题目大意: Ntarsis有一个长度为n的数组a。子数组a_l ... a_r(1≤l≤r≤n)的力定义为: - x的最大值,使得a_l ... a_r包含x,且a_1 ... a_{l-1}和a_{r+1} ... a_n不包含x。 - 如果不存在这样的x,则力为0。 称数组b为a的对手,如果满足以下条件: - a和b的长度都等于某个n。 - 对于所有1≤l≤r≤n的l, r,a_l ... a_r的力等于b_l ... b_r的力。 - b的元素是正数。 Ntarsis希望你找到一个对手b到a,使得b_i的总和在1≤i≤n时最大。帮助他完成这个任务! 输入数据格式: 每个测试包含多个测试用例。第一行包含测试用例数t(1≤t≤10^5)。接下来是测试用例的描述。 每个测试用例的第一行有一个整数n(1≤n≤10^5)。 下一行包含n个整数a_1, a_2, …, a_n(1≤a_i≤10^9)。 保证所有测试用例的n之和不超过2×10^5。 输出数据格式: 对于每个测试用例,输出n个整数b_1, b_2, …, b_n——一个有效的对手b到a,使得b_1 + b_2 + … + b_n最大。 如果存在多个最大和的对手,输出其中任何一个。

加入题单

上一题 下一题 算法标签: