POJ 3320 尺取法(基础题)
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 "cstdio"
#include "iostream"
#include "algorithm"
#include "string"
#include "cstring"
#include "queue"
#include "cmath"
#include "vector"
#include "map"
#include "stdlib.h"
#include "set"
#define mj
#define db double
#define ll long long
using namespace std;
const int N=1e6+;
const int mod=1e9+;
const ll inf=1e16+;
int a[N];
map<int ,int > u,v;
int main()
{
int n;
scanf("%d",&n);
int cnt=;
for(int i=;i<n;i++){
scanf("%d",a+i);
if(!u[a[i]]) cnt++;
u[a[i]]++;
}
int l=,r=,ans=,res=n;
for(;;)
{
while(r<n&&ans<cnt){
if(v[a[r++]]++==) ans++;
}
if(ans<cnt) break;
res=min(res,r-l);
if(--v[a[l++]]==) ans--;
}
printf("%d\n",res);
}
POJ 3320 尺取法(基础题)的更多相关文章
- POJ 3320 尺取法,Hash,map标记
1.POJ 3320 2.链接:http://poj.org/problem?id=3320 3.总结:尺取法,Hash,map标记 看书复习,p页书,一页有一个知识点,连续看求最少多少页看完所有知识 ...
- POJ 3320 (尺取法+Hash)
题目链接: http://poj.org/problem?id=3320 题目大意:一本书有P页,每页有个知识点,知识点可以重复.问至少连续读几页,使得覆盖全部知识点. 解题思路: 知识点是有重复的, ...
- poj 3320(尺取法)
传送门:Problem 3320 参考资料: [1]:挑战程序设计竞赛 题意: 一本书有 P 页,每页都有个知识点a[i],知识点可能重复,求包含所有知识点的最少的页数. 题解: 相关说明: 设以a[ ...
- poj 2566 Bound Found(尺取法 好题)
Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...
- hdu 5056(尺取法思路题)
Boring count Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- 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 ...
- poj3061 poj3320 poj2566尺取法基础(一)
poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...
- 【尺取法好题】POJ2566-Bound Found
[题目大意] 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. [思路] ……前缀和比较神奇的想法.一般来说,我们必须要保证数列单调性,才能使用尺取法. 预处理出前i个 ...
- poj 2100(尺取法)
Graveyard Design Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 6107 Accepted: 1444 ...
随机推荐
- Oracle快速插入数据append
1:用merge into 进行匹配更新和插入,2: 开启并行,提高速度3: 如果可以的话加NOLOGING 不写入日志4:如果数据有规律的话,分批次执行 1.使用marge快速插入; MERGE / ...
- 3年,阅读量100万+, Github Star 15000+
这两天突然发现,三年前在博客园写的一篇文章阅读量超过百万了,对,还是技术文章.这个让我蛮惊讶的,当时刚开始写这篇文章的时候,一周的阅读量也才两三千,随着时间慢慢的过去,在搜索引擎的加持下竟然超过了百万 ...
- Docker | 第七章:Docker Compose服务编排介绍及使用
前言 前面章节,我们学习了如何构建自己的镜像文件,如何保存自己的镜像文件.大多都是一个镜像启动.当一个系统需要多个子系统进行配合时,若每个子系统也就是镜像需要一个个手动启动和停止的话,那估计实施人员也 ...
- SpringBoot | 第十三章:测试相关(单元测试、性能测试)
前言 前面写了这么多章节,都是通过浏览器访问的形式,进行接口方法访问进而验证方法的正确与否.显然在服务或者接口比较少时,这么做没有啥问题,但一旦一个项目稍微复杂或者接口方法比较多时,这么验证就有点不符 ...
- [COGS 347]地震
时间限制:4 s 内存限制:128 MB 问题描述 某国地形狭长,中部有一列山脉,由于多发地震,山脉在不断变化中.地震发生时,山脉有可能发生如下变化:局部海拔升高或降低,板块运动产生地裂而出现一段 ...
- Android RecycleView实现混合Item布局
首先来看看效果吧: 效果预览.png 本实例来自于慕课网的视屏http://www.imooc.com/video/13046,实现步骤可以自己去观看视屏,这里只记录了下实现的代码. 添加依赖: (1 ...
- Django Form 表单
Form 表单功能 生成HTML表单元素检查表单元素的合法性验证如果错误,重复显示表单数据类型转换 Form相关的对象 Widget 渲染成HTML元素的工具Field Form对象中的一个字段For ...
- php之cURL惯用
1.php cURL的强大:PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器.使用各种协议.libcurl 目前支持的协议有 http.https.ft ...
- 【洛谷2152】[SDOI2009] SuperGCD(Python好题)
点此看题面 大致题意: 给你两个长度\(\le10000\)的正整数,让你求它们的\(gcd\). Python 高精请绕道. 这题的正解应该是Python. 对于这种高精题,肯定是Python最方 ...
- mongodb索引 全文索引
全文索引,也叫文本索引,平时,我们百度的搜索,比如api文档的搜索,这种全局的索引就可以使用全文索引实现 全文索引:对字符串与字符串数组创建全文可搜索对索引 使用情况:比如有一个数据集合,存储了用户的 ...