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++)以后,尾指针 ...
随机推荐
- apache安装 windows
进入cmd cd apache目录 httppd.exe ?显示全部命令 httppd.exe -k install 安装apache httppd.exe -k start 启动 检测是否运行 浏览 ...
- gulp 无损压缩图片
在做项目中,美工有时候会给一些比较大的图片,在做网站的时候,图片太大会影响加载速度.因此,我们需要无损压缩图片. 在尝试过几个压缩图片的方法,发现gulp中的gulp-tinypng-nokey插件是 ...
- js-对象的方法详解
Object.prototype 上的方法: constructor 返回创建该对象的构造函数 var arr = []; arr.constructor == function Array() { ...
- [RabbitMQ]Windows环境下rabbitmqclt(Command Line Tools)出现Erlang distribution failed错误的解决方法
摘要 当使用rabbitmqctl时出现Erlang distribution failed,把%SystemRoot%Windows\System32\config\systemprofile下的. ...
- Android 使用xml实现边框阴影,背景渐变效果(附有RGB颜色查询对照表)
上图是显示效果,下面是代码实现: 个人理解就是使用layer-list实现两层view的叠加,其中top,left,bottom,left控制阴影 <?xml version="1.0 ...
- 建立本地yum源
使用环境 服务器处于内网,需要更新 网络资源紧张,节约带宽 建立yum目录 mkdir -p /opt/opmgmt/yum rsync服务器列表 centos mirrors epel mirror ...
- IDEA下通过Git实现代码管理
IDEA下通过Git实现代码管理 1.介绍 1.1 Git概述 Git是类似于SVN等代码管理软件,使用分布式技术实现.Github是互联网代码仓库,每个人可以在上面创建自己的仓库,使用git完成同g ...
- php中的curl_multi的应用(php多进程)
相信许多人对PHP手册中语焉不详的curl_multi一族的函数头疼不已,它们文档少,给的例子 更是简单的让你无从借鉴,我也曾经找了许多网页,都没见一个完整的应用例子. curl_multi_add_ ...
- COGS 2091. Asm.Def的打击序列
★★★ 输入文件:asm_lis.in 输出文件:asm_lis.out 简单对比时间限制:4 s 内存限制:256 MB [题目描述] 白色圆柱形的“蓝翔”号在虚空中逐渐变大,一声沉 ...
- SQL语句关于时间的查询小心得,希望大家给点意见
完全使用时间函数去搞定查询日期,之前写的可能有些问题,现在删了修正一下 本月记录: SELECT * FROM 表 WHERE datediff(month,[dateadd],getdate())= ...