POJ 3320 尺取法(基础题)

时间:2022-09-23 20:15:44
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 &lpar;尺取法&plus;Hash&rpar;

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

  3. poj 3320&lpar;尺取法&rpar;

    传送门: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&lpar;尺取法思路题&rpar;

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

  6. A - Jessica&&num;39&semi;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&lpar;尺取法&rpar;

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

随机推荐

  1. python 库安装

    用到再更新. #Windows 一 exe 安装包 http://www.lfd.uci.edu/~gohlke/pythonlibs/#scikit-learn 二 setup.py cmd 进入目 ...

  2. freefilesync 7 使用

    官方下载地址:http://www.freefilesync.org/download.php 1.打开FreeFileSync 设置左右的文件夹,设置过滤规则,设置同步方式(双向.单向),执行同步 ...

  3. (转)在Repeater中嵌套使用Repeater

    在一般的网站中浏览类别的用户控件通常都位于大多数 ASP.NET 页的左边,它使用户能够按类别快速的查找产品.最近遇到一个客户,因为在他网站上展示的产品并不多,所以要求在原有类别浏览的基础上将产品也加 ...

  4. php 父类子类构造函数注意事项

    网上流传的2点: PHP的构造函数继承必须满足以下条件: 当父类有构造函数的声明时,子类也必须有声明,否则会出错. 在执行父类的构造函数时,必须在子类中引用parent关键字. 第1点不需要. 第二个 ...

  5. 【转】 要做linux运维工程师的朋友,必须要掌握以下几个工具才行

          本人是linux运维工程师,对这方面有点心得,现在我说说要掌握哪方面的工具吧 说到工具,在行外可以说是技能,在行内我们一般称为工具,就是运维必须要掌握的工具. 我就大概列出这几方面,这样入 ...

  6. 【c&num;】RabbitMQ学习文档(七)C&num; API

    今天这篇博文是我翻译的RabbitMQ的最后一篇文章了,介绍一下RabbitMQ的C#开发的接口.好了,言归正传吧. Net/C# 客户端 API简介 主要的命名空间,接口和类 定义核心的API的接口 ...

  7. bootstrap知识笔记

    .nav>.active>a{ background-color:#0088cc; color:#fff; } /*! * Bootstrap v3.3.7 (http://getboot ...

  8. Python—合并两个有序列表

    def hb(list1,list2): result = [] while list1 and list2: ] < list2[]: result.append(list1[]) del l ...

  9. 关于vs设置其他主题配色问题

    可以再网上找其他的.vssettings文件导入 导入方法如下: 一般vs会将你之前设置下的配色方案自动保存下来,你也可以直接覆盖 2019-03-21  17:31:07

  10. NumPy与ndarray简介(转)

    http://blog.csdn.net/u014374284/article/details/45420645 一.NumPy简介 NumPy的全名为Numeric Python,是一个开源的Pyt ...