POJ3320-Jessica's Reading Problem

Jessica面临考试,需找到课本中覆盖所有知识点的最短连续部分。通过离散化和滑动窗口算法,解决如何快速定位这一区段的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

5
1 8 8 8 1

Sample Output

2

Source



题意:给出一个有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 long
const 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值