zoukankan      html  css  js  c++  java
  • poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用

    jessica's Reading PJroblem
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 9134   Accepted: 2951

    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

    Source

    题意:给你n个数字序列,求最小的子序列的长度,包含所有种类的数字
    分析: 尺取法,能用尺取法的原因:假设当前a[s],a[s+1],,,,,a[t]包含只有种类,当s=s+1时,要仍然包含所有种类,则t'>=t,
            关键是set和map的使用 ,set相当于集合,map相当于数组,map数组可以开的大小范围不知道。。
    int num[1000100],a[1000100];
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            set<int > w;
            memset(num,0,sizeof(num));
            for(int i=1;i<=n;i++)
               {
                   scanf("%d",&a[i]);
                   w.insert(a[i]);
               }
            int cnt=w.size();
            int p=1,q=1,t=1,res=n+1;num[a[p]]++;//第一次re的代码,因为a[i]是整形范围的
    //所以num就会爆内存,故只能换map了

      下面是AC代码

    #include<cstdio>
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <vector>
    #include <queue>
    #include<map>
    #include <algorithm>
    #include <set>
    using namespace std;
    #define MM(a) memset(a,0,sizeof(a))
    typedef long long LL;
    typedef unsigned long long ULL;
    const int mod = 1000000007;
    const double eps = 1e-10;
    const int inf = 0x3f3f3f3f;
    int a[1000100];
    int main()
    {
        int n;
        while(~scanf("%d",&n))
        {
            set<int> w;map<int,int> num;
            for(int i=1;i<=n;i++)
               {
                   scanf("%d",&a[i]);
                   w.insert(a[i]);//set相当于集合
               }  
            int cnt=w.size();   
            int p=1,q=1,t=1,res=n+1;num[a[p]]++;
            for(;;)
            {
                while(t<cnt&&q<=n){
                      q++;
                      if(!num[a[q]])
                        t++;
                      num[a[q]]++;
                }
                if(t<cnt)
                    break;
                if(res>q-p+1) res=q-p+1;
                num[a[p]]--;
                if(!num[a[p]]) t--;
                p++;
            }
            printf("%d
    ",res);
        }
        return 0;
    }
     
    

      

  • 相关阅读:
    pip国内源
    高级信息系统项目管理师十大管理优秀范文
    DOM是什么
    js中!!的妙用
    Angular中@Output()的使用方法
    Js数组内对象去重
    JS去除对象或数组中的空值('',null,undefined,[],{})
    JS数组与字符串相互转换
    Js删除数组中的空值
    promise.all的应用场景举例
  • 原文地址:https://www.cnblogs.com/smilesundream/p/5125088.html
Copyright © 2011-2022 走看看