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
Source
POJ Monthly--2007.08.05, Jerry
题目类型:追逐法
算法分析:动态维护两个指针p1和p2,然后判断指针之间的区间内部的元素个数是否已经到达总的元素种数,最后直接使用追逐法的思路解决即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 7; const int maxn = 1660000; int ans[maxn]; int main() { // ifstream cin ("aaa.txt"); // freopen ("aaa.txt", "r", stdin); int n; while (scanf ("%d", &n) != EOF) { int i; set <int> s; for (i = 1; i <= n; i++) { scanf ("%d", &ans[i]); if (!s.count (ans[i])) s.insert (ans[i]); } int tot = s.size (); int p1 = 1, p2 = 1, minval = n, sum = 0; map <int, int> ma; while (1) { while (sum < tot && p2 <= n) { if (ma[ans[p2]] == 0) sum++; ma[ans[p2]]++; p2++; } if (sum < tot) break; minval = min (minval, p2 - p1); if (ma[ans[p1]] == 1) sum--; ma[ans[p1]]--; p1++; } printf ("%d\n", minval); } return 0; } |
- « 上一篇:poj3268
- poj3468:下一篇 »