Thursday, February 20, 2020

POJ.3320 Jessica's Reading Problem

1.Problem
http://poj.org/problem?id=3320

2.Idea
Two Pointers method

3.Source
 int P;  
 int a[1000006];  
 void solve()  
 {  
      set<int> all;  
      for (int i = 0; i < P; i++) all.insert(a[i]);  
      int n = all.size();  
      map<int, int> count;  
      int s = 0, t = 0, num = 0, res = P;  
      for (;;) {  
           while (t < P && num < n) {  
                if (count[a[t++]]++ == 0) num++;  
           }  
           if (num < n) break;  
           res = min(res, t - s);  
           if (--count[a[s++]] == 0) {  
                num--;  
           }  
      }  
      printf("%d\n", res);  
 }  
 int main()  
 {  
      scanf("%d", &P);  
      for (int i = 0; i < P; i++) scanf("%d", &a[i]);  
      solve();  
      return 0;  
 }  

No comments:

Post a Comment