zoukankan      html  css  js  c++  java
  • Jessica's Reading Problem POJ

    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 #include<set>
     2 #include<map>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<iostream>
     6 #include<algorithm>
     7 using namespace std;
     8 
     9 const int maxn=1000005;
    10 
    11 int n; 
    12 int p[maxn];
    13 
    14 int main()
    15 {    while(cin>>n){
    16         set<int> s;
    17         for(int i=0;i<n;i++){
    18             scanf("%d",&p[i]);
    19             s.insert(p[i]);
    20         }
    21         
    22         map<int,int> mp;
    23         int l=0,r=0,sum=0,ans=n,len=s.size();
    24         while(true){
    25             while(r<n&&sum<len) if(!mp[p[r++]]++) sum++;
    26             if(sum<len||l>r) break;
    27             ans=min(ans,r-l);
    28             if(--mp[p[l++]]==0) sum--;
    29         }
    30         cout<<ans<<endl;
    31     }
    32     return 0;
    33 }
  • 相关阅读:
    Git轻松入门3:远程仓库篇
    Git轻松入门2:分支篇
    Git轻松入门1:本地仓库篇
    通俗易懂的解释:什么是API
    小白都看得懂的Javadoc使用教程
    尾调用与尾递归
    要理解递归就要先理解递归:手把手教你写递归
    不复杂的空间复杂度
    不复杂的时间复杂度
    Java程序执行过程及内存机制
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7282109.html
Copyright © 2011-2022 走看看