标签:des com pac namespace 复杂度 putc turn body 存储
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16925 | Accepted: 5861 |
Description
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 <cstdio> 2 #include <cctype> 3 #include <algorithm> 4 #include <cmath> 5 #include <map> 6 #include <set> 7 #define num s-‘0‘ 8 9 using namespace std; 10 11 const int MAX_N=1000000; 12 const int INF=0x3f3f3f3f; 13 int P; 14 int a[MAX_N]; 15 16 int min(int x, int y) 17 { 18 if (x<y) return x; 19 return y; 20 } 21 22 void read(int &x){ 23 char s; 24 x=0; 25 bool flag=0; 26 while(!isdigit(s=getchar())) 27 (s==‘-‘)&&(flag=true); 28 for(x=num;isdigit(s=getchar());x=x*10+num); 29 (flag)&&(x=-x); 30 } 31 32 void write(int x) 33 { 34 if(x<0) 35 { 36 putchar(‘-‘); 37 x=-x; 38 } 39 if(x>9) 40 write(x/10); 41 putchar(x%10+‘0‘); 42 } 43 44 int main() 45 { 46 read(P); 47 set<int> all; 48 for (int i=0; i<P; i++) 49 { 50 read(a[i]); 51 all.insert(a[i]); 52 } 53 int n=all.size(); 54 int s=0, t=0, numb=0; 55 map<int, int> count; 56 int res = P; 57 for (;;) 58 { 59 while (t<P && numb<n) 60 if (count[a[t++]]++==0) numb++; 61 if (numb<n) break; 62 res=min(res,t-s); 63 if (--count[a[s++]]==0) numb--; 64 } 65 write(res); 66 }
Jessica's Reading Problem(POJ 3320)
标签:des com pac namespace 复杂度 putc turn body 存储
原文地址:https://www.cnblogs.com/Ymir-TaoMee/p/9509475.html