A - Jessica’s Reading Problem POJ - 3320

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

思路

这应该是一个非常经典的 尺取 应用 问题,一般应用尺取来维护 一个连续的区间 的问题

代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<map>
using namespace std; int ar[1000005];
map<int, int> mp;
map<int, int> kind; int main()
{
//ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
//freopen("A.txt","r",stdin);
int n;
//cin >> n;
scanf("%d", &n);
for(int i = 1; i <= n; i ++)
{
//cin >> ar[i];
scanf("%d", &ar[i]);
kind[ar[i]] = 1;
}
int k = kind.size();
int l = 0, r = 1;
for(int i = 1; i <= n; i ++)
{
mp[ar[i]] ++;
if(mp.size() == k)
{
r = i;
break;
}
} int cnt = k;
int len = r - l; while(l < r && l <= n - k)
{
while(cnt == k)
{
len = min(len, r - l); l ++;
mp[ar[l]] --;
if(mp[ar[l]] == 0)
cnt --;
}
if(r == n)
break; while(cnt < k && r < n)
{
r ++;
if(mp[ar[r]] == 0)
cnt ++;
mp[ar[r]] ++;
}
}
cout << len << endl; return 0;
}

A - Jessica's Reading Problem POJ - 3320 尺取的更多相关文章

  1. Jessica's Reading Problem POJ - 3320

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17562   Accep ...

  2. Greedy:Jessica's Reading Problem(POJ 3320)

    Jessica's Reading Problem 题目大意:Jessica期末考试临时抱佛脚想读一本书把知识点掌握,但是知识点很多,而且很多都是重复的,她想读最少的连续的页数把知识点全部掌握(知识点 ...

  3. Jessica's Reading Problem POJ - 3320(尺取法2)

    题意:n页书,然后n个数表示各个知识点ai,然后,输出最小覆盖的页数. #include<iostream> #include<cstdio> #include<set& ...

  4. POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法

    Subsequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13955   Accepted: 5896 Desc ...

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

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

  6. POJ 3320 Jessica's Reading Problem 尺取法/map

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7467   Accept ...

  7. POJ 3320 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6001   Accept ...

  8. POJ3320 Jessica's Reading Problem(尺取+map+set)

    POJ3320 Jessica's Reading Problem set用来统计所有不重复的知识点的数,map用来维护区间[s,t]上每个知识点出现的次数,此题很好的体现了map的灵活应用 #inc ...

  9. POJ 3220 Jessica's Reading Problem

    Jessica's Reading Problem Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12944   Accep ...

随机推荐

  1. [转帖]RSYNC 的核心算法

    RSYNC 的核心算法 https://coolshell.cn/articles/7425.html rsync是unix/linux下同步文件的一个高效算法,它能同步更新两处计算机的文件与目录,并 ...

  2. 数据挖掘入门系列教程(三)之scikit-learn框架基本使用(以K近邻算法为例)

    数据挖掘入门系列教程(三)之scikit-learn框架基本使用(以K近邻算法为例) 简介 scikit-learn 估计器 加载数据集 进行fit训练 设置参数 预处理 流水线 结尾 数据挖掘入门系 ...

  3. 基于osg的python三维程序开发(二)------向量

    上一篇文章展示了如何简单创建一个osg python 程序, 本篇展示了了一些基础数据结构的使用: from pyosg import * vec = osg.Vec3Array() #push ba ...

  4. 2019-分享数百个 HT 工业互联网 2D 3D 可视化应用案例分享

    继<分享数百个 HT 工业互联网 2D 3D 可视化应用案例>2018 篇,图扑软件定义 2018 为国内工业互联网可视化的元年后,2019 年里我们与各行业客户进行了更深度合作,拓展了H ...

  5. ggplot2(10) 减少重复性工作

    10.1 简介 灵活性和鲁棒性的敌人是:重复! 10.2 迭代 last_plot()用于获取最后一次绘制或修改的图形. 10.3 绘图模板 gradient_rb <- scale_colou ...

  6. Win10下如何安装和搭建appium自动化测试环境

    转:https://www.cnblogs.com/huainanhai/p/11577419.html 安装Android SDK https://www.jianshu.com/p/2acdc1d ...

  7. python基础(初识)

      Python简介 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开发一个新的脚本解 ...

  8. 大数据存储利器 - Hbase 基础图解

    由于疫情原因在家办公,导致很长一段时间没有更新内容,这次终于带来一篇干货,是一篇关于 Hbase架构原理 的分享. Hbase 作为实时存储框架在大数据业务下承担着举足轻重的地位,可以说目前绝大多数大 ...

  9. Head First设计模式——桥接模式

    桥接模式 桥接模式:不只改变你的实现,也改变你的抽象. 如果有一个电视厂家,遥控器需要升级,电视也需要修改.这种变化部分的封装就适合使用桥接模式,桥接模式通过将实现和抽象放在两个不同的类层次中而使它们 ...

  10. sleep()和wait()方法的区别

    1,sleep()声明在Thread类中,而且是静态方法: wait()声明在Object类中,而且必须由锁对象调用. 2,sleep()时间达到后恢复: wait()可以设置事件自动恢复,如果没有设 ...