码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2017-05-22 16:34:30      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:more   map   ext   ring   8 8   int   index   recent   sub   

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

首先,使用set求出不同”数字“的个数。之后采用尺取法的想法,对第几页进行尺取即可。

 1 #include <iostream>
 2 #include <string>
 3 #include <algorithm>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <queue>
 8 #include <set>
 9 #include <map>
10 #include <list>
11 #include <vector>
12 #include <stack>
13 #define mp make_pair
14 #define MIN(a,b) (a>b?b:a)
15 //#define MAX(a,b) (a>b?a:b)
16 typedef long long ll;
17 typedef unsigned long long ull;
18 const int MAX=1e5+5;
19 const int INF=1e9+5;
20 const double M=4e18;
21 using namespace std;
22 const int MOD=1e9+7;
23 typedef pair<int,int> pii;
24 const double eps=0.000000001;
25 int p,n;
26 int a[MAX];
27 map <int,int> num;
28 set <int> re;
29 int st,en;
30 int cnt,an;
31 int main()
32 {
33     scanf("%d",&p);
34     for(int i=1;i<=p;i++)
35     {
36         scanf("%d",&a[i]);
37         re.insert(a[i]);
38     }
39     n=re.size();
40     st=en=1;
41     an=INF;
42     for(;;)
43     {
44         while(en<=p&&cnt<n)
45         {
46             if(num[a[en++]]++==0)
47                 ++cnt;
48         }
49         if(cnt<n)
50             break;
51         else
52         {
53             an=min(an,en-st);
54             if(!(--num[a[st++]]))
55                 --cnt;
56         }
57     }
58     printf("%d\n",an);
59     return 0;
60 }

 

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

标签:more   map   ext   ring   8 8   int   index   recent   sub   

原文地址:http://www.cnblogs.com/quintessence/p/6889641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!