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
解题思路:题意就是找出连续的最少页数包含所有知识点ID,典型的尺取法。用set统计知识点的总个数,然后尺取取出某个区间中含有所有知识点,如果有多个满足条件,则取最小的区间长度;注意:右指针向右移动,对应指向的知识点个数加1,如果出现新的知识点,对应种类数加1;左指针向右移动,对应知识点个数先减1,如果其个数减为0,则对应种类数减1。时间复杂度为O(PlogP)。
AC代码(407ms):
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<set>
#include<map>
using namespace std;
const int maxn=;
int P,cnt,bg,ed,res,num,a[maxn];set<int> st;map<int,int> mp;
int main(){
while(~scanf("%d",&P)){
st.clear();mp.clear();//清空
for(int i=;i<P;++i)scanf("%d",&a[i]),st.insert(a[i]);
cnt=st.size();bg=ed=num=;res=P;//res初始化为P,表示最多读P页
while(){
while(ed<P&&num<cnt){
if(mp[a[ed++]]++==)num++;//出现新的知识点
}
if(num<cnt)break;//如果尺取得到的区间中知识点个数小于总个数,不用继续循环了,直接退出
res=min(res,ed-bg);
if(--mp[a[bg++]]==)num--;//队首元素的次数如果减为0,则种数num减1
}
printf("%d\n",res);
}
return ;
}

题解报告:poj 3320 Jessica's Reading Problem(尺取法)的更多相关文章

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

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

  2. 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 ...

  3. poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用

    jessica's Reading PJroblem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9134   Accep ...

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

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

  5. POJ 3320 Jessica's Reading Problem

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

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

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

  7. POJ 3320 Jessica's Reading Problem (尺取法)

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

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

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

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

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

随机推荐

  1. 手机没Root?你照样可以渗透路由器

    和Metasploit差不多,RouterSploit是一个强大的漏洞利用框架,用于快速识别和利用路由器中的普通漏洞,它还有个亮点,就是可以在绝大多数安卓设备上运行. 如果你想在电脑上运行,可以阅读这 ...

  2. win8系统 如何默认显示文件扩展名和显示隐藏文件

    装一个魔方软件,然后再任意文件或者文件夹上面右击,依次点击下面两项,就可以默认显示文件扩展名和显示隐藏文件  

  3. Selenium系列之--03【转】页面元素找不到问题的分析思路

    如果在测试过程中遇到了NoSuchElementException 这个异常, 说明元素查找失败. Caused by: org.openqa.selenium.NoSuchElementExcept ...

  4. MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式

    MVC的验证(模型注解和非侵入式脚本的结合使用)   @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...

  5. 李洪强iOS开发之性能优化技巧

    李洪强iOS开发之性能优化技巧 通过静态 Analyze 工具,以及运行时 Profile 工具分析性能瓶颈,并进行性能优化.结合本人在开发中遇到的问题,可以从以下几个方面进行性能优化. 一.view ...

  6. Android SDK update被墙

    1.输入命令:$ sudo gedit /etc/hosts 2.在打开的 /etc/hosts 在文件的末尾添加下面一句:74.125.237.1 dl-ssl.google.com

  7. MVC为Html对象建立一个扩展方法,使用自己的控件就像使用TextBox一样方便

    先看一下我想要的结果: 很容易它就是一个单选按钮组,当我后台为Html对象(HtmlHelper的一个实例,它被定义在System.Web.Mvc名称空间下的WebViewPage类,即它对于所有MV ...

  8. 小幻美图 API

    『不忘初心,方得始终.』 小幻美图 API 更新:2015.03.29 目前提供的API共有10种! 必应各种今日获取API共4种! 本站收录图片获取API共4种! 网络图片尺寸修改API共1枚! 百 ...

  9. YTU 2928: 取不重复的子串。

    2928: 取不重复的子串. 时间限制: 1 Sec  内存限制: 128 MB 提交: 5  解决: 5 题目描述 输入字母构成的字符串(不大于30字符)从中读取3个不重复的字符,求所有取法,取出的 ...

  10. html5--switch选择结构的优化

    html5--switch选择结构的优化 问题: 使用循环语句判断月份是31天还是30天 两点提示: 使用switch多条件判断语句 合理的省略break优化代码 <!DOCTYPE html& ...