• (尺取法)POJ 3320 Jessica's Reading Problem


    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

    首先,使用set求出不同”数字“的个数。之后采用尺取法的想法,对第几页进行尺取即可。

     1 #include <iostream>
     2 #include <string>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <queue>
     8 #include <set>
     9 #include <map>
    10 #include <list>
    11 #include <vector>
    12 #include <stack>
    13 #define mp make_pair
    14 #define MIN(a,b) (a>b?b:a)
    15 //#define MAX(a,b) (a>b?a:b)
    16 typedef long long ll;
    17 typedef unsigned long long ull;
    18 const int MAX=1e5+5;
    19 const int INF=1e9+5;
    20 const double M=4e18;
    21 using namespace std;
    22 const int MOD=1e9+7;
    23 typedef pair<int,int> pii;
    24 const double eps=0.000000001;
    25 int p,n;
    26 int a[MAX];
    27 map <int,int> num;
    28 set <int> re;
    29 int st,en;
    30 int cnt,an;
    31 int main()
    32 {
    33     scanf("%d",&p);
    34     for(int i=1;i<=p;i++)
    35     {
    36         scanf("%d",&a[i]);
    37         re.insert(a[i]);
    38     }
    39     n=re.size();
    40     st=en=1;
    41     an=INF;
    42     for(;;)
    43     {
    44         while(en<=p&&cnt<n)
    45         {
    46             if(num[a[en++]]++==0)
    47                 ++cnt;
    48         }
    49         if(cnt<n)
    50             break;
    51         else
    52         {
    53             an=min(an,en-st);
    54             if(!(--num[a[st++]]))
    55                 --cnt;
    56         }
    57     }
    58     printf("%d
    ",an);
    59     return 0;
    60 }
  • 相关阅读:
    强化学习第2版第15章笔记——神经科学
    强化学习第2版第14章笔记——心理学
    Rainbow: Combining Improvements in Deep Reinforcement Learning
    强化学习模型实现RL-Adventure(DQN)
    【Raspberry Pi】 小问题汇总
    Dir命令
    Ubuntu 安装GNU Scientific library(GSL)
    【Raspberry Pi】USB无线网卡自动连接
    【Error】SSL InsecurePlatform error when using Requests package
    【Python】日期模块总结
  • 原文地址:https://www.cnblogs.com/quintessence/p/6889641.html
Copyright © 2020-2023  润新知