题干:

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

题目大意:

有一本书有n个章节,但是有的章节是在讲相同的内容,现在每个内容用数字代替,问你最少需要读多少章节才可以把所有的内容都学到手?

解题报告:

一看就是尺取嘛,,然后cur==cnt之后,别忘看看l还可不可以往右移动就好了。

AC代码:

#include<cstdio>
#include<map>
#include<iostream>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e6 + 5;
int a[MAX];
int cnt,cur,ans;
map<int , int> mp;
int main()
{int n;while(~scanf("%d",&n)) {cnt = cur = 0;ans = 0x3f3f3f3f;mp.clear();for(int i = 1; i<=n; i++) {scanf("%d",a+i);if(mp.find(a[i]) == mp.end()) mp[a[i]]=1,cnt++;         } int l = 1,r = 1;mp.clear();while(r <= n) {if(mp[a[r]] == 0) cur++;mp[a[r]]++;if(cur >= cnt) {while(cur > cnt) {mp[a[l]]--;if(mp[a[l]] == 0) cur--;l++;}while(mp[a[l]] >= 2) {mp[a[l]]--;l++; }               ans = min(ans,r-l+1); }r++;}printf("%d\n",ans);} return 0 ;}
/*
4
3 3 2 1
4
2 3 2 1
*/

总结:

1.和这题一样【HDU - 5672】String(尺取法),都是要注意一种样例:

4

3 3 2 1

和:

4

3 2 3 1

。如果找到cur==cnt就更新答案了,那就错了,因为左边说不定还是可以缩减的。但是我给的这两个样例是两种类型的!!

如果仅仅针对第一个样例可以这样        while(a[l] == a[l+1]) l++;

但是对第二个又过不了,因为这几个3不一定连续!!

2.这题数据量1e6,用map的话是516msAC的,有点慢,这题可以用hash数组

【POJ - 3320 】Jessica's Reading Problem (尺取,哈希)相关推荐

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

    Description 给出一段长度为\(n\)的整数序列,求包含所有出现过的数的连续区间的最小长度. Input 第一行给出序列长度\(n\),第二行给出\(n\)个整数,表示这个序列.\(n \l ...

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

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

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

  4. POJ3320 Jessica's Reading Problem 尺取法

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

  5. Jessica‘s Reading Problem(尺取法)

    Jessica's Reading Problem (尺取法) Jessica's a very lovely girl wooed by lots of boys. Recently she has ...

  6. poj3320Jessica's Reading Problem—尺取法(java)

    题目链接 大意:给序列数字,找出最小子序列,包含所有的元素类型.例如 5 1 8 8 8 1 输出2,因为1 8就包含了所有元素 思路:尺取法 这个和裸的尺取优点不同的是,他需要一个map来维护判断而 ...

  7. Jessica's Reading Problem

    第一部分:题目 Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. ...

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

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

  9. Bound Found POJ - 2566 (尺取+前缀和)

    题意 就是给一串序列 在给一个t 求一段区间的加和绝对值与t最小的值与区间左右端点是多少 思路 这道题一开始不知道如何去做 用尺取法求区间但是因为区间中存在负数没有单调性  没有特殊的特征 若对区间求 ...

最新文章

  1. php拦截器配置,php 类中的各种拦截器
  2. ipad UISplitViewController 导航视图控制器标题设置
  3. mysql把select结果存到变量中_mysql实例 select into保存到变量的例子
  4. python 字符串函数 center_Python字符串处理
  5. easyui form表单提交标准格式
  6. 18.海量分布式存储系统 Doris 的高可用架构设计分析
  7. 今天博客园肿了吗?希望团队修复一下
  8. ECCV 2018 papers+ oral+ 开源+导读
  9. winows8.1或winows7 64bit 安装Itunes 64bit 11.1.3 无法打开一直停止工作的解决办法
  10. 知悉未来的趋势 ---- 小评 创新者的窘境
  11. [Mac]图像处理JPEG工具箱jpegtbx_1.4的MATLAB编译及使用
  12. 转贴:Objective-C Tutorial
  13. unity篇-官方序列化接口 ISerializationCallbackReceiver
  14. 时间序列多步预测的一般方法
  15. WPS格式转换成Word的方法
  16. 世界名画陈列馆(最少机器人问题和不重复监视问题)
  17. 树莓派GPIO 基础(二)
  18. 炉石传说 爬取全部卡牌
  19. CYCADA: cycle-consistent adversaial domain adaption阅读笔记
  20. IDEA搭建一个SpringBoot项目——十分详细(web+mysql)

热门文章

  1. 判断一个数是偶数还是奇数
  2. c语言中的取模运算符_C语言除法算法和取模运算的实现(多种算法,多种思路)...
  3. lombok的使用三部曲及使用中遇到的问题(持续更新)
  4. 江阳职高计算机应用教改实验,计算机应用课程教改模式
  5. 高仿人人android梦想版终极源码发送,人人Android客户端梦想版发布
  6. python绘画音频_人工智能下的音频还能这样玩!!!!
  7. 成都软件工程师python_为什么每个软件工程师都应该学习Python?
  8. apache根据ip分发_腾讯广告进入“IP新融点”时代
  9. 用c语言编程减法计算,求用C编个大数加减法运算程序
  10. ubuntu/wireshark --Lua: Error during loading: [string /usr/share/wireshark/init.lua]:45问题解决