zoukankan      html  css  js  c++  java
  • POJ 3320.Jessica's Reading Problem 尺取法

    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 12488   Accepted: 4255

    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

    题意:一本有p页的书,每一页上面都有一个知识点。问读最少的连续页数,使得学完所有的知识点。
    思路:尺取法。
    代码:
    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<map>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<set>
    using namespace std;
    #define PI acos(-1.0)
    typedef long long ll;
    typedef pair<int,int> P;
    const int maxn=1e5+110,maxm=1e5+100,inf=0x3f3f3f3f,mod=1e9+7;
    const ll INF=1e13+7;
    int a[maxn];
    map<int,int> sign,flag;
    int main()
    {
        int T;
        int n,s;
        scanf("%d",&n);
        int all=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            if(++flag[a[i]]==1) all++;
        }
        int l=0,cou=0;
        int ans=n;
        for(int i=1; i<=n; i++)
        {
            if(++sign[a[i]]==1) cou++;
            while(cou==all)
            {
                //printf("(%d%d]
    ",l,i);
                ans=min(ans,i-l);
                if(--sign[a[++l]]==0) cou--;
                if(l==i) break;
            }
        }
        cout<<ans<<endl;
        return 0;
    }
    尺取法
  • 相关阅读:
    Solr的配置和在java中的使用
    druid + mysql + mybatis 批量更新报错
    linux find命令-print0和xargs中-0使用技巧
    Shell 函数相关
    公钥、私钥和数字签名这样最好理解
    Liunx find/locate/whereis/which 总结
    Linux find命令
    Shell bash 数学运算 bc
    Shell 使用 expr 进行数学运算
    Shell 有类型变量
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7118624.html
Copyright © 2011-2022 走看看