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

POJ Monthly--2007.08.05, Jerry

题目大意:

离散化+尺取乱搞

 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.

poj3320 Jessica's Reading Problem

时间: 2024-10-17 15:43:04

poj3320 Jessica's Reading Problem的相关文章

poj3061 Subsequence&amp;&amp;poj3320 Jessica&#39;s Reading Problem(尺取法)

这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针(t)要么不动要么也往前走.满足这种特点的就可以考虑尺取法. poj3061 比较简单,也可以用二分做,时间复杂度O(n*logn).用尺取法可以O(n)解决. #include<iostream> #include<cstdio> #include<cstdlib> #i

POJ3320 Jessica&#39;s Reading Problem(尺取+map+set)

POJ3320 Jessica's Reading Problem set用来统计所有不重复的知识点的数,map用来维护区间[s,t]上每个知识点出现的次数,此题很好的体现了map的灵活应用 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <vector> #include &

poj3320(Jessica&#39;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 au

poj 3320 Jessica&#39;s Reading Problem(尺取法+map/hash)

题目:http://poj.org/problem?id=3320 题意:给定N个元素的数组,找出最短的一段区间使得区间里面的元素种类等于整个数组的元素种类. 分析:暴力枚举区间的起点x,然后找到最小的y,使得区间[x,y]满足条件,x向有移位后变成x',现在的y'肯定不至于在y的左边.存状态的话map和hash都可以. map代码: #include <iostream> #include <set> #include <map> #include <cstdi

poj 3320 Jessica&#39;s Reading Problem

题意:有n页书,每页有个编号为ci的知识点,求最小看连续的页数,其中包括所有的知识点 分析:n<=1e6,只能搞O(n)的解法,无非就是枚举起点和终点,尺取法正好适合这个想法,枚举一个起点,然后往后扫描到区间内包括所有的知识点,然后每次起点都往右移动一次,直到扫描到右边界也没有答案了,就跳出 程序跑了469ms,应该是map太慢,可以hash搞一波,也过了,但是可以离散化知识点的编号,最多有1e6个,空间换时间 #include<iostream> #include<cstdio&

poj 3320 Jessica&#39;s Reading Problem (尺取法)

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8787   Accepted: 2824 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 littl

尺取法 POJ 3320 Jessica&#39;s Reading Problem

题目传送门 1 /* 2 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 3 */ 4 #include <cstdio> 5 #include <cmath> 6 #include <cstring> 7 #include <algorithm> 8 #include <set> 9 #include <map> 10 using namespace std; 11 12 const int M

Jessica&#39;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 au

POJ 3320 Jessica&#39;s Reading Problem 尺取法/map

Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accepted: 2369 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 littl