A12983 | Dominated Subarray
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 $a_1, a_2, \dots, a_n$ . 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 $a_i, a_{i + 1}, \dots, a_j$ for some $1 \le i \le j \le n$ .
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 $a_1, a_2, \dots, a_n$ . 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 $a_i, a_{i + 1}, \dots, a_j$ for some $1 \le i \le j \le n$ .
输入格式
The first line contains single integer $T$ ( $1 \le T \le 1000$ ) — the number of test cases. Each test case consists of two lines.
The first line contains single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the length of the array $a$ .
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 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 \cdot 10^5$ .
The first line contains single integer $n$ ( $1 \le n \le 2 \cdot 10^5$ ) — the length of the array $a$ .
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ( $1 \le a_i \le 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 \cdot 10^5$ .
输出格式
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.
输入输出样例
输入 #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 $a_4, a_5, a_6$ is the shortest dominated subarray.
In the fourth test case, all subarrays of length more than one are dominated.
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 $a_4, a_5, a_6$ is the shortest dominated subarray.
In the fourth test case, all subarrays of length more than one are dominated.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted