[POJ] 3320 Jessica's Reading Problem (尺取法)
题目地址:http://poj.org/problem?id=3320
1 #include<cstdio> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 #include<math.h> 6 #include<stdbool.h> 7 #include<time.h> 8 #include<stdlib.h> 9 #include<set> 10 #include<map> 11 #include<stack> 12 #include<queue> 13 #include<vector> 14 using namespace std; 15 #define clr(x,y) memset(x,y,sizeof(x)) 16 #define sqr(x) ((x)*(x)) 17 #define rep(i,a,b) for(int i=(a);i<=(b);i++) 18 #define LL long long 19 #define INF 0x3f3f3f3f 20 #define A first 21 #define B second 22 const int N=1e6+131; 23 int n,p,a[N]; 24 25 void solve() 26 { 27 set<int>all; 28 map<int,int>count; 29 30 scanf("%d",&p); 31 for(int i=0;i<p;i++) { 32 scanf("%d",&a[i]); 33 all.insert(a[i]); 34 } 35 n=all.size(); 36 37 int lb=0,ub=0,num=0; 38 int res=p; 39 while(true) { 40 while(ub<p && num<n) { 41 if(count[a[ub++]]++ == 0) 42 num++; 43 } 44 if(num<n) break; 45 res=min(res,ub-lb); 46 if(--count[a[lb++]]==0) { 47 num--; 48 } 49 } 50 51 printf("%d\n",res); 52 53 } 54 int main() 55 { 56 solve(); 57 return 0; 58 }