Jessica's Reading Problem POJ - 3320

14 篇文章 0 订阅
1 篇文章 0 订阅

Jessica's Reading Problem

 POJ - 3320 

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,

接着给定 一个N个数字,要求找出最短的包含所有数字的连续子序列长度

思路:

迟取法

设置两个哨兵,s和t,和一张哈希表

首先计数不同数字的个数,作为是否包含全部数字的总依据。

然后在通过循环,t不断向前寻找,若原先的集合里没有这个数字,则添加进来,若包含了全部的数字种类,则计算一次长度

接着,s也向前寻找,因为子序列是[s,t),所以相当于s在迭代的时候,丢弃前一个,剔除对应的集合数字,在下一轮迭代的时候,再计算t和s 的差值,直至不再包含所有种类数字。

#include<iostream>
#include<map>
#include<set>
#include<stdio.h>
const int MAXN = 1e6+5;
using namespace std;

int pages[MAXN];
int main(){
	set<int> all;
	map<int,int> map_;
	
	int N;
	scanf("%d",&N);
	
	int count=0;
	for(int i=0;i<N;i++){
		scanf("%d",&pages[i]);
		all.insert(pages[i]);
	}
	
	count = all.size();
	int len =N;
	int num = count;
	
	int s=0,t=0;
	count =0;
	while(true){
		while(count<num&&t<N){
			if(map_[pages[t]]==0){
				count++;
			}
			map_[pages[t]]++;
			t++;
		}
		if(count<num) break;
		len = min(len,t-s);
		int temp =map_[pages[s]]-1;
		map_[pages[s]]--;
		if(temp==0){
			count--;
		}
		s++;
	}

	printf("%d\n",len);

	return 0;
} 

thick

n. 最拥挤部分;活动最多部分;事物的粗大浓密部分

adj. 厚的;浓的;粗大的

adv. 密集地;浓浓地,厚厚地

n. (Thick)人名;(英)西克

fussy

adj. 爱挑剔的,难取悦的;易烦恼的

n. (Fussy)人名;(法)菲西

contiguous 

adj. 连续的;邻近的;接触的 

courtship

n. 求爱;求婚;求爱期

convenience

n. 便利;厕所;便利的事物

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值