POJ 3320
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的更多相关文章
- 【尺取】POJ 3320
POJ 3320 Jessica's Reading Problem 题意:一本书P页,第i页有ai知识点,问你至少从某一处开始连续要翻多少页才能复习完所有的知识点,不能跨页翻. 思路:<挑战程 ...
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- 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 ...
- POJ 3320 (尺取法+Hash)
题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针
地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展 若满足要求则从左缩减区域 代码如下 正确性调整了几次 然后 ...
- 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 ...
- poj 3320(尺取法)
传送门:Problem 3320 参考资料: [1]:挑战程序设计竞赛 题意: 一本书有 P 页,每页都有个知识点a[i],知识点可能重复,求包含所有知识点的最少的页数. 题解: 相关说明: 设以a[ ...
随机推荐
- 《大话设计模式》ruby版代码:简单工厂模式
之前有看过<ruby设计模式>,不过渐渐的都忘记了.现在买了一个大话设计模式,看起来不是那么枯燥,顺便将代码用ruby实现了一下. # -*- encoding: utf-8 -*- #运 ...
- 菜鸟学习Spring——60s利用JoinPoint获取参数的值和方法名称
一.概述 AOP的实现方法在上两篇博客中已经用了两种方法来实现现在的问题来了虽然我们利用AOP,那么客户端如何信息传递?利用JoinPoint接口来实现客户端给具体实现类的传递参数. 二.代码演示. ...
- Mysql允许外网接入
首先你可以为mysql创建一个账户,或者为root用户接入数据库. 授权用户指定所有主机以指定用户连接服务器 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDE ...
- Run ionic web app in nodejs
首先需要express插件:sudo npm install express 将ionic project的www拷贝至wwwroot,新建server.js: var express = requi ...
- SQL Server 2008 R2 主从数据库同步
一.准备工作: 主数据库服务器: OS:Windows Server 2008 R2 DB: SQL Server 2008 R2 Hostname : CXMasterDB IP: 192.1 ...
- iOS学习之C语言函数指针
通过函数名调用函数: int max = maxValue(4, 5); printf("max = %d\n", max); 函数类型:int (int, int) 1. ...
- UIBezierPath 的使用介绍
使用UIBezierPath类可以创建基于矢量的路径.此类是Core Graphics框架关于path的一个封装.使用此类可以定义简单的形状,如椭圆或者矩形,或者有多个直线和曲线段组成的形状 ...
- ios coreData使用
ios中的coredata的使用(转) 分类: ios2013-07-15 18:12 27288人阅读 评论(1) 收藏 举报 Core Data数据持久化是对SQLite的一个升级,它是ios集成 ...
- Matlab 支持向量机(SVM)实现多分类
1.首先,你需要安装完成Matlab. 2.将libsvm-3.17.zip和drtoolbox.tar文件解压到:libsvm-3.17文件夹和drtoolbox,并放到MATLAB的工具箱安装目录 ...
- java提供了native2ascii工具
可以使用这个工具,把中文编码称为ascii码 在命令行输入native2ascii 输入中文 得到数据