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++)以后,尾指针 ...
随机推荐
- Android GreenDao清空数据库的方法
最近在做项目的时候,为了方便测试人员测试,在应用中加入正式库和测试库切换的功能.为了防止正式库和测试库切换带来的数据冲突,切换的时候必须把当前的数据库清空.代码如下: package com.exam ...
- spring mvc踩坑记
前言 主要介绍自己在学习spring mvc过程中踩到的一些坑,涉及到当时遇到这个错误是如何思考的,对思路进行总结,下次遇到类似的错误能够提供一些思路甚至快速解决. 环境准备 jdk8,spring4 ...
- django choice字段模板展示
class UserInfo(AbstractUserInfo): """ 用户表 """ gender_choice = ( (1,&qu ...
- Excel操作之VLOOKUP函数
1.作用 VLOOKUP函数是Excel中的一个纵向查找函数,它与LOOKUP函数和HLOOKUP函数属于一类函数,在工作中都有广泛应用,例如可以用来核对数据,多个表格之间快速导入数据等函数功能.功能 ...
- MYSQL导入excel
MYSQL使用navicat导入excel 第一步:首先需要准备好有数据的excel 第二步:选择"文件"->"另存为",保存为"CSV(逗号分 ...
- php:定义时间跳转到指定页面
我们想要定义延迟时间,再跳转到指定页面,只要用header()即可,语法: header("Refresh:延迟时间;url=要跳转的页面"); 例子: 注意注意:我们在heade ...
- word文档快速转换为PPT演示文稿
方法一: 访问http://t.im/pdftoppt,点击继续浏览(会跳转至:https://smallpdf.com/cn/pdf-to-ppt): 打开word文档,设置为“横向”,输出为PDF ...
- MySQL入门很简单: 6 视图
1. 视图含义作用 视图是虚拟的表,是从数据率中一个或多个表中导出来的表: 数据库中只存放了视图的定义,没有存放视图中的数据,数据在原先的表中: 一旦表中的数据发生变化,显示在视图中的数据也会发生 ...
- vue中多个元素或多个组件之间的动画效果
多个元素的过渡 <style> .v-enter,.v-leave-to{ opacity: 0; } .v-enter-acitve,.v-leave-active{ opacity: ...
- CentOS下用rinetd做端口转发
windows下的端口转发一般用的是自带的nat和porttunnel.portmap linux下端口转发映射的程序叫rinetd,启动方法rinetd -c /etc/rinetd.conf , ...