POJ

Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12605   Accepted: 4306

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标记 看数组中数字出现的次数

直接看代码把
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<string.h>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<cmath>
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const double PI=acos(-1.0);
17 const double eps=0.0000000001;
18 const int N=1000000+10;
19 const int INF=0x3f3f3f3f;
20 int a[N];
21 map<int,int>mp;
22 map<int,int>mp1;
23 int main(){
24     int n;
25     while(scanf("%d",&n)!=EOF){
26         mp.clear();
27         mp1.clear();
28         for(int i=1;i<=n;i++){
29             scanf("%d",&a[i]);
30             mp[a[i]]=1;
31         }
32         int t=mp.size();
33         int ans=1e9;
34         //cout<<mp.size()<<endl;
35         int tt=0;
36         int j=1;
37         for(int i=1;i<=n;){
38             while(j<=n&&tt<t){
39                 if(mp1[a[j]]==0){
40                     tt++;
41                 }
42                 mp1[a[j]]++;
43                 j++;
44             }
45             if(tt<t)break;
46             ans=min(ans,j-i);
47             if(mp1[a[i]]==1){
48                 tt--;
49             }mp1[a[i]]--;
50             i++;
51         }
52         cout<<ans<<endl;
53     }
54 }
原文地址:https://www.cnblogs.com/Aa1039510121/p/7190739.html