Jessica ' s Reading problem
Description
Jessica ' s a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she had spent little time on it. If She wants to pass it, she had to master all ideas included in a very thick the text book. The author of that text book, like other authors, was extremely fussy about the ideas, thus some ideas was covered more tha N once. Jessica think if she managed to read all 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 hers each page of the Jessica ' s text-book with what's idea each page was about an D thus made a big progress for his courtship. Here you come on to save your Skin:given the index, help Jessica decide which contiguous part she should read. For convenience, the idea had been coded with an ID, and which is a non-negative integer.
Input
The first line of input is a integer p (1≤ P ≤1000000), which is the number of pages of Jessica ' s Tex T-book. The second line contains P non-negative integers describing what's the idea of each page was about. The first integer is what the first page was about, the second integer was what the second page was about, and so on. Assume all integers this 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
51 8 8) 8 1
Sample Output
2
Source
POJ monthly--2007.08.05, Jerry thought: a simple ruler method
#include <iostream>#include<cstdio>#include<cmath>#include<string>#include<queue>#include<algorithm>#include<stack>#include<cstring>#include<vector>#include<list>#include<Set>#include<map>using namespacestd;#definell __int64#defineMoD 1000000007#defineINF 999999999//#pragma COMMENT (linker, "/stack:102400000,102400000")intScan () {intres =0, ch; while( ! (ch = getchar ()) >='0'&& CH <='9' ) ) { if(ch = = EOF)return 1<< - ; } Res= CH-'0' ; while(ch = getchar ()) >='0'&& CH <='9') Res= Res *Ten+ (CH-'0' ) ; returnRes;}inta[1000010];Set<int>S;map<int,int>m;intMain () {intx,y,z,i,t; while(~SCANF ("%d",&x)) {s.clear (); M.clear (); for(i=0; i<x;i++) {scanf ("%d",&A[i]); S.insert (A[i]); } y=s.size (); intst=0, ji=0, en=0, minn=10000010; while(1) { while(en<x&&ji<y) {if(m[a[en]]==0) Ji++; M[a[en]]++; En++; } //cout<<st<< "<<en<<" <<ji<< "~ ~ ~" <<endl; if(ji<y) Break; if(en-st<Minn) Minn=en-St; M[A[ST]]--; if(m[a[st]]==0) Ji--; St++; } printf ("%d\n", Minn); } return 0;}
View Code
POJ 3320 Jessica ' s Reading problem ruler extraction