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 }
  • 相关阅读:
    android: 建立和断开a2dp link 相关方法总结
    Java: volatile和synchronized的区别 (转)
    shell 将变量当命令执行的几种方式
    tcp 自连接(主机的端口和自己建立连接)
    sed使用
    海尔风冷冰箱冷冻室最底层结冰 问题解决
    k8s 传参数的两种方式一种是 环境变量 拼接 另一种说是yaml传参到配置文件的表示怀疑要验证????????????????????????????
    k8s 集群中的etcd故障解决
    dockerfile
    使用VIM/VI给文件加密和解密
  • 原文地址:https://www.cnblogs.com/zgglj-com/p/7282109.html
Copyright © 2011-2022 走看看