题目大意:
一个数列a,找到一个尽量长的连续子序列 ax 到 ay,使得该系列中没有相同的元素,求数列长度
思路:
滑动窗口
每次判断窗口内是否有重复即可
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream>
2 #include<cstdio>
3 #include<algorithm>
4 #include<cmath>
5 #include<cstdlib>
6 #include<cstring>
7 #include<queue>
8 #include<vector>
9 #define inf 2147483611
10 #define ll long long
11 #define MAXN 1010101
12 #define MOD
13 using namespace std;
14 inline ll read()
15 {
16 ll x=0,f=1;
17 char ch;ch=getchar();
18 while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
19 while(isdigit(ch)) {x=x*10+ch-'0';ch=getchar();}
20 return x*f;
21 }
22 int l,r,ans,n,a[MAXN],hash[MAXN];
23 int main()
24 {
25 n=read();
26 l=r=1;
27 for(int i=1;i<=n;i++) a[i]=read();
28 while(r<n)
29 {
30 while(r<n)
31 {
32 if(!hash[a[r+1]]) hash[a[r++]]++;
33 else break;
34 }
35 ans=max(ans,r-l+1);
36 while(hash[a[r+1]]) {hash[a[l++]]--;}
37 }
38 printf("%d",ans);
39 }