A16147 | Rivalries
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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!
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!
输入格式
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$ .
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$ .
输出格式
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.
If there exist multiple rivals with the maximum sum, output any of them.
输入输出样例
输入 #1
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
输出 #1
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
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$ .
$[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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted