poj3320 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 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
Source
离散化+尺取乱搞
1 program rrr(input,output); 2 var 3 a,b,c:array[0..1000010]of longint; 4 n,m,i,j,ans,l,r,mid:longint; 5 function min(a,b:longint):longint; 6 begin 7 if a<b then exit(a) else exit(b); 8 end; 9 procedure sort(q,h:longint); 10 var 11 i,j,x,t:longint; 12 begin 13 i:=q;j:=h;x:=b[(i+j)>>1]; 14 repeat 15 while b[i]<x do inc(i); 16 while x<b[j] do dec(j); 17 if i<=j then 18 begin 19 t:=b[i];b[i]:=b[j];b[j]:=t; 20 inc(i);dec(j); 21 end; 22 until i>j; 23 if j>q then sort(q,j); 24 if i<h then sort(i,h); 25 end; 26 function find(x:longint):longint; 27 begin 28 l:=1;r:=m; 29 while l<r-1 do 30 begin 31 mid:=(l+r)>>1; 32 if b[mid]>x then r:=mid-1 else l:=mid; 33 end; 34 if b[l]=x then exit(l) else exit(r); 35 end; 36 begin 37 assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output); 38 readln(n); 39 for i:=1 to n do begin read(a[i]);b[i]:=a[i]; end; 40 sort(1,n); 41 m:=1;for i:=2 to n do if b[i]<>b[i-1] then begin inc(m);b[m]:=b[i]; end; 42 for i:=1 to n do a[i]:=find(a[i]); 43 fillchar(c,sizeof(c),0);i:=0; 44 for j:=1 to n do 45 begin 46 inc(c[a[j]]); 47 if c[a[j]]=1 then inc(i); 48 if i=m then break; 49 end; 50 ans:=j; 51 for i:=2 to n do 52 begin 53 dec(c[a[i-1]]); 54 while (j<n) and (c[a[i-1]]=0) do begin inc(j);inc(c[a[j]]); end; 55 if c[a[i-1]]=0 then break; 56 ans:=min(ans,j-i+1); 57 end; 58 write(ans); 59 close(input);close(output); 60 end.