Test instructions: There are n pages, each page has a knowledge point numbered CI, to minimize the number of consecutive pages, including all the knowledge points
Analysis: N<=1e6, can only make O (n) solution, is simply enumerated starting point and end point, the ruler method is suitable for this idea, enumerate a starting point, and then scan to the interval to include all the knowledge points, and then each start to move to the right once, until the scan to the right border and no answer, jump out
The program ran 469ms, should be map too slow, can hash a wave, also too, but can be discretized knowledge point number, up to 1e6, space change time
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<map>using namespacestd;Const intmaxn=1e6+5;intC[maxn];map<int,int>v;intMain () {intN; while(~SCANF ("%d",&N)) {v.clear (); intCount=0; for(intI=0; i<n;i++) {scanf ("%d", c+i); if(v[c[i]]==0) {V[c[i]]=1; Count++; } } intans=n+1;intCnt=0, l=0, r=0; V.clear (); for(;;) { while(r<n&&cnt<count) { if(v[c[r]]==0) cnt++; V[c[r++]]++; } if(Cnt<count) Break; Ans=min (ans,r-l); if(v[c[l]]==1) cnt--; V[c[l++]]--; } printf ("%d\n", ans); } return 0;}
View Code
Hash, ran the 250ms, the speed of the increase of half
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<map>#include<cstring>using namespacestd;Const intmaxn=1e6+5;intC[maxn],v[maxn];inlineintHashintx) { returnX%699997;}intMain () {intN; while(~SCANF ("%d",&N)) {memset (V,0,sizeof(v)); intCount=0; for(intI=0; i<n;i++) {scanf ("%d", c+i); if(V[hash (C[i])]==0) {V[hash (C[i])]=1; Count++; } } intans=n+1;intCnt=0, l=0, r=0; memset (V,0,sizeof(v)); for(;;) { while(r<n&&cnt<count) { if(V[hash (C[r])]==0) cnt++; V[hash (C[r++])]++; } if(Cnt<count) Break; Ans=min (ans,r-M); if(V[hash (c[l])]==1) cnt--; V[hash (C[l++])]--; } printf ("%d\n", ans); } return 0;}
View Code
POJ 3320 Jessica ' s Reading problem