Think is the data water the problem, see some directly with the array to save a value last occurrence of the position, OK 0 to 1e9 of the data range it?
Use set, save the current interval of the number of occurrences, the next number, did not appear, join, until the repetition, and then delete the left end, continue ...
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e6+2;intA[MAXN];intMain () {intT scanf"%d",&T); while(t--){ intN scanf"%d",&N); for(inti =0; I < n; i++) scanf ("%d", A +i); Set<int>s; intL =0, R =0, ans =0; while(R <N) { while(R < n &&!s.count (A[r]) S.insert (a[r++]); Ans= Max (ans,r-L); S.erase (A[l++]); } printf ("%d\n", ans); } return 0;}
The array version, and finally to the end of the time to be counted once, so a[n] = a[n-1].
#include <bits/stdc++.h>using namespacestd;Const intMAXN = 1e6+2;intA[MAXN];intPOS[MAXN];intMain () {intT scanf"%d",&T); while(t--){ intN scanf"%d",&N); for(inti =0; I < n; i++) scanf ("%d", A +i); memset (POS,-1,sizeof(POS)); intL =0, ans =0; A[n]= a[n-1]; for(inti =0; I <= N; i++){ if(Pos[a[i]] >=L) {ans= Max (ans,i-L); L= pos[a[i]]+1; } Pos[a[i]]=i; } printf ("%d\n", ans); } return 0;}
UVA 11572 Unique Snowflakes (sliding window)