Jessica’s Reading Problem

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 22716 Accepted: 7687

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

题目意思就是要读最少的连续书页,复习到所有的知识点。
这道题目跟 poj3061 有点像,但是比之稍复杂。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
const int N = 1e6 + 10;
map<int, int> m, Count;//第一个m是记录书的总页数。第二个是用来后面计数的
int a[N], n, ans, sum;
int solve() {int now = 0,l = 1, r = 1;while(true) {while(now < sum && r <= n) {//条件满足或者后面没有元素了if(Count[a[r++]]++ == 0)//这是第一次加入这本书。now++;}if(now < sum)break;ans = min(ans, r - l);//取答案的最下值。if(--Count[a[l++]] == 0)//这是里面的最后一本书。now--;}return ans;
}
int main() {while(scanf("%d", &n) != EOF) {m.clear();//队组读入注意清零。Count.clear();ans = N, sum = 0;for(int i = 1; i <= n; i++) {scanf("%d", &a[i]);if(m[a[i]]++ == 0)sum++;}printf("%d\n", solve());}return 0;
}

POJ3320 Jessica's Reading Problem 尺取法相关推荐

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

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

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

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

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

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

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

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

  5. 【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 i ...

  6. Jessica's Reading Problem

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

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

  8. POJ-2566,HDU-1058,POJ-3320,POJ-3061(尺取法)

    题目链接: Bound Found 思路 用前缀和先统计每个数到第一个数的总和.因为尺取法需要数组有序,所以对前缀和得到的数组排序.设立两个类似指针了l,r,使得l,r不断前进,并记录这之间产生的最小 ...

  9. ACM常用技巧之尺取法--POJ3061/3320/2739/2100

    尺取法:反复推进区间的开头和结尾,来求取满足条件的最小区间的方法  .    <挑战程序设计>P146 POJ3061 Subsequence Time Limit: 1000MS   M ...

最新文章

  1. 事件相机特征跟踪-EKLT方法
  2. VMware的安装教程
  3. centos7.0 安装nginx
  4. gedit乱码 fedora
  5. Silverlight 5 Beta新特性博文索引
  6. overfitting(过度拟合)的概念
  7. 静止一秒_生命静止前的那一秒,我们并不是无能为力...
  8. strtus常用的一些配置
  9. java中动态代理的使用
  10. [翻译] ios-image-filters
  11. matlab m怎么使用,MATLAB的M文件的学习和使用
  12. 老农斗杀巨型蜈蚣目击记
  13. mysql 创建表指定时区_mysql解决时区相关问题
  14. 如何定制allure报告的logo
  15. 设计水杯,门锁的测试用例
  16. VS 关于GetLastError()返回码列表及对应的解释
  17. 世界经典咖啡@配制方法
  18. .pdm文件怎么打开
  19. 防火墙对计算机有作用吗,win7自带防火墙有用吗?关闭win7系统自带的防火墙对电脑有影响吗...
  20. HANA数据自助查询

热门文章

  1. composer切换源_Composer具体安装方法
  2. 第4章 字符串和格式化输入/输出
  3. 来自爸妈的敷衍问候!| 今日最佳
  4. 人造流星这种生日礼物,你有过吗?现在国外有了
  5. st link v2引脚连接_ST-Link资料02_ST-Link固件介绍,及固件命名规则
  6. 罗马数字转换成数字java_C趣味编程百例(31)将阿拉伯数字转换为罗马数字
  7. linux内核线程socket,从Linux源码看Socket(TCP)的accept
  8. 用java求直角三角形的面积_JAVA 已知三角形的三个边判断 是否为直角三角形,如果是求面积!...
  9. row number函数_Hive排名函数ROW_NUMBER,RANK 和 DENSE_RANK的区别
  10. 计算机基础知识菜鸟教程,机器学习基础知识整理归纳