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

    Jessica's Reading Problem
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23019   Accepted: 7776

    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
    Jessica's Reading Problem
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 23019   Accepted: 7776

    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

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll  long long
    #define PII  pair<int, int>
    using namespace std;
    int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1e9 + 7;
    const int maxn = 200000;
    //if(x<0 || x>=r || y<0 || y>=c)
    //1000000000000000000
    
    int main()
    {
        int n;
        cin >> n;
        vector<int> a(n);
        map<int, int> mp,cnt;
        for (int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
            mp[a[i]]++;
        }
        int en=0, st=0,sum=0,ans=inf;
        while (1)
        {
            while (en < n && cnt.size()<mp.size())
            {
                cnt[a[en]]++;
                en++;
            }
            if (cnt.size() < mp.size())
                break;
            ans = min(ans, en - st);
            cnt[a[st]]--;
            if(cnt[a[st]]==0)
                cnt.erase(a[st]);
            st++;
        }
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    如何打开windows的远程桌面
    mongodb存储引擎
    使用场景举例
    webpack初识
    mongodb快速入门
    mongodb初识
    mysql主从复制与读写分离
    gradle简单配置跟模块依赖
    mysql执行计划
    synchronized的可见性理解
  • 原文地址:https://www.cnblogs.com/dealer/p/12653677.html
Copyright © 2011-2022 走看看