POJ:3320-Jessica's Reading Problem(尺取法)
Jessica’s Reading Problem
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 15224 Accepted: 5241
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
解题心得:
- 就是给你n个可能重复的数,要你选择选择一段最小的区间要包含所有的数,问这个最小的区间有多长。
- 其实就是一个尺取法啊,考验了一点点技巧,关于这段区间内有多少个数,可以使用map映射,在map中从0变1数量+1,从1变0数量-1,这个数列一共有多少个数可以使用set来区分。
#include <algorithm>
#include <stdio.h>
#include <map>
#include <set>
using namespace std;
const int maxn = 1e6+100;
int num[maxn],n;
set <int> se;
map <int,int> maps;
void init() {
scanf("%d",&n);
for(int i=0;i<n;i++) {
scanf("%d",&num[i]);
se.insert(num[i]);
}
}
int main() {
init();
int sum = se.size();
int cnt,l,r,ans = maxn;
l = r = cnt = 0;
while(1) {
while(cnt < sum && r < n) {
if(maps[num[r]] == 0)
cnt++;
maps[num[r]]++;
r++;
}
if(cnt < sum)
break;
ans = min(ans,r-l);
maps[num[l]]--;
if(maps[num[l]] == 0)
cnt--;
l++;
}
printf("%d\n",ans);
}
POJ:3320-Jessica's Reading Problem(尺取法)的更多相关文章
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- 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 ...
- poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用
jessica's Reading PJroblem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9134 Accep ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3320 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- 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 ...
- 题解报告: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 ...
- poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)
这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...
随机推荐
- mybatis springmvc批量删除 2最新
service层: @Override public void batchDeletes(List list) { creditDao.batchDeletes(list); } 控制层control ...
- (11)JavaScript之[DOM HTML][DOM CSS]
DOM HTML //改变HTML输出流 document.write(Date()); //改变HTML的内容 document.getElementById('box').innerHTML = ...
- redis笔记(三)
redis配置文件 配置文件对单位大小写不敏感 tcp-backlog 511 高并发环境连接数 tcp-keepalive 单位为秒 0 表示不会进行keepalive检测,,,建议设置 ...
- JavaScript 面向对象编程(二):继承
Javascript面向对象编程(二):构造函数的继承 这个系列的第一部分,主要介绍了如何"封装"数据和方法,以及如何从原型对象生成实例. 今天要介绍的是,对象之间的"继 ...
- 使用坚果云+keepass实现网盘密码管理
准备工作 登录坚果云web版 在主页创建了一个新的同步文件夹AAA(与同步文件夹My Nutstore并列) 设置同步文件夹AAA:勾选“默认不同步到电脑” 把KeePass的数据库文件db.kdbx ...
- centos 7(1611)安装笔记
麻烦 前天我把双系统笔记本里的 deepin 的磁盘分区直接从 Windows 7 磁盘管理里格式化了,结果悲催了,开不了机了,显示: 我以为是 Windows 7 的引导没了,就进 PE 修复了 ...
- phpStudy:使用localhost无法访问的解决方案
今天遇到新的问题,很有意思,当使用Localhost时,发现报403错误: 百度找到问题所在:没有权限问题 所以我们打开phpStudy,找到配置文件“vhosts-conf”,看到的情况是这样的 接 ...
- 使用ABAP代码创建S/4HANA里的Sales Order
下图是使用ABAP代码创建的S/4HANA的Sales Order的截图: 其中红色区域的值是我代码里硬编码的,而蓝色是函数SD_SALESDOCUMENT_CREATE自己创建的. 来看下代码: D ...
- IOS笔记 : 一些小技巧
计算单元格高度,在自定义cell中 -(void) resizeTheHeight{ CGFloat contentWidth = 280; UIFont *font = [UIFont fontWi ...
- 温故而知新:Asp.Net中如何正确使用Session
原文链接作者:菩提树下的杨过出处:http://yjmyzz.cnblogs.com Asp.Net中的Session要比Asp中的Session灵活和强大很多,同时也复杂很多:看到有一些Asp.Ne ...