int n; scanf("%d", &n); set<int> S; for (int i = 0; i < n; i++) { scanf("%d", a + i); S.insert(a[i]); } int sn = S.size(); S.clear(); map<int, int> M; int s = 0, t = 0, cnt = 0, ans = n; for (;;) { while (t < n && cnt < sn) { if (M[a[t]] == 0) cnt++; M[a[t]]++; t++; } if (cnt < sn) break; ans = min(ans, t - s); if (M[a[s]] == 1) cnt--; M[a[s]]--; s++; } printf("%d\n", ans); }
#include<cstdio> #include<map> #include<cctype> #include<set> #include<algorithm> usingnamespacestd; constint maxn = 1000100; int a[maxn]; constint hash_n = 535442; int M0[hash_n], M[hash_n]; inlineintget() { int k=0; char f=1; char c=getchar(); for(;!isdigit(c);c=getchar() ) if(c=='-') f=-1; for(;isdigit(c);c=getchar() ) k=k*10+c-'0'; return k*f; } intmain(){ int n; n = get();
for (int i = 0; i < n; i++) { a[i] = get(); a[i] %= hash_n; M0[a[i]]++; } int sn = 0; for (int i = 0; i < hash_n; i++) if (M0[i]) sn++; int s = 0, t = 0, cnt = 0, ans = n; for (;;) { while (t < n && cnt < sn) { if (M[a[t]] == 0) cnt++; M[a[t]]++; t++; } if (cnt < sn) break; ans = min(ans, t - s); if (M[a[s]] == 1) cnt--; M[a[s]]--; s++; } printf("%d\n", ans); }