Subject address: Ultraviolet A 11572
This method has been used before. It defines two pointers and continuously slides from left to right to determine whether the pointer meets the requirements.
In order to quickly determine whether this number exists, set in STL can be used.
The Code is as follows:
#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <queue>#include <map>#include <set>#include <algorithm>using namespace std;int a[1100000];int main(){ int t, n, l, r, i, max1; scanf("%d",&t); while(t--) { scanf("%d",&n); max1=-1; for(i=0;i<n;i++) { scanf("%d",&a[i]); } l=r=0; set<int>q; while(r!=n) { while(q.count(a[r])) { q.erase(a[l++]); } q.insert(a[r++]); if(max1<r-l) max1=r-l; } printf("%d\n",max1); } return 0;}
(White Book Training Program) ultraviolet (11572 unique snowflakes)