CF1905D.Cyclic MEX
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
For an array a, define its cost as ∑i=1nmex†([a1,a2,…,ai]).
You are given a permutation‡ p of the set 0,1,2,…,n−1. Find the maximum cost across all cyclic shifts of p.
†mex([b1,b2,…,bm]) is the smallest non-negative integer x such that x does not occur among b1,b2,…,bm.
‡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).
对于一个数组 a,定义其代价为 ∑i=1nmex†([a1,a2,…,ai])。
给定集合 {0,1,2,…,n−1} 的一个排列‡ p。求 p 的所有循环移位中所能达到的最大代价。
†mex([b1,b2,…,bm]) 是最小的非负整数 x,使得 x 不在 b1,b2,…,bm 中出现。
‡ 集合 {0,1,2,...,n−1} 的一个排列是指由 0 到 n−1 中互不相同的 n 个整数以任意顺序组成的数组。例如,[1,2,0,4,3] 是一个排列,但 [0,1,1] 不是排列(数字 1 在数组中出现了两次),[0,2,3] 也不是排列(此时 n=3,但数组中出现了 3)。
输入格式
Each test consists of multiple test cases. The first line contains a single integer t (1≤t≤105) — 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≤n≤106) — the length of the permutation p.
The second line of each test case contain n distinct integers p1,p2,…,pn (0≤pi<n) — the elements of the permutation p.
It is guaranteed that sum of n over all test cases does not exceed 106.
每个测试包含多个测试用例。第一行包含一个整数 t(1≤t≤105),表示测试用例的数量。随后是各测试用例的描述。
每个测试用例的第一行包含一个整数 n(1≤n≤106),表示排列 p 的长度。
每个测试用例的第二行包含 n 个互不相同的整数 p1,p2,…,pn(0≤pi<n),即排列 p 的元素。
保证所有测试用例的 n 值之和不超过 106。
输出格式
For each test case, output a single integer — the maximum cost across all cyclic shifts of p.
对于每个测试用例,输出一个整数——即 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.
在第一个测试用例中,获得最大开销的循环移位是 [2,1,0,5,4,3],其开销为 0+0+3+3+3+6=15。
在第二个测试用例中,获得最大开销的循环移位是 [0,2,1],其开销为 1+1+3=5。
输入解题思路,AI测评打分。不知道怎么写?