[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. MySQL数据库运行环境的搭建

    第一步:安装wampserver2.5-Apache-2.4.9-Mysql-5.6.17-php5.5.12-64b文件,安装过程中可能会遇到问题,把遇到的问题代码复制粘贴到360人工服务,查找方案 ...

  2. VSCode Web Developement for Javascript. Must have plugins.

    Es6 Javascript front-end web developemnt must have plugins Prettier - Code Formatter Javascript (ES6 ...

  3. Desert King(POJ2728+最优比率生成树+二分)

    题目链接:http://poj.org/problem?id=2728 题目: 题意:求一颗生成树,使得费用与距离的比值最小,其中距离等于两点之间的平面欧拉距离,费用为z坐标之差. 思路: 由上图我们 ...

  4. python中range函数与列表中删除元素

    一.range函数使用 range(1,5)   代表从1到4(不包含5),结果为:1,2,3,4   ,默认步长为1 range(1,5,2)   结果为:1, 3  (同样不包含5) ,步长为2 ...

  5. Java中的return语句使用总结

    Java中的return语句总是和方法有密切关系,return语句总是用在方法中,有两个作用,一个是返回方法指定类型的值(这个值总是确定的),一个是结束方法的执行(仅仅一个return语句).   在 ...

  6. 【tomcat】手动部署动态JavaWeb项目到tomcat

    1.通过修改server.xml进行配置 1.查看项目的目录结构: tomcat运行时加载WebConmtent目录

  7. Java多线程学习(五)线程间通信知识点补充

    系列文章传送门: Java多线程学习(二)synchronized关键字(1) Java多线程学习(二)synchronized关键字(2) Java多线程学习(三)volatile关键字 Java多 ...

  8. vue基本介绍

    https://cn.vuejs.org/v2/guide/ Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上 ...

  9. 64_l6

    lightdm-qt5-devel-1.22.0-1.fc26.i686.rpm 19-May-2017 11:11 22854 lightdm-qt5-devel-1.22.0-1.fc26.x86 ...

  10. IDEA 内存设置

    -server -Xms2g -Xmx2g -XX:NewRatio=3 -Xss16m -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled - ...