• 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 }
  • 相关阅读:
    [King.yue]EXT.Grid行双击事件
    [King.yue]关于代码调试时的缓存问题的一个解决办法
    [Buffalo]ASP.NET MVC路由映射
    [Tommas] 如何创建自动化功能测试的基本原则
    [Tommas] Web测试中,各类web控件测试点总结
    [Tommas] ERP系统测试用例设计1(转)
    [King.yue]VS2012 无法启动IIS Express Web服务器的解决方案
    原创 html动态表格
    原创 SqlParameter 事务 批量数据插入
    jquery readio checked
  • 原文地址:https://www.cnblogs.com/767355675hutaishi/p/3950057.html
Copyright © 2020-2023  润新知