C. DOMINATED SUBARRAY[求最短的两个相同的数的子串长度]

http://www.yyycode.cn/index.php/2020/05/19/c-dominated-subarray%e6%b1%82%e6%9c%80%e7%9f%ad%e7%9a%84%e4%b8%a4%e4%b8%aa%e7%9b%b8%e5%90%8c%e7%9a%84%e6%95%b0%e7%9a%84%e5%ad%90%e4%b8%b2%e9%95%bf%e5%ba%a6/


 

 

https://codeforces.com/problemset/problem/1257/C

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.Input

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.Output

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.ExampleinputCopy

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

outputCopy

-1
6
3
2

Note

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.


题意:找一串中能有最短的两个相同的数的子串长度

思路:双指针模拟,ij快慢指针

感觉这种同方向指针的双指针模拟自己还没有形成模板….容易卡边界debug

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=2e5+10;
typedef long long LL;
LL a[maxn]; 
LL b[maxn];
int main(void)
{
	LL t;cin>>t;
	while(t--)
	{
		LL n;cin>>n;
		for(LL i=1;i<=n;i++)
			cin>>a[i];
		///多组桶清空	
		for(LL i=1;i<=n+10;i++)
			b[i]=0;
		LL res=0x3f3f3f3f;	
		if(n==1) cout<<-1<<endl;
		else
		{
			b[a[1]]++;//第一个先加进桶里面 
			for(LL i=1,j=1;i<=n&&j<=n;)
			{		
				if(b[a[i]]<2)
				{
					i++;
					b[a[i]]++;		
				}
				if(b[a[i]]==2)
				{
					res=min(res,i-j+1);
					b[a[j]]--;
					j++;
				}	
			}	
		if(res==0x3f3f3f3f||res>n) cout<<-1<<endl;///要特判res>n的,不懂... 
		else	cout<<res<<endl;
		}
	}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值