Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray
Let's call an array tt dominated by value vv in the next situation.
At first, array tt should have at least 22 elements. Now, let's calculate number of occurrences of each number numnum in tt and define it as occ(num)occ(num). Then tt is dominated (by vv) if (and only if) occ(v)>occ(v′)occ(v)>occ(v′) for any other number v′v′. For example, arrays [1,2,3,4,5,2][1,2,3,4,5,2], [11,11][11,11] and [3,2,3,2,3][3,2,3,2,3] are dominated (by 22, 1111 and 33 respectevitely) but arrays [3][3], [1,2][1,2] and [3,3,2,2,1][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,…,ana1,a2,…,an. Calculate its shortest dominated subarray or say that there are no such subarrays.
The subarray of aa is a contiguous part of the array aa, i. e. the array ai,ai+1,…,ajai,ai+1,…,aj for some 1≤i≤j≤n1≤i≤j≤n.
The first line contains single integer TT (1≤T≤10001≤T≤1000) — the number of test cases. Each test case consists of two lines.
The first line contains single integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the length of the array aa.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — the corresponding values of the array aa.
It's guaranteed that the total length of all arrays in one test doesn't exceed 2⋅1052⋅105.
Print TT integers — one per test case. For each test case print the only integer — the length of the shortest dominated subarray, or −1−1 if there are no such subarrays.
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
6
3
2
In the first test case, there are no subarrays of length at least 22, so the answer is −1−1.
In the second test case, the whole array is dominated (by 11) and it's the only dominated subarray.
In the third test case, the subarray a4,a5,a6a4,a5,a6 is the shortest dominated subarray.
In the fourth test case, all subarrays of length more than one are dominated.
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+;
int n,a[maxn],pre[maxn];
int main() {
int t;
scanf("%d",&t);
while(t--) {
scanf("%d",&n);
int ans=0x3f3f3f3f;
for(int i=; i<=n; i++) pre[i]=-;
for(int i=; i<=n; i++) {
scanf("%d",&a[i]);//先输入
if(pre[a[i]]!=-)//判断之前有没有出现过
ans=min(ans,i-pre[a[i]]+);//如果出现过,取到上一个点的距离并取最小
pre[a[i]]=i;//标记此时点的距离
}
printf("%d\n",ans==0x3f3f3f3f?-:ans);
}
}
//计算每两个相同元素的距离对ans 取min
Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray的更多相关文章
- Educational Codeforces Round 76 (Rated for Div. 2) C. Dominated Subarray 水题
C. Dominated Subarray Let's call an array
- Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest
Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest(dp+线段树) 题目链接 题意: 给定3个人互不相同的多个数字,可以 ...
- Educational Codeforces Round 76 (Rated for Div. 2)E(dp||贪心||题解写法)
题:https://codeforces.com/contest/1257/problem/E 题意:给定3个数组,可行操作:每个数都可以跳到另外俩个数组中去,实行多步操作后使三个数组拼接起来形成升序 ...
- Educational Codeforces Round 76 (Rated for Div. 2)
传送门 A. Two Rival Students 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/13 22:37:26 */ #incl ...
- Educational Codeforces Round 76 (Rated for Div. 2) E. The Contest dp
E. The Contest A team of three programmers is going to play a contest. The contest consists of
- Educational Codeforces Round 76 (Rated for Div. 2) D. Yet Another Monster Killing Problem 贪心
D. Yet Another Monster Killing Problem You play a computer game. In this game, you lead a party of
- Educational Codeforces Round 76 (Rated for Div. 2) B. Magic Stick 水题
B. Magic Stick Recently Petya walked in the forest and found a magic stick. Since Petya really likes ...
- Educational Codeforces Round 76 (Rated for Div. 2) A. Two Rival Students 水题
A. Two Rival Students There are
- Educational Codeforces Round 76 (Rated for Div. 2) D题
题意: 给你n个关卡,每个关卡有一个怪物,怪物的攻击力为a[i],你有n个英雄,每个英雄有一个攻击力,和疲劳值,只要英雄的攻击力比怪物的高就算打过了,同时疲劳减一,一天只能出战一个英雄,一个英雄可以打 ...
随机推荐
- laravel 解决 sql mode only_full_group_by
this is incompatible with sql_mode=only_full_group_by 先贴报错是这样的哦,sql 中使用到了 group by 然后这是mysql-5.7以上版本 ...
- hdu 2187 悼念512汶川大地震遇难同胞——老人是真饿了(贪心)
新人题:n2的排序就可以过 #include <stdio.h> #include <stdlib.h> int main() { int c,n,i,j,o; ],b[],m ...
- ubuntu18.04 安装与卸载 php7.2
安装: 如果之前有其他版本PHP,在这边禁用掉 1 sudo a2dismod php5 再来安装做准备 1234 sudo apt-get install software-properties-c ...
- SpringBoot的应运而生
随着动态语言的流行(Ruby,Groovy,Scala,Node.js),java的开发显得格外的笨重,繁多的配置,低下的效率,复杂的部署流程以及第三方技术集成难度大.springboot应运而生,使 ...
- mysql 数据备份。pymysql模块
阅读目录 一 IDE工具介绍 二 MySQL数据备份 三 pymysql模块 一 IDE工具介绍 生产环境还是推荐使用mysql命令行,但为了方便我们测试,可以使用IDE工具 下载链接:https:/ ...
- 2018 HBCPC 菜鸡选手记
我果然太菜了 A B--T2 D--T4 C--T3 F 我果然太菜了 作为学校最菜的队员,今天下午被虐惨了. 一下午才做A-D四道题 官方题解链接 晚上吃完饭再去看. A 队友A的不清楚.没看题. ...
- 爬取漫画DB上的JoJo的奇妙冒险 第七部 飙马野郎
SBR是JOJO系列我最喜欢的一部,所以今天把漫画爬取到本地,日后慢慢看. import re import time import requests from requests import cod ...
- POJ3723(最小生成树,负权)
题目描述 温迪有一个国家,他想建立一支军队来保护他的国家.他收留了N个女孩和M个男孩,想把她们收留成他的士兵.征兵无特权,必须交纳一万元.女孩和男孩之间有一些关系,温迪可以利用这些关系来降低他的成本. ...
- java实现判断两个二叉树是否相同
1.定义树节点类:节点值.左节点.右节点.构造器 2.先判断树是否为空的情况 3.树不为空时,判断节点所指的值是否相等,若相等,则递归判断节点的左右节点是否相同,相同则返回true /** * Def ...
- ORA-12638: 身份证明检索失败
PLSQL 连接Oracle时提示 “ORA-12638: 身份证明检索失败” 错误, 但是开发IDE可以正常连接,DataGrid 可以正常连接,所以确定是本地设置问题. 网上搜了一下,说是因为Or ...