Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6496   Accepted: 1998

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

 
尺取法求解
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map> using namespace std; #define maxn 1000005 int p,len = ;
int a[maxn];
map<int,int> ele; void solve() {
map<int,int> vis;
int s = ,sum = ,pos = ;
int ans = p; for(; ; ++s) {
while(sum < len && pos <= p) {
if(vis.find(a[pos]) == vis.end() || vis[ a[pos] ] == ) {
++sum;
}
++vis[ a[pos++] ];
} if(sum < len) break;
if(--vis[ a[s] ] == ) {
--sum;
}
ans = min(ans,pos - s);
} printf("%d\n",ans);
} int main()
{
// freopen("sw.in","r",stdin); scanf("%d",&p);
for(int i = ; i <= p; ++i) {
scanf("%d",&a[i]);
if(ele.find(a[i]) == ele.end()) {
ele[ a[i] ] = len++;
}
}
//printf("len = %d\n",len); solve();
return ;
}

POJ 3320的更多相关文章

  1. 【尺取】POJ 3320

    POJ 3320 Jessica's Reading Problem 题意:一本书P页,第i页有ai知识点,问你至少从某一处开始连续要翻多少页才能复习完所有的知识点,不能跨页翻. 思路:<挑战程 ...

  2. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

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

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

  4. A - Jessica's Reading Problem POJ - 3320 尺取

    A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...

  5. POJ 3320 (尺取法+Hash)

    题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...

  6. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  7. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

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

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  9. poj 3320(尺取法)

    传送门:Problem 3320 参考资料: [1]:挑战程序设计竞赛 题意: 一本书有 P 页,每页都有个知识点a[i],知识点可能重复,求包含所有知识点的最少的页数. 题解: 相关说明: 设以a[ ...

随机推荐

  1. makefile复习时发现的编写makefile规则注意事项

    博客中关于makefile的博文数不胜数,比较经典的都很相似,下面这一片,很全面,只是很长,可以作为参考资料:http://blog.csdn.net/liang13664759/article/de ...

  2. php之常用函数库

    1.时间和日期 如何获取时间戳 time()--从1970年开始计算的毫秒数 echo time(); 日期 echo date('Y-m-d H:i:s'); 获取默认是时区 echo date_d ...

  3. dubbox使用

    1.命令行下 git clone https://github.com/dangdangdotcom/dubbox 2.mvn install -Dmaven.test.skip=true 跳过测试编 ...

  4. win7 系统如何设置快速启动栏

    a.在任务栏上右键 -> 工具栏 -> 新建工具栏 -> 跳出选择文件夹对话框,在文件夹里面(光标山洞处)输入这个路径,然后按回车: %userprofile%\AppData\Ro ...

  5. 微软职位内部推荐-SDEII for Windows Phone Apps

    微软近期Open的职位: Job title: Software Design Engineer II Location: China, Beijing Division: Operations Sy ...

  6. eclipse格式化代码末班修改

    在窗口->首选项->输入format(格式)搜索,或者找Java->代码样式->格式化程序: 几个内置的不能调格式化代码风格,但是可以根据内置的新建一个,出来很多选项,开始调吧 ...

  7. USB硬件远程共享解决iphone已停用

    悲剧的在iphone拆过电池之后,再开机显示iphone已停用,请在23000000分钟后再试一次 算算这得45年了,可以留给孙子用了... 网上除了刷机和有同步过的电脑貌似没有别的办法了 因是旧系统 ...

  8. 网络攻防工具介绍——Wireshark

    网络攻防工具介绍 Wireshark 简介 Wireshark(前称Ethereal)是一个网络封包分析软件.它是一个理想的开源多平台网络协议分析工具.网络封包分析软件的功能是撷取网络封包,并尽可能显 ...

  9. 软件工程课后作业——四则运算Ⅲ(C++)

    一.设计思路 题目:可以答题并判断对错,最后显示做对几道题. 在原有的基础上,又拓展了答题模块. 在结构体中添加了answer属性,把输入的答案与正确答案比较,若相等则计数加一. 二.源代码 (1)四 ...

  10. ios应运程序的五种状态

    ios应运程序的五种状态即转化  从apple的官方文档扣下来的 5状态: Not running The app has not been launched or was running but w ...