【尺取法】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

大概题意:一共P页的书,没页都有一个知识点, 问最少看连续多少页可以覆盖所有的知识点;

题解:覆盖知识点,则知识点的出现次数一定是大于等于1的, 所以用map做记录,set成二叉树来存储区间上点的出现次数,

           从区间的最开头把S取出来,在同一个知识点出现前,不停的将区间末尾的t向后推进;每次将后推t页,则将第t页的知识点   出现次数加1 , 这样就完成了下一个区间上各个知识点出现次数的更新;

AC代码:(要clear)

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
using namespace std;
int a[1000000+10] , p  ;
inline bool scan_d(int &num)
{
        char in;bool IsN=false;
        in=getchar();
        if(in==EOF) return false;
        while(in!='-'&&(in<'0'||in>'9')) in=getchar();
        if(in=='-'){ IsN=true;num=0;}
        else num=in-'0';
        while(in=getchar(),in>='0'&&in<='9'){
                num*=10,num+=in-'0';
        }
        if(IsN) num=-num;
        return true;
}
int main()
{
    while(~scanf("%d",&p))
    {
        memset(a,0,sizeof(a));
        set<int>all;
        map<int ,int > count ;
        for(int i = 0 ; i<p ;i++)
        {
            scan_d(a[i]);
        }
        all.clear();
        for(int i = 0 ; i<p ; i++)
        {
            all.insert(a[i]);
        }
        int n = all.size();
        int s , t , num ;
        s = t = num = 0 ;
        int res = p ;
        for(;;)
        {
            while( t < p && num < n)
            {
                if(count[a[t++]]++==0)
                {
                    num++;
                }
            }
            if(num <  n) break;
            res = min(res,t-s);
            if(--count[a[s++]]==0)
            {
                num--;
            }
        }
        printf("%d\n",res);
    }
    return 0 ;
}


你可能感兴趣的:(【尺取法】Jessica's Reading Problem)