CF1905D.Cyclic MEX

普及+/提高

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

For an array aa, define its cost as ∑i=1nmex⁡†([a1,a2,…,ai])\sum_{i=1}^{n} \operatorname{mex} ^\dagger ([a_1,a_2,\ldots,a_i]).

You are given a permutation‡^\ddagger pp of the set 0,1,2,…,n−1{0,1,2,\ldots,n-1}. Find the maximum cost across all cyclic shifts of pp.

†mex⁡([b1,b2,…,bm])^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m]) is the smallest non-negative integer xx such that xx does not occur among b1,b2,…,bmb_1,b_2,\ldots,b_m.

‡^\ddaggerA permutation of the set 0,1,2,...,n−1{0,1,2,...,n-1} is an array consisting of nn distinct integers from 00 to n−1n-1 in arbitrary order. For example, [1,2,0,4,3][1,2,0,4,3] is a permutation, but [0,1,1][0,1,1] is not a permutation (11 appears twice in the array), and [0,2,3][0,2,3] is also not a permutation (n=3n=3 but there is 33 in the array).

对于一个数组 aa,定义其代价为 ∑i=1nmex⁡†([a1,a2,…,ai])\sum_{i=1}^{n} \operatorname{mex} ^\dagger ([a_1,a_2,\ldots,a_i])。

给定集合 {0,1,2,…,n−1}\{0,1,2,\ldots,n-1\} 的一个排列‡^\ddagger pp。求 pp 的所有循环移位中所能达到的最大代价。

†mex⁡([b1,b2,…,bm])^\dagger\operatorname{mex}([b_1,b_2,\ldots,b_m]) 是最小的非负整数 xx,使得 xx 不在 b1,b2,…,bmb_1,b_2,\ldots,b_m 中出现。

‡^\ddagger 集合 {0,1,2,...,n−1}\{0,1,2,...,n-1\} 的一个排列是指由 00 到 n−1n-1 中互不相同的 nn 个整数以任意顺序组成的数组。例如,[1,2,0,4,3][1,2,0,4,3] 是一个排列,但 [0,1,1][0,1,1] 不是排列(数字 11 在数组中出现了两次),[0,2,3][0,2,3] 也不是排列(此时 n=3n=3,但数组中出现了 33)。

输入格式

Each test consists of multiple test cases. The first line contains a single integer tt (1≤t≤1051 \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 nn (1≤n≤1061 \le n \le 10^6) — the length of the permutation pp.

The second line of each test case contain nn distinct integers p1,p2,…,pnp_1, p_2, \ldots, p_n (0≤pi<n0 \le p_i \lt n) — the elements of the permutation pp.

It is guaranteed that sum of nn over all test cases does not exceed 10610^6.

每个测试包含多个测试用例。第一行包含一个整数 tt(1≤t≤1051 \le t \le 10^5),表示测试用例的数量。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1061 \le n \le 10^6),表示排列 pp 的长度。

每个测试用例的第二行包含 nn 个互不相同的整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(0≤pi<n0 \le p_i \lt n),即排列 pp 的元素。

保证所有测试用例的 nn 值之和不超过 10610^6。

输出格式

For each test case, output a single integer — the maximum cost across all cyclic shifts of pp.

对于每个测试用例,输出一个整数——即 pp 的所有循环移位中最大的代价。

输入输出样例

  • 输入#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][2,1,0,5,4,3] with cost 0+0+3+3+3+6=150+0+3+3+3+6=15.

In the second test case, the cyclic shift that yields the maximum cost is [0,2,1][0,2,1] with cost 1+1+3=51+1+3=5.

在第一个测试用例中,获得最大开销的循环移位是 [2,1,0,5,4,3][2,1,0,5,4,3],其开销为 0+0+3+3+3+6=150+0+3+3+3+6=15。

在第二个测试用例中,获得最大开销的循环移位是 [0,2,1][0,2,1],其开销为 1+1+3=51+1+3=5。

输入解题思路,AI测评打分。不知道怎么写?

首页