【POJ - 3320 】Jessica's Reading Problem (尺取,哈希)

版权声明:欢迎学习我的博客,希望ACM的发展越来越好~ https://blog.csdn.net/qq_41289920/article/details/83385879

题干:

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个章节,但是有的章节是在讲相同的内容,现在每个内容用数字代替,问你最少需要读多少章节才可以把所有的内容都学到手?

解题报告:

    一看就是尺取嘛,,然后cur==cnt之后,别忘看看l还可不可以往右移动就好了。

AC代码:

#include<cstdio>
#include<map>
#include<iostream>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e6 + 5;
int a[MAX];
int cnt,cur,ans;
map<int , int> mp;
int main()
{
	int n;
	while(~scanf("%d",&n)) {
		cnt = cur = 0;
		ans = 0x3f3f3f3f;
		mp.clear();
		for(int i = 1; i<=n; i++) {
			scanf("%d",a+i);
			if(mp.find(a[i]) == mp.end()) mp[a[i]]=1,cnt++;			
		} 
		int l = 1,r = 1;
		mp.clear();
		while(r <= n) {
			if(mp[a[r]] == 0) cur++;
			mp[a[r]]++;

			if(cur >= cnt) {
				while(cur > cnt) {
					mp[a[l]]--;
					if(mp[a[l]] == 0) cur--;
					l++;
				}
				while(mp[a[l]] >= 2) {
					mp[a[l]]--;
					l++;	
				}				
				ans = min(ans,r-l+1); 
			}
			r++;
		}
		printf("%d\n",ans);
	} 
	return 0 ;
 }
/*
4
3 3 2 1
4
2 3 2 1
*/

总结:

1.和这题一样【HDU - 5672】String(尺取法),都是要注意一种样例:

4

3 3 2 1

和:

4

3 2 3 1

。如果找到cur==cnt就更新答案了,那就错了,因为左边说不定还是可以缩减的。但是我给的这两个样例是两种类型的!!

如果仅仅针对第一个样例可以这样        while(a[l] == a[l+1]) l++;    

但是对第二个又过不了,因为这几个3不一定连续!!

2.这题数据量1e6,用map的话是516msAC的,有点慢,这题可以用hash数组

猜你喜欢

转载自blog.csdn.net/qq_41289920/article/details/83385879