CF1257C.Dominated Subarray
普及-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Let's call an array t dominated by value v in the next situation.
At first, array t should have at least 2 elements. Now, let's calculate number of occurrences of each number num in t and define it as occ(num). Then t is dominated (by v) if (and only if) occ(v)>occ(v′) for any other number v′. For example, arrays [1,2,3,4,5,2], [11,11] and [3,2,3,2,3] are dominated (by 2, 11 and 3 respectevitely) but arrays [3], [1,2] and [3,3,2,2,1] are not.
Small remark: since any array can be dominated only by one number, we can not specify this number and just say that array is either dominated or not.
You are given array a1,a2,…,an. Calculate its shortest dominated subarray or say that there are no such subarrays.
The subarray of a is a contiguous part of the array a, i. e. the array ai,ai+1,…,aj for some 1≤i≤j≤n.
我们称数组 t 被值 v 主导(dominated),当且仅当满足以下条件:
首先,数组 t 至少包含 2 个元素。接着,我们统计 t 中每个数 num 的出现次数,并记为 occ(num)。那么 t 被 v 主导(即以 v 为主导元素),当且仅当对任意其他数值 v′,均有 occ(v)>occ(v′)。例如,数组 [1,2,3,4,5,2]、[11,11] 和 [3,2,3,2,3] 分别被 2、11 和 3 主导;但数组 [3]、[1,2] 和 [3,3,2,2,1] 均不被任何值主导。
一个小提示:由于任意数组至多只能被一个数值主导,因此我们无需显式指明该主导值,而可直接说“该数组是否被主导”。
现给你一个数组 a1,a2,…,an。请找出它的最短主导子数组;若不存在这样的子数组,则说明不存在。
数组 a 的子数组是指 a 的一个连续片段,即形如 ai,ai+1,…,aj 的数组,其中 1≤i≤j≤n。
输入格式
The first line contains single integer T (1≤T≤1000) — the number of test cases. Each test case consists of two lines.
The first line contains single integer n (1≤n≤2⋅105) — the length of the array a.
The second line contains n integers a1,a2,…,an (1≤ai≤n) — the corresponding values of the array a.
It's guaranteed that the total length of all arrays in one test doesn't exceed 2⋅105.
第一行包含一个整数 T(1≤T≤1000)—— 测试用例的数量。每个测试用例由两行组成。
第一行包含一个整数 n(1≤n≤2⋅105)—— 数组 a 的长度。
第二行包含 n 个整数 a1,a2,…,an(1≤ai≤n)—— 数组 a 的对应元素值。
保证单个测试中所有数组的总长度不超过 2⋅105。
输出格式
Print T integers — one per test case. For each test case print the only integer — the length of the shortest dominated subarray, or −1 if there are no such subarrays.
输出 T 个整数——每个测试用例对应一个整数。对于每个测试用例,输出唯一的整数——最短支配子数组的长度;若不存在这样的子数组,则输出 −1。
输入输出样例
输入#1
4 1 1 6 1 2 3 4 5 1 9 4 1 2 4 5 4 3 2 1 4 3 3 3 3
输出#1
-1 6 3 2
说明/提示
In the first test case, there are no subarrays of length at least 2, so the answer is −1.
In the second test case, the whole array is dominated (by 1) and it's the only dominated subarray.
In the third test case, the subarray a4,a5,a6 is the shortest dominated subarray.
In the fourth test case, all subarrays of length more than one are dominated.
在第一个测试用例中,不存在长度至少为 2 的子数组,因此答案为 −1。
在第二个测试用例中,整个数组是被支配的(由 1 支配),且它是唯一被支配的子数组。
在第三个测试用例中,子数组 a4,a5,a6 是最短的被支配子数组。
在第四个测试用例中,所有长度大于一的子数组均被支配。
输入解题思路,AI测评打分。不知道怎么写?