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

    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

     1 #include"iostream"
     2 #include"cstdio"
     3 #include"cstring"
     4 #include"algorithm"
     5 #include"set"
     6 #include"map"
     7 using namespace std;
     8 const int ms=1000001;
     9 int a[ms];
    10 int P;
    11 void solve()
    12 {
    13     set<int> S;
    14     for(int i=0;i<P;i++)
    15         S.insert(a[i]);
    16     int n=S.size();
    17     int ans=P,num=0,s=0,t=0;
    18     map<int,int> mp;
    19     while(1)
    20     {
    21         while(t<P&&num<n)
    22         {
    23             if(mp[a[t++]]++==0)
    24                 num++;
    25         }
    26         if(num<n)
    27             break;
    28         ans=min(ans,t-s);
    29         if(--mp[a[s++]]==0)
    30             num--;
    31     }
    32     printf("%d
    ",ans);
    33     return ;
    34 }
    35 int main()
    36 {
    37     scanf("%d",&P);
    38     for(int i=0;i<P;i++)
    39         scanf("%d",&a[i]);
    40     solve();
    41     return 0;
    42 }
  • 相关阅读:
    函数对象与闭包函数
    day15
    应用在tomcat下的四种部署方式(原创)
    Servlet的四种映射模式
    背景框代码
    RabbitMQ 示例-生产者-消费者-direct-topic-fanout
    idea 设置自动生成注释
    cSpring Boot整合RabbitMQ详细教程
    Windows下RabbitMQ安装及配置
    Threadlocal线程本地变量理解
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3950057.html
Copyright © 2011-2022 走看看