Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6897 | Accepted: 2144 |
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
题意:
给出 P(1 ~ 1000000),后给出 P 个数,输出最小的长度序列,这个长度序列的数包含整个序列的所有不同的数。
思路:
尺取法。先统计数的种类 n,然后用两个变量分别指向开头和结尾(一开始都为开头),结尾变量遍历过程中如果这个数是第一次出现的(用 map 映射判断是不是等于 0 )就增加这一页,同时用 num 统计遍历过程中的种类,当 num == n,说明这个长度内的数已经能访问所有的数种,这时候开始移动头变量,如果删除这个数后剩下这个数种的数量仍然大于 0,说明没有影响,当删除到这一个数出现数种 == 0 的时候再移动尾变量。不断循环这个过程就能找到一个最小的长度。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <set> #include <map> using namespace std; const int MAXN = 1000005; int num[MAXN]; set<int> ans; map<int, int> cnt; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &num[i]); ans.insert(num[i]); } int sum = ans.size(); int s = 0, t = 0, now = 0; int Min_len = n; for(;;) { while (t < n && now < sum) { if (cnt[ num[t++] ]++ == 0) { ++now; } } if (now < sum) break; Min_len = min(Min_len, t - s); if (--cnt[ num[s++] ] == 0) { --now; } } printf("%d\n", Min_len); return 0; }
相关推荐
CV of jessica pointing from Stanford and Harvard Uni.
哈佛大学经典 概率论教材,概率论导论《Introduction to Probability》 by. Joseph K. Blitzstein and Jessica Hwang,深入浅出,难得的一本好书。
在IT行业中,HTML(HyperText Markup Language)是一种用于创建网页的标准标记语言,是构建...通过学习和实践,你可以创建出富有吸引力、功能丰富的网页,并可能在"Jessica"这个学习资源中找到进一步的指导和支持。
"Jessica_winform_"项目可能是一个示例工程,展示了如何在WinForm应用中实现动态效果,并允许用户通过鼠标进行交互。 首先,让我们理解WinForm。WinForm是.NET Framework提供的一种用于创建Windows桌面应用程序的...
C语言初学者练习_ Jessica
【Jessica.rar】是一个压缩包文件,其内容是一个名为"Jessica"的营养师网站模板。这个模板基于现代网页设计技术HTML5构建,旨在为营养师或健康顾问提供一个专业且吸引人的在线平台。HTML5是超文本标记语言的第五次...
Jessica-Jessie(安全分布式Javascript)编译器体系结构 注意:尚未准备好生产。 杰西卡(Jessica)现在仍然处于引导状态,但是欢迎您来贡献和讨论! 该目录包含Jessica,这是实现的编译器体系结构。 简而言之,...
Identify and solve problems with your app’s UI to deliver a better user experience Start getting to grips with the new UI features coming up in Android N, including multi-window mode and direct ...
Blitzstein和Jessica Hwang所著的书籍《Introduction to Probability》是一本哈佛大学出版社出版的概率论入门教材。这本书在概率论的教学领域享有盛誉,它以细致而透彻的方式讲解了概率论的基础知识,同时辅以大量...
杰西卡·特伦特(Jessica Trent)的继承 这是Evelyn Raymond所著《杰西卡·特伦特的继承》一书的源文件的git存储库。 本书是美国的公共领域。有关详细信息,请参阅LICENSE文件。 发现错误? 如果您在GITenberg中的...
《Jessica:C语言入门练习》 在编程世界里,C语言是一种基础且强大的编程语言,它以其简洁、高效和灵活性著称。本资源“Jessica-c语言入门练习”旨在帮助初学者Jessica,以及所有对C语言感兴趣的朋友们,踏上学习的...
Jessica and the wolf: A story for children who have bad dreams. New York: Magination Press, 32 pp., [dollar]15.95 (cloth); [dollar]5.95 (paper). Cain, B. S. (1990). Double-dip feelings: A book to ...
《CSS在网页设计中的应用与重要性——以“jessica-cv”为例》 “jessica-cv”是一个以CSS技术为核心的个人简历网页项目,它充分展示了CSS在网页设计中的强大功能和灵活性。CSS,全称为层叠样式表(Cascading Style ...
《Python在商业知识图谱构建中的应用——以"jessica_business_kg"为例》 在信息化时代,商业知识图谱已经成为企业管理和决策的重要工具,它能够整合、关联和理解大量的商业数据,为企业提供深入洞察和智能建议。...
将新标签页替换为“杰西卡·阿尔巴(Jessica Alba)”自定义页面,其中包含书签,应用,游戏和杰西卡·阿尔巴(Jessica Alba)壁纸。 杰西卡·奥尔芭(Jessica Alba) 安装它以获得最大的浏览体验。 免费打开有趣的...
Image semantic segmentation is more and more being of ... Firstly, we formulate the semantic segmentation problem and define the terminology of this field as well as interesting background concepts
初级入门吉他谱 guitar tab