题目链接

                                                                         Jessica's Reading Problem

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17142 Accepted: 5950

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

思路:尺取法

re代码:用数组来记录显然是不行的,所以想到了map

#include<stdio.h>
#include<iostream>
#include<set>
#include<string.h>
#define min(a,b) a<b?a:b
using namespace std;
const int maxn=1e7+5;
int k[maxn],gg[maxn];
int size,p,smin;
void chiqu()
{
	int right,left,lsum,size2=0,ll;
	lsum=right=left=0;
	while(right<p){
		while(right<p&&size2<size){
			gg[k[right]]++;
			if(gg[k[right]]==1){
				size2++;
			}
			lsum++;
			right++;
		}
		smin=min(smin,lsum);
		while(size2>=size){
			gg[k[left]]--;
			if(gg[k[left]]==0){
				size2--;
			}
			left++;
			lsum--;
		}
		smin=min(smin,lsum+1);
	}
	return;
}
int main()
{
	set<int>st;
	smin=9999;
	while(scanf("%d",&p)!=EOF){
		for(int i=0;i<p;i++){
			scanf("%d",&k[i]);
			st.insert(k[i]);
		}
		size=st.size();
		chiqu();
		printf("%d\n",smin);
		st.clear();	
		memset(gg,0,sizeof(gg));	
	}
}

ac代码:

#include<stdio.h>
#include<iostream>
#include<set>
#include<string.h>
#include<map>
#define min(a,b) a<b?a:b
using namespace std;
const int maxn=1e6+5;
int k[maxn];
int size,p,smin;
map<int,int>mp;
void chiqu()
{
	int right,left,lsum,size2=0;
	lsum=right=left=0;
	while(right<p&&left<p){
		while(right<p&&size2<size){//满足条件right右移动 
			if(mp[k[right]]==0){//为零说明首次出现+1 
				size2++;
			}
			mp[k[right]]++;
			right++;
			lsum++;
		}
		while(size2>=size){//满足条件left右移动 
			mp[k[left]]--;
			if(mp[k[left]]==0){//为零说明没有这个元素-1 
				size2--;
			}
			left++;
			lsum--;
		}
		smin=min(smin,lsum+1);
	}
}
int main()
{
	set<int>st;
	while(scanf("%d",&p)!=EOF){
		smin=p;//记得smin初始化,因此wa了很多次TAT 
		for(int i=0;i<p;i++){
			scanf("%d",&k[i]);
			st.insert(k[i]);
		}
		size=st.size();
		chiqu();
		printf("%d\n",smin);
		st.clear();	
		mp.clear();	
	}
}

 

更多推荐

poj3320-Jessica's Reading Problem