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 }
  • 相关阅读:
    【2021-05-18】人生十三信条
    【2021-05-17】打了第一针疫苗
    【2021-05-16】该工作时好好工作,该休息时好好休息
    【2021-05-15】人生十三信条
    【2021-05-14】要保持团队作战的模式
    【2021-05-13】罗马不是一天能建成的
    【2021-05-12】己所不欲勿施于人
    【2021-05-11】服务好了别人,也就服务好了自己
    二维区域和检索
    寻找重复数
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7282109.html
Copyright © 2011-2022 走看看