Jessica's Reading Problem POJ - 3320 (尺取法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SEVENY_/article/details/83242657

Jessica's Reading Problem

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                                                                                                                                                                                                                                                        

题意:

现在要把课本上每个知识点看一遍。这本书一共有P页,第i页恰好有一个知识点ai(每个知识点都有一个编号),同一个知识点可能被多页反复提到。所以她想阅读连续的一些页把书本中所有的知识点全覆盖到。给定每页写到的知识点,求出阅读的最少页数。

我们假设从某一页L开始到R页能覆盖所有知识点,这样的话从L+1开始,必须阅读到R'(R'>=R)为止。可以用尺取法解决问题。

所有知识点被覆盖 ==每个知识点出现的次数大于等于1。则我们在尺取法过程中记录下每个知识点出现的次数,从区间头部把L取出时,则L对应的知识点出现的次数减1,如果此时某个知识点出现的次数为0时,则将尾部R向后推进,直到再次覆盖住所有知识点,不断更新每次R-L的最小值。 

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<cmath>
#include<cstring>
#include<map>
using namespace std;
int a[1000010]; 
int main()
{
	int P,count;
	while(scanf("%d",&P)!=EOF)
	{
		set<int>s;  //存放不同的知识点
		map<int,int>vis;  //每个知识点出现的个数
		for(int i=0;i<P;++i)
		{
			scanf("%d",&a[i]);
			s.insert(a[i]);
		}
		count=s.size();//记录知识点的总个数 
		int l=0,r=0,num=0,minx=P;  //l代表尺子的左边界,r代表右边界,num为知识点的个数,minx为阅读的最小值
		while(true)
		{
			while(r<P&&num<count)
			{
				if(vis[a[r]]==0)//vis[]为0说明这个知识点没有看到过,就更新看到的知识点个数
					num++;
				vis[a[r]]++; //更新知识点出现的次数
				r++;
			}
			if(num<count) 
				break;
			minx=min(minx,r-l); //更新阅读的最小值
			vis[a[l]]--;//左边界右移
			if(vis[a[l]]==0)//有一个知识点出现的次数为0了,就是减少了一个知识点 
				num--;
			l++;
		}
		printf("%d\n",minx);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/SEVENY_/article/details/83242657