原题链接:https://vjudge.net/problem/UVA-11572
分类:滑动窗口
备注:双指针
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5;
map<int,int>vis;
int t,n,a[maxn];
int main(void){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
scanf("%d",&t);
while(t--){
int ans=0;
vis.clear();
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
int R=1,len=0;
for(int L=1;L<=n;vis[a[L]]=0,L++,len--){
while(R<=n&&!vis[a[R]]){
vis[a[R]]=1; R++; len++;
}
if(len>ans)ans=len;
while(L<R&&a[L]!=a[R]){
vis[a[L]]=0; L++; len--;
}
}
printf("%d\n",ans);
}
return 0;
}