zoukankan      html  css  js  c++  java
  • POJ3320 Jessica's Reading Problem(尺取法)

    题目链接

    Jessica's Reading Problem

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 17034   Accepted: 5898

    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页为止。由此这题也可以使用尺取法。

    根据“set容器中只能存储键,是单纯的键的集合,其中键是不能重复的”这一性质可求出知识点的种类数。

    用map维护知识点->出现次数的映射,利用下标操作添加元素或查找元素。

    戳此参考 C++ STL map 下标操作注意事项

    AC代码:

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<string>
     5 #include<sstream>
     6 #include<vector>
     7 #include<set>
     8 #include<map>
     9 using namespace std;
    10 int P,a[1000010];
    11 int main()
    12 {
    13     ios::sync_with_stdio(false);
    14     cin.tie(0);cout.tie(0);
    15     cin>>P;
    16     set<int> all;
    17     for(int i=1;i<=P;i++) 
    18     {
    19         cin>>a[i];
    20         all.insert(a[i]);
    21     }
    22     int n=all.size();//知识点的种类数 
    23     
    24     int s=1,t=1,num=0,res=P;
    25     map<int,int> count;//知识点->出现次数的映射 
    26     for(;;)
    27     {
    28         while(t<=P&&num<n)
    29         {
    30             if(count[a[t++]]++==0) num++;//出现新的知识点 
    31         }
    32         if(num<n) break;
    33         res=min(res,t-s);
    34         if(--count[a[s++]]==0) num--; 
    35     }
    36     
    37     cout<<res;
    38 }
    39  
    View Code

    小细节:需要注意的是a数组应该设为全局变量,用cin输入数据记得关同步。

  • 相关阅读:
    关于J2SE/Jsp/Sping/Hibernate/Struts2的视频下载
    JQuery插件之图片轮播插件–slideBox
    javascript学习-创建json对象数据,遍历
    MD5算法原理
    【Linux】linux经常使用基本命令
    藏书阁(非技术流书籍)
    搭建自己的SIPserver:开源sipserveropensips的搭建及终端TwInkle的使用
    linux下仅仅有rman备份集的异机不同文件夹恢复
    URAL 1684. Jack&#39;s Last Word KMP
    javabean总结
  • 原文地址:https://www.cnblogs.com/wangzhebufangqi/p/12796153.html
Copyright © 2011-2022 走看看