POJ3320-Jessica's Reading Problem

来源:互联网 发布:爱福窝设计软件下载 编辑:程序博客网 时间:2024/05/21 07:56

Jessica's Reading Problem
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 12353 Accepted: 4202

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

51 8 8 8 1

Sample Output

2

Source

POJ Monthly--2007.08.05, Jerry


题意:给出一个有n个数的序列,问最少多少个连续的数能包含序列中出现的所有数

解题思路:先对所有数字进行离散化,然后用尺取法,用一个数组记录每个数字出现的次数,设序列中一共有m中数,设两个指针,若出现了m种数,更新一下长度,然后移动头指针,直至只有m-1种数


#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <queue>#include <cmath>#include <map>#include <bitset>#include <set>#include <vector>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;int n;int a[1000009],b[1000009],visit[1000009];int main(){    while(~scanf("%d",&n))    {        for(int i=1;i<=n;i++) scanf("%d",&a[i]),b[i]=a[i],visit[i]=0;        sort(b+1,b+1+n);        int m=unique(b+1,b+1+n)-b;        for(int i=1;i<=n;i++)            a[i]=lower_bound(b+1,b+m,a[i])-b;        int mi=INF,head=0,rear=0,cnt=0;        while(rear<n)        {            rear++;            visit[a[rear]]++;            if(visit[a[rear]]==1) cnt++;            while(cnt==m-1)            {                mi=min(mi,rear-head);                visit[a[++head]]--;                if(!visit[a[head]]) cnt--;            }        }        printf("%d\n",mi);    }    return 0;}

原创粉丝点击