Jessica‘s Reading Problem(尺取法)

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.

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

题意:已知一本书的每一页和每一页上的知识点,找出能涵盖所有知识点的最少连续页数。

解题思路:要找的是满足要求的一段最短区间,考虑用尺取法来做,这里用尺取法的难点是怎么判断在这个区间内有几个不同的知识点,以及一共有多少个不同知识点,这里我选择用map的大小来统计不同知识点的总数,用map的键值来统计相同知识点的数量,每次移动左右边界进行相同知识点个数的加或减,当相同知识点个数为0时来判断不同知识点个数加或减。

AC代码

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
using namespace std;

int a[1000010];
int main()
{
    int p;
    cin>>p;
    map<int,int>page;         
    for(int i=0; i<p; i++)
    {
        scanf("%d",&a[i]);                 //不能用cin,会超时;
        page[a[i]]++;               //统计每种知识点各占几页;
    }
    int sum=page.size();            //map的大小为这本书中共含有多少个不同知识点;
    //cout<<sum<<endl;              
    page.clear();                  //统计完成后清空map;
    //cout<<page.size()<<endl;
    int l=0,r=0,ans=p;
    while(1)
    {
        while(r<p&&page.size()<sum)     
        {
            page[a[r++]]++;         
        }
        //cout<<ans<<endl;
        //cout<<r<<endl;
        //cout<<l<<endl;
        if(page.size()<sum)        //if语句要放在ans前面,可以把最后不符合要求的答案去掉;
            break;
        ans=min(ans,r-l);
        page[a[l++]]--;
        if(page[a[l-1]]==0)      
            page.erase(a[l-1]);   //当相同知识点个数为0时,要把这个知识点移除来改变map的大小;
    }
    printf("%d\n",ans);
    return 0;
}

map+set:

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <set>
using namespace std;

int a[1000010];
int main()
{
    int p;
    cin>>p;
    map<int,int>page;
    set<int>sum;
    for(int i=0; i<p; i++)
    {
        scanf("%d",&a[i]);         //用scanf,不要用cin;
        //page[a[i]]++;
        sum.insert(a[i]);
    }
    int countt=sum.size();    //用set的大小统计不同知识点的总个数;
    //int sum=page.size();
    //page.clear();
    int l=0,r=0,ans=p,num=0;
    while(1)
    {
        while(r<p&&num<countt)
        {
            if(page[a[r]]==0)    //当新加入一个不同知识点的时候num就+1;
                num++;
            page[a[r++]]++;     //统计相同知识点的个数; 
        }
        //cout<<l<<" "<<r<<" "<<endl;
        if(num<countt)         
            break;
        ans=min(ans,r-l);
        page[a[l++]]--;       //左边界移动map值自减;
        if(page[a[l-1]]==0)   //当map值为零时,表示移除了一个不同知识点,num--;
            num--;
    }
    printf("%d\n",ans);
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值