[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\n",l);
}

【二分】Jessica's Reading Problem的更多相关文章

  1. POJ 3320 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6001   Accept ...

  2. Greedy:Jessica's Reading Problem(POJ 3320)

    Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...

  3. POJ3320 Jessica's Reading Problem(尺取+map+set)

    POJ3320 Jessica's Reading Problem set用来统计所有不重复的知识点的数,map用来维护区间[s,t]上每个知识点出现的次数,此题很好的体现了map的灵活应用 #inc ...

  4. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

  5. POJ3320 Jessica's Reading Problem 2017-05-25 19:55 38人阅读 评论(0) 收藏

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12346   Accep ...

  6. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...

  7. Jessica's Reading Problem——POJ3320

    Jessica's Reading Problem——POJ3320 题目大意: Jessica 将面临考试,她只能临时抱佛脚的在短时间内将课本内的所有知识点过一轮,课本里面的P个知识点顺序混乱,而且 ...

  8. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  9. POJ 3220 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12944   Accep ...

随机推荐

  1. Achain 钱包部署

    官网 GIT: [ Achain_linux ] 基础环境 OS: CentOS, Ubuntu Achain: 官网 [ release 最新版本 ] 安装 Achain 钱包 下载 CentOS ...

  2. js_一个简单的30分钟循环倒计时

    吐槽段: 需求的变更是千变万化的,至少在你说服和你打交道的那位谁谁谁之前. 创业公司就是这样,产品经理一个想法,就是改改改,管你改起来复杂不复杂,在他们眼里都是非常简单的. 今天的一个小改动需求,把活 ...

  3. python基础===获取知乎标题时候,文件编码失败的总结

    总结一下,关于获取到的信息编码失败. 刚才在执行代码的时候,发现一个问题: 然后修改代码如下: '''爬取知乎界面的标题''' import requests import re import sys ...

  4. HDU 6115 Factory LCA,暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6115 题意:中文题面 分析:直接维护LCA,然后暴力枚举集合维护答案即可. #include < ...

  5. Unsupported gpu architecture 'compute_20'

    NVCC src/caffe/layers/reduction_layer.cunvcc fatal   : Unsupported gpu architecture 'compute_20'Make ...

  6. VM虚拟机,Linux系统安装tools过程遇到 what is the location of the “ifconfig” program

    安装步骤: 复制到/mnt 解压文件 tar -zxvf VMwareTools-10.1.6-5214329.tar.gz 进入减压文件夹后安装 ./vmware-install.pl ... 一直 ...

  7. 如何在苹果官网下载旧版本的Xcode

    如何在苹果官网下载旧版本的Xcode 前段时间XcodeGhost事件让很多应用中招,不乏一些知名的互联网公司开发的应用.事件的起因是开发者使用了非官方的Xcode,这些Xcode带有xcodegho ...

  8. 给自己立一个flag

    工作理念:做完!做对!做好!做优! 1.请教问题方面 遇到问题先自己想办法解决(限定时长为30分钟). 请教问题的时候,明确:“问题是什么,为什么错在那里,结果是什么” 2.博客 一周两篇左右:对工作 ...

  9. plus.networkinfo.getCurrentType()

    HTML5+API device Device Device模块管理设备信息,用于获取手机设备的相关信息,如IMEI.IMSI.型号.厂商等.通过plus.device获取设备信息管理对象. 对象: ...

  10. 构造函数、原型对象prototype、实例、隐式原型__proto__的理解

    (欢迎一起探讨,如果有什么地方写的不准确或是不正确也欢迎大家指出来~) PS: 内容中的__proto__可能会被markdown语法导致显示为proto. 建议将构造函数中的方法都定义到构造函数的原 ...