• 【二分】Jessica's Reading Problem


    [POJ3320]Jessica's Reading Problem
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 13095   Accepted: 4495

    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

     
    题目大意:选一段长度为k的区间使其包含所有元素,询问最小k
    试题分析:二分区间长度,滑动窗口(尺取法),离散化
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    inline long long read(){
        long long x=0,f=1;char c=getchar();
        for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
        for(;isdigit(c);c=getchar()) x=x*10+c-'0';
        return x*f;
    }
    #define LL long long
    const int MAXN=1000001;
    const int INF=999999;
    const int mod=999993;
    int N;
    long long a[1000001];
    int Hash[1000001];
    long long A[1000001];
    int tmp;
    
    bool check(int k){
        memset(Hash,0,sizeof(Hash));
        int sum=0;
        for(int i=1;i<=k;i++){
            if(!Hash[A[i]]) sum++;
            Hash[A[i]]++;
        }
        if(sum==tmp) return true;
        for(int i=2;i+k-1<=N;i++){
            Hash[A[i-1]]--;
            if(!Hash[A[i-1]]) sum--;
            if(!Hash[A[i+k-1]]) sum++;
            Hash[A[i+k-1]]++;
            if(sum==tmp) return true;
        }
        return false;
    }
    
    int main(){
        N=read();
        for(int i=1;i<=N;i++){
            A[i]=a[i]=read();
        }
        sort(a+1,a+N+1);
        for(int i=1;i<=N;i++) A[i]=lower_bound(a+1,a+N+1,A[i])-a;
        for(int i=1;i<=N;i++) if(!Hash[A[i]]) Hash[A[i]]++,tmp++;
        int l=1,r=N;
        while(l<=r){
            int mid=(l+r)>>1;
            if(check(mid)) r=mid-1;
            else l=mid+1;
        }
        printf("%d
    ",l);
    }
  • 相关阅读:
    markdown
    显示数学公式
    iOS----时间日期处理
    OC中文件读取类(NSFileHandle)介绍和常用使用方法
    深刻理解----修饰变量----关键字
    iOS----轻松掌握AFN网络顶级框架
    iOS
    iOS--多线程之线程间通讯
    iOS--多线程之NSOperation
    iOS--多线程之GCD
  • 原文地址:https://www.cnblogs.com/wxjor/p/7301432.html
Copyright © 2020-2023  润新知