• POJ 3320 Jessica's Reading Problem (尺取法)


    题目链接

    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

    分析:
    我们假设从某一页s开始阅读,为了覆盖所有的知识点需要阅读到t页。这样的话就知道如果从s+1开始阅读的话,那么必须阅读到t'>=t页为止。因此我们可以用尺取法求解这道题。

    在某个区间[s,t]已经覆盖所有的只是点的情况下,下一个区间s+1,t'应该如何求出呢?
    所有的只是点都被覆盖《=》每个知识点出现的次数都不小于1.

    由以上的等价关系,我们可以用二叉树等数据结构来存储[s,t]区间上每个知识点出现的次数,这样吧最开始的页s去除后便可以判断[s+1,t]是否满足条件。

    从去间的最开头把s取出之后,页s上书写的知识点出现的次数就要减1,此时如果这个知识点的出现次数为0 了,在同一个知识点再出现之前,不停将区间末尾t向后推进即可。每次在期间末尾追加页t时将页t上的知识点出现的次数加一,这样就完成下一个区间上各个知识点出现次数的更新。从而求得最小区间。

    **代码:

    #include<iostream>
    #include<stdio.h>
    #include<set>
    #include<map>
    using namespace std;
    int n;
    int a[1000009];
    void solve()
    {
        set<int>all;///et本身具有去重的功能,相当于计算出书中一共有多少个知识点
        for(int i=0;i<n;i++)
            all.insert(a[i]);
        int num=all.size();///书中的知识点的个数
    
        map<int,int>mp;
        int s=0,t=0,sum=0;///分别表示起始页,结束页,区间内的知识点个数
        int res=n;///最后的答案
        for(;;)
        {
            while(t<n&&sum<num)
            {
                if(mp[a[t++]]++==0)///这页上的知识点没有出现过
                    sum++;
            }
            if(sum<num) break;///找到最后也没有能将所有的知识点都看完
    
            res=min(res,t-s);
            if(--mp[a[s++]]==0)///起始页上的知识点在以后的页上都没有出现
                sum--;
        }
        printf("%d
    ",res);
    }
    int main()
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
            scanf("%d",&a[i]);
        solve();
        return 0;
    }
    
  • 相关阅读:
    git push :推送本地更改到远程仓库的三种模式
    GIT版本库回滚【图文版】
    微服务架构的分布式事务解决方案
    断路器-Hystrix的深入了解
    Elasticsearch顶尖高手系列课程推荐
    Dockerfile分离构建LNMP环境部署wordpress
    ELK 聚合查询
    tomcat日志采集
    ELK采集之nginx 之高德地图出城市IP分布图
    Elasticsearch 安装与集群配置
  • 原文地址:https://www.cnblogs.com/cmmdc/p/7210173.html
Copyright © 2020-2023  润新知