POJ 3320 Jessica's Reading Problem
2016-07-20 01:24
423 查看
题目链接:http://poj.org/problem?id=3320
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.
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.
思路:map的应用。用map记录一个数出现的次数,并且记录最后一个数初始出现的位置。这样答案初始就为该位置减去首位置加上1,此时还要更新当前区间的计数,将多余的计数还回去。然后从这个位置开始,每次将右边位置向右移动一下,接着不断右移左边位置,什么时候能移动呢?当最左边的数的计数大于1时移动,因为区间内还有该值,当前的值要不要无所谓,所以我们当然不要它。当最左边不能再右移了,就更新答案。详见代码。
附上AC代码:
Jessica's Reading Problem
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 10206 | Accepted: 3395 |
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 textbook. 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思路:map的应用。用map记录一个数出现的次数,并且记录最后一个数初始出现的位置。这样答案初始就为该位置减去首位置加上1,此时还要更新当前区间的计数,将多余的计数还回去。然后从这个位置开始,每次将右边位置向右移动一下,接着不断右移左边位置,什么时候能移动呢?当最左边的数的计数大于1时移动,因为区间内还有该值,当前的值要不要无所谓,所以我们当然不要它。当最左边不能再右移了,就更新答案。详见代码。
附上AC代码:
#include <cstdio> #include <map> #include <algorithm> //#pragma comment(linker, "/STACK:102400000, 102400000") using namespace std; const int maxn = 1000005; int num[maxn]; map<int, int> cnt; int n; int main(){ #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif while (~scanf("%d", &n)){ for (int i=0; i<n; ++i) scanf("%d", num+i); int l=0, r=0; cnt.clear(); for (int i=0; i<n; ++i){ if (cnt[num[i]] == 0) r = i; ++cnt[num[i]]; } for (int i=r; i<n; ++i) --cnt[num[i]]; int ans = r-l+1; --r; while (++r < n){ ++cnt[num[r]]; while (cnt[num[l]] > 1) --cnt[num[l]], ++l; ans = min(ans, r-l+1); } printf("%d\n", ans); } return 0; }
相关文章推荐
- android Google Map获取地理位置信息的方法
- Spark RDD API详解(一) Map和Reduce
- Python中map()函数浅析
- Android使用Google Map浅谈
- Erlang中的映射组Map详细介绍
- 浅析STL中的常用算法
- STL区间成员函数及区间算法总结
- c++中map的基本用法和嵌套用法实例分析
- 百度地图给map添加右键菜单(判断是否为marker)
- JavaScript中实现Map的示例代码
- jquery中map函数与each函数的区别实例介绍
- jquery中map函数遍历数组用法实例
- jquery与google map api结合使用 控件,监听器
- 浅谈jquery的map()和each()方法
- c++ STL容器总结之:vertor与list的应用
- Jquery中map函数的用法
- C++在成员函数中使用STL的find_if函数实例
- JavaScript中的数组遍历forEach()与map()方法以及兼容写法介绍
- JS模拟的Map类实现方法
- jquery遍历数组与筛选数组的方法