POJ:3320 Jessica's Reading Problem

POJ:3320 Jessica’s Reading Problem(尺取法)

问题描述

**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.**
输入描述
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 one line: the number of pages of the shortest contiguous part of the book which contains all ideals covered in the book.
输入样例:
5
1 8 8 8 1
输出样例:
2
题目大意:
为了准备考试,jessica开始读一本很厚的课本。要想通过考试呢,他必须要掌握书中所有的知识点。这本书一共有P页,第i页恰好有一个知识点ai,(每个知识点都有一个整数编号)。全书中同一个知识点可能会多次提到,所以她希望能通过阅读连续的一些页把所有的知识点都覆盖到。给定每页写到的知识点,求出最少需要阅读多少页。
思路:
首先拿到这个题,想到了曾经看过的一种尺取法,题目要求 要覆盖所有的知识点,并且每个知识点出现的次数不得小于1;因此需要给每一种知识点计数,这里可以想到通过c++STL中set的特性,来记录不同种类知识点的个数。通过二叉树来记录每个知识点出现的次数。下面是ac的代码(由于我习惯性使用c++的cin,cout。第一代 代码提交以后超时,注意到 p的范围是10的六次方,因此 如果读入这么多数据,scanf的速度就充分发挥出来,果断换成scanf AC)
AC代码:

#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
using namespace std;
int p;
int a[1000005];//记录每页的知识点 
set<int > np;//记录知识点的种类 
map<int ,int > cnt;//记录某知识点出现次数 
int main(){
    scanf("%d",&p);
    for(int i=0;i<p;i++){
        scanf("%d",&a[i]);
        np.insert(a[i]);
    }
    int n=np.size(); //得到知识点的种类数目 
    int head=0,tail=0,sum=0; //head tail 用来截取p页书上的子区间。 
    int res=p;
    while(true){
        while(tail<p&&sum<n){ //当不超过页数最大并且 没有覆盖全部知识点时 
            if(cnt[a[tail++]]++==0){ //这里注意后置++的优先级,有助于理解代码 
                //出现一个新知识点
                sum++; 
            }
        }
        if(sum<n)break;  //如果循环结束后 所求区间内不能覆盖所有知识点 则跳出循环 
        res=min(res,tail-head);//没有跳出 则更新最小的区间长度 
        if(--cnt[a[head++]]==0){ //此时将区间头部向后缩进一位 

            sum--;//如果去掉的页码是的某个知识点出现次数变为0  则sum--  
        }
    }
    printf("%d\n",res);
    return 0;
}

代码中注释写的比较详细,相信你都能看懂。第一次写博客,不太会,希望大家多多提意见。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值