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. OOM when allocating tensor of shape [] and type float [[node conv2d_224/kernel/Initializer/random_uniform/min (defined at ./intances/utils.py:19) ]]

    当你们在用模型加载,tensorflow-gpu预测的时候,有没有出现这样的错误?? 经过网上的资料查阅,更多的解释是GPU的显存不足造成的,于是乎,我查看了一下GPU显存:nvidia-smi 不看 ...

  2. 【原创】(四)Linux进程调度-组调度及带宽控制

    背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...

  3. plsql乱码问题

    1,问题:在plsql 中执行sql语句,查询结果带有中文,出现乱码,即" ??? ":如下: 2,解决: 1)输入sql语句 select * from V$NLS_PARAME ...

  4. Python模块二

    os模块是与操作系统交互的一个接口​ <em>#和文件夹相关 os.makedirs('dirname1/dirname2')    可生成多层递归目录 os.removedirs('di ...

  5. centos-Linux静态IP地址配置

    首先在VMware菜单中点击编辑-->虚拟网卡编辑器,查看NAT网段(子网掩码.网关.起止IP地址) 1.用nmcli命令配置IP地址 [root@Core ~]# nmcli connecti ...

  6. git回滚到任意一个版本

    1.首先查找提交的记录(-3表示显示最近的3条) git log -3 2.强制回滚到制定版本 git reset --hard 制定版本commitId 如:git reset --hard 4ba ...

  7. nodejs中的并发编程

    从sleep的实现说起 在nodejs中,如果要实现sleep的功能主要是通过"setTimeout + promise"实现,也可以通过"循环空转"来解决.前 ...

  8. 作为一位Vue工程师,这些开发技巧你都会吗?

    路由参数解耦 一般在组件内使用路由参数,大多数人会这样做: export default { methods: { getParamsId() { return this.$route.params. ...

  9. java-随机点名2(新手)

    //创建的一个包名. package qige; //导入一个包.import java.util.*; //定义一个类.public class SJdm { //公共静态的主方法. public ...

  10. 性能测试工具LoadRuner你所不知道的内幕

    谈到性能测试,大家一定会联想到Jmeter和LoadRunner,这两款工具目前在国内使用的相当广泛,主要原因是Jmeter是开源免费,LoadRunner 11在现网中存在破解版本.商用型性能测试工 ...