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;
    }
    尺取法
  • 相关阅读:
    php jquery pjax示例源码 (ajax请求,并改变url)
    mysql 中查看指定表的字段名 (可根据字段变量生成c#后台代码)
    原生js Ajax
    ajax basic 认证
    Json序列化问题
    MSSQL 日期操作函数 总结
    用sql语句按周、按月、按季、按年统
    mssql中得到当天数据的语句
    SP_CreateInsertScript 将表内的数据全部拼接成INSERT字符串输出
    MSSQL 获取指定日期所在星期的第一天和最后一天日期 获取指定日期坐在月的第一天和最后一天
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/7118624.html
Copyright © 2011-2022 走看看