Poj 3320 Jessica's Reading Problem【尺取法】

Jessica面临考试,需掌握课本所有知识点。通过连续阅读部分章节来覆盖全部知识点,目标是最小化这部分章节的长度。

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

Jessica's Reading Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10870 Accepted: 3647

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页的一本书,每页书包含一种知识点,问最少看连续的多少页的书就能看完整本书的所有知识点。

分析样例:看第一页和第二页就能看完整本书的所有知识点。


思路:


1、如果设定区间sum【1,t】=x表示从第一页到第t页的连续区间内包含x种知识点,并且设定contz表示整本书一共有多少种知识点、

如果设定sum【1,t】==contz&&sum【1,t-1】<contz;

那么如果还有:sum【2,t】<contz

那么如果再有:sum【2,tt】==contz;

那么一定有tt>t


2、那么根据以上特性,我们设定一个起点t,一共终点s, 一个知识点覆盖数sum,表示连续子区间的起点和终点,以及一共包含了多少知识点。

算法过程如下:

①首先使用map映射求出这本书一共有多少个知识点、

②初始化t=s=sum=0;

③如果sum<contz:

情况1:如果当前知识点在从s到t-1没有出现过,那么sum++;对应标示上当前这个知识点出现过了。

步骤2:如果当前知识点在s到t-1出现过,那么从s开始向t扫,如果当前知识点出现过多次我们就可以将当前起点向后推移,就对应s++,否则跳出;

结束后t++;

④当sum==contz时,对应将答案维护即可。(一些细节详见代码进行参考),并且还要从s开始向t扫,如果当前知识点出现过多次,我们将起点向后推移,对应s++;然后在这个过程结束之后,s++(表示去掉一个知识点),继续进行尺取法。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<map>
using namespace std;
int a[1200050];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        map<int ,int >s;
        s.clear();
        int cont=1;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
            if(s[a[i]]==0)
            {
                s[a[i]]=cont++;
            }
        }
        //cont-1表示一共有多少种知识点。
        int ss=0,tt=0,sum=0;
        int ans=0x3f3f3f3f;
        s.clear();
        while(1)
        {
            while(sum<cont-1&&tt<n)
            {
                if(s[a[tt]]==0)
                {
                    s[a[tt]]++;
                    sum++;
                }

                else
                {
                    s[a[tt]]++;
                    while(ss<tt&&s[a[ss]]>1)
                    {
                        s[a[ss]]--;
                        ss++;
                    }
                }
                tt++;
            }
            if(sum<cont-1)break;
            ans=min(ans,tt-ss);
            s[a[ss]]--;
            sum--;
            ss++;
            while(ss<tt&&s[a[ss]]>1)
            {
                s[a[ss]]--;
                ss++;
            }
        }
        printf("%d\n",ans);
    }
}
/*
6
1 2 3 2 1 6
*/





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值