Jessica's Reading Problem

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 尺取法(基础题)的更多相关文章

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

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

  2. POJ 3320 (尺取法+Hash)

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

  3. poj 3320(尺取法)

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

  4. poj 2566 Bound Found(尺取法 好题)

    Description Signals of most probably extra-terrestrial origin have been received and digitalized by ...

  5. hdu 5056(尺取法思路题)

    Boring count Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. 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 ...

  7. poj3061 poj3320 poj2566尺取法基础(一)

    poj3061 给定一个序列找出最短的子序列长度,使得其和大于等于S 那么只要用两个下标,区间和小于S时右端点向右移动,区间和大于S时左端点向右移动,在这个过程中更新Min #include < ...

  8. 【尺取法好题】POJ2566-Bound Found

    [题目大意] 给出一个整数列,求一段子序列之和最接近所给出的t.输出该段子序列之和及左右端点. [思路] ……前缀和比较神奇的想法.一般来说,我们必须要保证数列单调性,才能使用尺取法. 预处理出前i个 ...

  9. poj 2100(尺取法)

    Graveyard Design Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 6107   Accepted: 1444 ...

随机推荐

  1. Maven的学习资料收集--(二)安装m2eclipse插件

    在Eclipse中可以安装Maven插件,可以更方便的使用: 官网地址:http://www.eclipse.org/m2e/ 可以在线安装或者离线下载,之前在线安装总是失败,可能是网速的原因,找到了 ...

  2. Flash图表控件FusionCharts调整图表百分比大小

    用户可以为图表的宽度和高度设置百分比值,用来替代绝对的像素值. 以百分比的方式调整图表,首先需要更新HTML代码,如下所示: <div id="chartContainer" ...

  3. C++ int转string / string转int

    c++ 最近标准添加了stringstream类,可以非常简单的对int 和 string 进行相互的转化 //int 转 string void int2str(const int& i, ...

  4. SQL中如何避免书签查找

    1.使用聚集索引 对于聚集索引,索引的叶子页面和表的数据页面相同.因此,当读取聚集索引键列的值时,数据引擎可以读取其他列的值而不需要任何导航.例如前面的区间数据查询的操作,SQLServer通过B树结 ...

  5. ASP.NET MVC网站学习问题积累(一)

    最近工作压力比较大,不得已开始自学C#.同时网站开发业务开展迫在眉睫,只能先从ASP.NET学起.回想一下,连C#和ASP.NET的关系都没有明白,就被赶鸭子上架了...我觉得这将是我工作以来最具有戏 ...

  6. NetTime——c++实现计算机时间与网络时间的更新

    <Windows网络与通信程序设计>第二章的一个小例子,网络编程入门. #include "stdafx.h" #include <WinSock2.h> ...

  7. git入门使用摘录

    无论使用github或者gitlab,第一步都是在本地生产ssh-key,ssh-key作为客户端的身份证存放在user用户的.ssh文件夹下.如果之前没有生产过,需要用ssh-keygen命令生成. ...

  8. bzoj1189 [HNOI2007]紧急疏散

    Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空地:如果是'X',那么表示这是一面墙,如果是'D',那么表示这是一 ...

  9. geoWithin查询 多边形查询

    $geoWithin查询 形状的表示 .$box:矩形,使用 {$box:[[<x1>,<y1>],[<x2>,<y2>]]}表示 都是坐标,第一个坐标 ...

  10. jquery iCheck 插件

    1 官网:http://www.bootcss.com/p/icheck/#download 2 博客:https://www.cnblogs.com/xcsn/p/6307610.html http ...