題解:設置左右端點 l,r=0,當a[r]不在[l,r]區間是,r++;否則 l++,知道a[r]不在其區間中。
[l,r]區間中的元素可以用set進行判重和刪除操作
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e7;
int a[maxn];
int main()
{
int t,n;
cin>>t;
while(t--){
cin>>n;
int l=0,r=0;
set<long long>s;
for(int i=0;i<n;i++) scanf("%d",&a[i]);
int Max=0;
while(r<n){
while(s.count(a[r])) s.erase(a[l++]);
s.insert(a[r++]);
Max=max(Max,r-l);
}
cout<<Max<<endl;
}
return 0;
}