POJ3320 Jessica's Reading Problem(尺取法)

Jessica's Reading Problem
Time Limit: 1000MS      Memory Limit: 65536K
Total Submissions: 11032        Accepted: 3718
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
好久没刷题了,有点惭愧,收心收心,脑袋有点秀逗了。
哎。惭愧。
直接尺取法。
set统计多少个知识点。
map记录当前读取数。
从前往后读,小剪枝,所读取的页数(已包含所有知识点)的数量刚好等于所有知识点的数量,直接输出。
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<ctime>
#include<string>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#include<set>
#include<map>
#include<cstdio>
#include<limits.h>
#define MOD 1000000007
#define fir first
#define sec second
#define fin freopen("/home/ostreambaba/文档/input.txt", "r", stdin)
#define fout freopen("/home/ostreambaba/文档/output.txt", "w", stdout)
#define mes(x, m) memset(x, m, sizeof(x))
#define Pii pair<int, int>
#define Pll pair<ll, ll>
#define INF 1e9+7
#define Pi 4.0*atan(1.0)

#define lowbit(x) (x&(-x))
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1

typedef long long ll;
typedef unsigned long long ull;
const double eps = 1e-12;
const int maxn = 1000010;
const int maxm = 200010;
using namespace std;

inline int read(){
    int x(0),f(1);
    char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int bk[maxn],N;
set<int> st;
void solve()
{
    int t,s,res,len,re;
    len=st.size();
    map<int,int> mp;
    t=0,s=0,res=N,re=0;
    for(;;){
        while(t<N&&re<len){
            if(mp[bk[t]]==0){
                ++re;
            }
            ++mp[bk[t]];
            ++t;
        }
        if(re<len){
            break;
        }
        res=min(res,t-s);
        if(res==len){
            printf("%d\n",res);
            return;
        }
        if(mp[bk[s]]==1){
            --re;
        }
        --mp[bk[s]];
        ++s;
    }
    printf("%d\n",res);
}
int main()
{
    scanf("%d",&N);
    for(int i=0;i<N;++i){
        bk[i]=read();
       // scanf("%d",&bk[i]);
        st.insert(bk[i]);
    }
    solve();
    return 0;
}

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