1. 程式人生 > >POJ-3320 Jessica's Reading Problem

POJ-3320 Jessica's Reading Problem

which page 是否 cnblogs been pear num lang wan

Jessica‘s a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The author of that text book, like other authors, is extremely fussy about the ideas, thus some ideas are covered more than once. Jessica think if she managed to read each idea at least once, she can pass the exam. She decides to read only one contiguous part of the book which contains all ideas covered by the entire book. And of course, the sub-book should be as thin as possible.

A very hard-working boy had manually indexed for her each page of Jessica‘s text-book with what idea each page is about and thus made a big progress for his courtship. Here you come in to save your skin: given the index, help Jessica decide which contiguous part she should read. For convenience, each idea has been coded with an ID, which is a non-negative integer.

Input

The first line of input is an integer P (1 ≤ P ≤ 1000000), which is the number of pages of Jessica‘s text-book. The second line contains P non-negative integers describing what idea each page is about. The first integer is what the first page is about, the second integer is what the second page is about, and so on. You may assume all integers that appear can fit well in the signed 32-bit integer type.

Output

Output one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.

Sample Input

5
1 8 8 8 1

Sample Output

2

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<algorithm>
 5 #include<set>
 6 #include<map>
 7 using namespace std;
 8 const int mx=1e6+10;
 9 map<int ,int > mp;//計算每個知識點重復的次數
10 set<int> seet; //用來計算知識點的種類
11 int a[mx];
12 int main(){
13      int paper;
14      while(~scanf("%d",&paper)){
15         seet.clear();
16         mp.clear();
17         int tmp;
18         for(int i=1;i<=paper;i++){
19              scanf("%d",&tmp);
20              a[i]=tmp;
21              seet.insert(tmp);
22         }
23         int num=seet.size();
24         int ans=paper;
25         if(num==1){
26             cout<<num<<endl;
27         }else{
28              int st,en,sum;
29              st=en=1;
30              sum=0;
31              while(st<=paper&&en<=paper){
32                  if(mp[a[en]]==0) sum++;
33                  mp[a[en]]++;
34                  while(mp[a[st]]>1){    //當其大於1的時候說明st往後移一位並不影響知識種類的變化
35                      mp[a[st]]--;
36                      st++;
37                  }
38                  if(sum==num){          //判斷是否包含了所有的
39                      ans=min(ans,en-st+1);//取最少的頁數
40                      if(mp[a[st]]==1)  sum--;//因為待會要將st往後移,判斷是否需要sum-1
41                     mp[a[st]]--;
42                      st++;    //st後移
43                  } 
44                  en++;  //en後移
45              }
46              cout<<ans<<endl;
47          }
48      }
49      return 0;
50  }


POJ-3320 Jessica's Reading Problem