Jessica's Reading Problem(POJ 3320)

Posted ymir-taomee

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jessica's Reading Problem(POJ 3320)相关的知识,希望对你有一定的参考价值。

  • 原题如下:
    Jessica‘s Reading Problem
    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
  • 题解:假设从某一页s开始阅读,为了覆盖所有的知识点需要阅读到t,如果从s+1开始阅读的话,必须阅读到t‘≥t页为止,因此,这题可以使用尺取法。这题的关键是,在某个区间[s,t]已经覆盖了所有的知识点的情况下,下一个区间[s+1,t‘](t‘≥t)要如何求出?有这样一个等价关系:所有的知识点都被覆盖?每个知识点出现的次数都不小于1,由这个等价关系,我们可以用二叉树等数据结构来存储[s,t]区间上每个知识点的出现次数,这样把最开头的页s去除后便可以判断[s+1,t]是否满足条件,从区间的开头把s取出后,页s上书写的知识点的出现次数就要减一,如果此时这个知识点的出现次数为0了,在同一个知识点再次出现前,不停将区间末尾t向后推进即可,每次在区间末尾追加页t时将页t上的知识点的出现次数加1,这样就完成了下一个区间上各个知识点出现次数的更新,通过重复这一操作可以以O(PlogP)的复杂度内求出最小的区间了
  • 代码:
     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)的主要内容,如果未能解决你的问题,请参考以下文章

POJ-3320 Jessica's Reading Problem

POJ - 3320 Jessica's Reading Problem

(尺取法)POJ 3320 Jessica's Reading Problem

POJ 3320Jessica's Reading Problem (尺取法)

Jessica's Reading Problem(POJ 3320)

poj3320 Jessica's Reading Problem