zoukankan      html  css  js  c++  java
  • 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;
    }
    
  • 相关阅读:
    Application Loader上传app程序
    Xcode解决代码高亮、语法提示、错误警告等功能失效的解决方法
    c#上iOS apns p12文件制作记录 iOS推送证书制件
    iOS开发~CocoaPods使用详细说明
    ios实现屏幕旋转的方法
    View页面内容的旋转,在某些情况下可替代屏幕旋转使用
    集成乐视点播功能的注意事项
    NSMutableAttributedString 富文本删除线的用法
    Objective-c的@property 详解
    ASIHttpRequest addRequestHeader的处理
  • 原文地址:https://www.cnblogs.com/cmmdc/p/7210173.html
Copyright © 2011-2022 走看看