POJ 3320 Jessica's Reading Problem
来源:互联网 发布:批判性思维工具 知乎 编辑:程序博客网 时间:2024/05/19 20:42
题目链接:http://poj.org/problem?id=3320
Jessica's Reading Problem
Memory Limit: 65536KTotal 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 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 containsP 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
51 8 8 8 1
Sample Output
2
Source
#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 LOCALfreopen("input.txt", "r", stdin);freopen("output.txt", "w", stdout);#endifwhile (~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;}
- POJ:3320 Jessica's Reading Problem
- poj 3320 Jessica's Reading Problem
- poj 3320 Jessica's Reading Problem
- POJ-3320-Jessica's Reading Problem
- POJ 3320 Jessica's Reading Problem
- poj 3320 Jessica's Reading Problem
- Jessica's Reading Problem POJ 3320
- POJ 3320 Jessica's Reading Problem
- POJ 3320 Jessica's Reading Problem
- POJ 3320 Jessica's Reading Problem
- POJ-3320 Jessica's Reading Problem
- poj 3320 Jessica's Reading Problem
- POJ 3320 Jessica's Reading Problem
- poj 3320 Jessica's Reading Problem
- POJ 3320 Jessica's Reading Problem
- poj-3320-Jessica's Reading Problem
- POJ 3320 Jessica's Reading Problem
- poj 3320 Jessica's Reading Problem
- Leetcode Rectangle Area
- 在Unity中使用ProtoBuffer进行数值表转换以及生成网络通信用的协议
- Leetcode Gas Station
- CodeForces 346A Alice and Bob
- Codeforces Round #363 (Div. 2)[B]One Bomb
- POJ 3320 Jessica's Reading Problem
- 字节对齐详解
- 应用内跳转百度,高德地图
- 【题】【矩阵】NKOJ 1901 喜欢奇数的面包师
- java基础学习(5)疯狂java讲义第4章课后习题解答源码
- Codeforces Round #363 (Div. 2) [C] Vacations
- Leetcode Sort Colors
- java 多态,向上转型,向下转型(强制转换)之间的理解(学习笔记而已,不一定全部准确)
- Python下安装Numpy,Scipy,Matlotlib