[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. python初步学习-python数据类型-字典(dict)

    字典 字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起.注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息. 注意,你只 ...

  2. Part2-HttpClient官方教程-Chapter3-HTTP状态管理

    ps:近日忙于课设与一个赛事的准备....时间真紧啊~~ 最初,HTTP被设计为一种无状态的,面向请求/响应的协议,它并没有为跨越多个逻辑相关的请求/响应交换的有状态会话做出特殊规定.随着HTTP协议 ...

  3. Java案例之士兵作战功能实现

    实现的功能比较简单,主要用到了多态的,抽象类以及模板方法模式这几个知识点.效果图如下,哈哈 ,大神勿喷,后面我会把这些知识点详细介绍出来,即使Java学的不好,只要有一点其他语言基础或者没有应该都能看 ...

  4. c语言中网络字节序和主机字节序的转换

    函数说明   相关函数:htonl, htons, ntohl 头文件:#include <netinet/in.h> 定义函数:unsigned short int ntohs(unsi ...

  5. python—RE正则表达式

    re正则表达式 正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写的 ...

  6. yocto 离线编译

    使用yocto编译一个软件包时,一般会先在本地寻找下载好的源码包,如果不存在则根据配置从网络下载. 添加本地源码包 为了支持离线编译,添加一个包的配置文件后,需要在本地也准备好源码包. 可以先打开网络 ...

  7. EasyHook远程进程注入并hook api的实现

    EasyHook远程进程注入并hook api的实现 http://blog.csdn.net/v6543210/article/details/44276155

  8. 3.FireDAC组件快照

    TFDManager 连接定义和Connect连接管理  TFDConnection 数据库连接组件,支持三种连接方式:1.持久定义(有一个唯一名称和一个配置文件,可以由FDManager管理) 例: ...

  9. Chrome控制台的妙用之使用XPATH

    谷歌浏览器,对于作为程序员的我们来说可以是居家必备了,应该用的相当的熟悉了,我们用的最多的应该是network选项吧,一般用来分析网页加载的请求信息,比如post参数之类的,这些基本的功能基本上够用了 ...

  10. 基数排序c++实现

    基数排序:是一种非比较型整数排序算法,其原理是将整数按位数切割成不同的数字,然后按每个位数分别比较.由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数,所以基数排序也不是只能使用于整数.但在 ...