Ideas:
Two variables I j for each I meet the search is maintained with J for the length of the interval is the minimum of I meet the requirements of the interval so you don't have to run o^n time complexity
#include <stdio.h> #include <string.h> #include <iostream>using namespaceStd; int Num[100010],Mark[100010]; int Main () { int N,M,Q,I,J; while (~scanf("%d%d",&N,&M) ) { if( N==0&&M==0 ) break; for (I=1;I<=N;I++)scanf("%d",&Num[I]); while (M--) {scanf("%d",&Q);Memset(Mark,0 ,sizeof( Mark)); int A; int Cont=0; for (I=1;I<=Q;I++) {scanf("%d",&A); if (Mark[A]==0)Cont++;Mark[A]=1; } int Leap[100010];Memset(Leap,0 ,sizeof( Leap)); int Min; int Flash=0; for (I=1;;I + +) { if( Mark[Num[I]]==0 ) continue; for (J=I;J<=N;J + +) { if( Mark[Num[J ]]) { if( Leap[Num[J]]==0)Flash++;Leap[Num[J]]++; } if( Flash==Cont ) break; } break; }Min=J-I+1;Leap[Num[I]]--; if (Leap[Num[I]]==0)Flash--; for (I++;I<=N;I + +) { if(! Mark[Num[I ]]) continue; if (Flash==Cont ) { if( J-I+1<Min)Min=J-I+1;Leap[Num[I]]--; if (Leap[Num[I]]==0)Flash--; Continue ; } for ( J++;J<=N;J + +) { if(! Mark[Num[J ]]) continue; if (Leap[Num[J]]==0)Flash++;Leap[Num[J]]++; if (Flash==Cont ) break; } if( Flash==Cont&&J-I+1<Min)Min=J-I+1;Leap[Num[I]]--; if (Leap[Num[I]]==0)Flash--; }Printf("%d\n",Min); }} return 0;}
Hdu 2158 Good idea questions