题目地址:http://vjudge.net/problem/UVA-11572
第一种用set做
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=(int)(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(int)(b);--i)
int A[(int)1e6+5];
int main(int argc, char const *argv[])
{int T,n; scanf("%d",&T);while(T--&&scanf("%d",&n)==1){REP(i,1,n) scanf("%d",&A[i]);set<int> st;int L=1,R=1,ans=1;while(R<=n){while(R<=n&&!st.count(A[R])) st.insert(A[R]),R++;ans=max(ans,R-L);st.erase(A[L]); L++;}printf("%d\n", ans);}return 0;
}
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=a;i<=(int)(b);++i)
#define REPD(i,a,b) for(int i=a;i>=(int)(b);--i)
int A[1000000+5],last[1000000+5];
int main(int argc, char const *argv[])
{int T,n; scanf("%d",&T);while(T--&&scanf("%d",&n)==1){map<int,int> cur;REP(i,1,n) {scanf("%d",&A[i]);if(cur.count(A[i])==0) last[i]=-1;else last[i]=cur[A[i]];cur[A[i]]=i;}int L=1,R=1,ans=1;while(R<=n){while(R<=n&&last[R]<L) R++;ans=max(ans,R-L);L++;}printf("%d\n", ans);}return 0;
}