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. 磁盘显示为GPT(保护分区)

    问题描述:PE进入系统,在计算机管理里面磁盘显示为GPT(保护分区).此时硬盘是不能重新分区或者格式化的. 解决思路:低版本的WIndows(PE)是不支持GPT分区的,我们需要使用系统自带的Disk ...

  2. uva 10069 Distinct Subsequences 【dp+大数】

    题目:uva 10069 Distinct Subsequences 题意:给出一个子串 x 和母串 s .求子串在母串中的不同序列的个数? 分析:定义dp[i][j]:x 的前 i 个字母在 s 的 ...

  3. mysql数据库优化之表的设计和慢查询定位

    一.数据库优化包含的方面 数据库优化是一种综合性的技术.并非通过某一种方式让数据库效率提高非常多.而是通过多方面的提高.从而使得数据库性能提高. 主要包含: 1.表的设计合理化(3范式) 2.给表加入 ...

  4. Django项目开发-小技巧

    当你开发完一个Django项目之后肯定要吧他丢到服务器让跑起来,但是你在自己的环境下安装了好多的包,是不是在服务器中也要一个个的安装了, pip freeze > read.txt #这条命令会 ...

  5. Linux下用Xdebug调试php

    Linux下用Xdebug调试php 博客分类: php PHPLinuxZendEclipseC# 为了调试PHP程序,安装一下xdebug. 官方网址: http://www.xdebug.org ...

  6. Android Material Design-Maintaining Compatibility(保持兼容性)-(七)

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40634829 翻译自: http://developer.android.com/traini ...

  7. PHP引用是什么?

    引用是什么 在 PHP 中引用意味着用不同的名字访问同一个变量内容.这并不像 C 的指针,替代的是,引用是符号表别名.注意在 PHP 中,变量名和变量内容是不一样的,因此同样的内容可以有不同的名字.最 ...

  8. jQuery经典面试题及答案精选

    jQuery是一款非常流行的Javascript框架,如果你想要从事Web前端开发这个岗位,那么jQuery是你必须掌握而且能够熟练应用的一门技术.本文整理了一些关于jQuery的经典面试题及答案,分 ...

  9. JavaScript语言基础4

    谈谈JavaScript 中的变量. 在JavaScript 中使用变量的优点:变量保存在计算机的内存中,变量很适合于保存暂时性的数据 ,变量仅仅具有有限的生存期,当 用户关闭了页面或者打开一个新的页 ...

  10. android stdio 异常

    1.android studio gradle project sync failed File -> Settings 搜索Gradle 2.eqmu-system-i386未响应   分辨率 ...