Jessica's Reading Problem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6496   Accepted: 1998

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

Source

 
尺取法求解
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map> using namespace std; #define maxn 1000005 int p,len = ;
int a[maxn];
map<int,int> ele; void solve() {
map<int,int> vis;
int s = ,sum = ,pos = ;
int ans = p; for(; ; ++s) {
while(sum < len && pos <= p) {
if(vis.find(a[pos]) == vis.end() || vis[ a[pos] ] == ) {
++sum;
}
++vis[ a[pos++] ];
} if(sum < len) break;
if(--vis[ a[s] ] == ) {
--sum;
}
ans = min(ans,pos - s);
} printf("%d\n",ans);
} int main()
{
// freopen("sw.in","r",stdin); scanf("%d",&p);
for(int i = ; i <= p; ++i) {
scanf("%d",&a[i]);
if(ele.find(a[i]) == ele.end()) {
ele[ a[i] ] = len++;
}
}
//printf("len = %d\n",len); solve();
return ;
}

POJ 3320的更多相关文章

  1. 【尺取】POJ 3320

    POJ 3320 Jessica's Reading Problem 题意:一本书P页,第i页有ai知识点,问你至少从某一处开始连续要翻多少页才能复习完所有的知识点,不能跨页翻. 思路:<挑战程 ...

  2. POJ 3320 尺取法,Hash,map标记

    1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...

  3. 尺取法 POJ 3320 Jessica's Reading Problem

    题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...

  4. A - Jessica's Reading Problem POJ - 3320 尺取

    A - Jessica's Reading Problem POJ - 3320 Jessica's a very lovely girl wooed by lots of boys. Recentl ...

  5. POJ 3320 (尺取法+Hash)

    题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...

  6. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  7. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  8. POJ 3320 Jessica's Reading Problem 尺取法

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  9. poj 3320(尺取法)

    传送门:Problem 3320 参考资料: [1]:挑战程序设计竞赛 题意: 一本书有 P 页,每页都有个知识点a[i],知识点可能重复,求包含所有知识点的最少的页数. 题解: 相关说明: 设以a[ ...

随机推荐

  1. C实现辗转相除法求两个数的最大公约数

    什么是辗转相除法? 辗转相除法(又名欧几里德算法),它主要用于求两个正整数的最大公约数.是已知的最古老的算法. 用辗转相除法求132和72的最大公约数的步骤: 132 / 72 = 1 ... 60 ...

  2. [第四版]用getaddrinfo设置tcp基本连接属性

    getaddrinfo getaddrinfo的一个重要功能, 很方便的构造struct sockaddr_in对象, 把繁琐的构造过程隐藏起来 getaddrinfo兼有gethostbyname和 ...

  3. chattr 与 lsattr 命令详解

    PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的li ...

  4. poj 2046 Gap

    题目连接 http://poj.org/problem?id=2046 Gap Description Let's play a card game called Gap. You have 28 c ...

  5. SQL Server实现数据的递归查询

    在一次项目中遇到一种需求,需要记录某产品的替换记录. 实际应用举例为:产品101被201替换,之后201又被303替换,303又被109替换:产品102被202替换,之后202又被105替换. 现在我 ...

  6. iOS学习之界面通信

    一.属性传值 在SecondViewController.h里 #import <UIKit/UIKit.h> @interface SecondViewController : UIVi ...

  7. java数据结构和算法------顺序查找

    package iYou.neugle.search; public class Sequence_search { public static int SequenceSearch(double[] ...

  8. qt QSS文件伪状态

    表 1. 伪状态列表伪状态    描述:checked    button部件被选中:disabled    部件被禁用:enabled    部件被启用:focus    部件获得焦点:hover  ...

  9. comparing-html5-mobile-ui-frameworks

    http://html5hub.com/comparing-html5-mobile-ui-frameworks/

  10. “我爱淘”冲刺阶段Scrum站立会议6

    完成任务: 对大部分的布局已经做好了布置. 计划任务: 实现数据库的链接,将页面功能完善. 遇到问题: 使用webservice对数据的提取,用数据库将书的信息展示软件中.