A16271 | Cyclic MEX
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
For an array $a$ , define its cost as $\sum_{i=1}^{n} \operatorname{mex} ^\dagger ([a_1,a_2,\ldots,a_i])$ .
You are given a permutation $^\ddagger$ $p$ of the set $\{0,1,2,\ldots,n-1\}$ . Find the maximum cost across all cyclic shifts of $p$ .
$^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m])$ is the smallest non-negative integer $x$ such that $x$ does not occur among $b_1,b_2,\ldots,b_m$ .
$^\ddagger$ A permutation of the set $\{0,1,2,...,n-1\}$ is an array consisting of $n$ distinct integers from $0$ to $n-1$ in arbitrary order. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ( $1$ appears twice in the array), and $[0,2,3]$ is also not a permutation ( $n=3$ but there is $3$ in the array).
You are given a permutation $^\ddagger$ $p$ of the set $\{0,1,2,\ldots,n-1\}$ . Find the maximum cost across all cyclic shifts of $p$ .
$^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m])$ is the smallest non-negative integer $x$ such that $x$ does not occur among $b_1,b_2,\ldots,b_m$ .
$^\ddagger$ A permutation of the set $\{0,1,2,...,n-1\}$ is an array consisting of $n$ distinct integers from $0$ to $n-1$ in arbitrary order. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ( $1$ appears twice in the array), and $[0,2,3]$ is also not a permutation ( $n=3$ but there is $3$ in the array).
输入格式
Each test consists of multiple test cases. The first line contains a single integer $t$ ( $1 \le t \le 10^5$ ) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^6$ ) — the length of the permutation $p$ .
The second line of each test case contain $n$ distinct integers $p_1, p_2, \ldots, p_n$ ( $0 \le p_i < n$ ) — the elements of the permutation $p$ .
It is guaranteed that sum of $n$ over all test cases does not exceed $10^6$ .
The first line of each test case contains a single integer $n$ ( $1 \le n \le 10^6$ ) — the length of the permutation $p$ .
The second line of each test case contain $n$ distinct integers $p_1, p_2, \ldots, p_n$ ( $0 \le p_i < n$ ) — the elements of the permutation $p$ .
It is guaranteed that sum of $n$ over all test cases does not exceed $10^6$ .
输出格式
For each test case, output a single integer — the maximum cost across all cyclic shifts of $p$ .
输入输出样例
输入 #1
4 6 5 4 3 2 1 0 3 2 1 0 8 2 3 6 7 0 1 4 5 1 0
输出 #1
15 5 31 1
In the first test case, the cyclic shift that yields the maximum cost is $[2,1,0,5,4,3]$ with cost $0+0+3+3+3+6=15$ .
In the second test case, the cyclic shift that yields the maximum cost is $[0,2,1]$ with cost $1+1+3=5$ .
In the second test case, the cyclic shift that yields the maximum cost is $[0,2,1]$ with cost $1+1+3=5$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted