POJ-3320 Jessica's Reading Problem 【尺取(or 二分)+STL】

20 篇文章 0 订阅
16 篇文章 0 订阅

B - Jessica’s Reading Problem

Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit

Status

Practice

POJ 3320
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


  1. 题意:给出一个序列,表示每一页上所包含的知识点,可能会有重复的知识点,问复习完所有知识点需要的连续子序列最短是多少;
  2. 思路:用尺取法,便历整个区间,找出符合要求的最小区间,不过需要用map和set来记录是否复习过此知识点和总共有多少知识点(有重复的知识点);
  3. 失误:没用过map和set还挺方便的;其实想用二分过了,无奈总是wa,等我水平提高了在解决你。
  4. 代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<set>
using namespace std;

const int MAXN=1e6+13;
int a[MAXN];

int main()
{
    int L,R,sum,ans,lsub,N,rs,i;
    while(~scanf("%d",&N))
    {
        map<int,int > MP; set<int > S;
        for(i=1;i<=N;++i)
        {
            scanf("%d",&a[i]);
            S.insert(a[i]);//集合插入元素 具有唯一性 
        }
        L=1; R=1; ans=N; lsub=0; sum=0; rs=S.size();
        while(R<=N)//尺取范围 
        {
            while(R<=N&&sum<rs)//如果当前序列不满足要求 R向右移动 
            {
                if(MP[a[R]]==0)//如果新加入的知识点没有复习 
                {
                   ++sum;//复习过的知识点增加1 
                }
                MP[a[R]]++;//记录复习此知识点的次数 
                ++R; ++lsub; //R右移 子序列长度加1 
            }
            while(sum==rs)//如果当前区间已复习完所有知识点 L右移 
            {
                --MP[a[L]]; //去除的知识点复习次数减少1次 
                if(MP[a[L]]==0)  --sum; 
                ++L; --lsub;  //去除后L右移 保证左开右闭           
            } 
            ans=min(ans,lsub+1);//lsub+1易忽略 lsub是刚好不满足题意的 
        }
        printf("%d\n",ans);
     } 
    return 0;   
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值