jessica's Reading PJroblem
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 9134   Accepted: 2951

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

题意:给你n个数字序列,求最小的子序列的长度,包含所有种类的数字
分析: 尺取法,能用尺取法的原因:假设当前a[s],a[s+1],,,,,a[t]包含只有种类,当s=s+1时,要仍然包含所有种类,则t'>=t,
        关键是set和map的使用 ,set相当于集合,map相当于数组,map数组可以开的大小范围不知道。。
int num[1000100],a[1000100];
int main()
{
int n;
while(~scanf("%d",&n))
{
set<int > w;
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
w.insert(a[i]);
}
int cnt=w.size();
int p=1,q=1,t=1,res=n+1;num[a[p]]++;//第一次re的代码,因为a[i]是整形范围的
//所以num就会爆内存,故只能换map了

  下面是AC代码

#include<cstdio>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include<map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a) memset(a,0,sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
int a[1000100];
int main()
{
int n;
while(~scanf("%d",&n))
{
set<int> w;map<int,int> num;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
w.insert(a[i]);//set相当于集合
}
int cnt=w.size();
int p=1,q=1,t=1,res=n+1;num[a[p]]++;
for(;;)
{
while(t<cnt&&q<=n){
q++;
if(!num[a[q]])
t++;
num[a[q]]++;
}
if(t<cnt)
break;
if(res>q-p+1) res=q-p+1;
num[a[p]]--;
if(!num[a[p]]) t--;
p++;
}
printf("%d\n",res);
}
return 0;
}

  

poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用的更多相关文章

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

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

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

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

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

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

  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(哈希、尺取法)

    http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...

  6. POJ 3320 Jessica's Reading Problem (尺取法)

    Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

  7. 题解报告:poj 3320 Jessica's Reading Problem(尺取法)

    Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...

  8. POJ 3320 Jessica's Reading Problem

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

  9. poj3061 Subsequence&&poj3320 Jessica's Reading Problem(尺取法)

    这两道题都是用的尺取法.尺取法是<挑战程序设计竞赛>里讲的一种常用技巧. 就是O(n)的扫一遍数组,扫完了答案也就出来了,这过程中要求问题具有这样的性质:头指针向前走(s++)以后,尾指针 ...

随机推荐

  1. [转帖]Java 8新特性探究(九)跟OOM:Permgen说再见吧

    Java 8新特性探究(九)跟OOM:Permgen说再见吧 https://my.oschina.net/benhaile/blog/214159 need study 很多开发者都在其系统中见过“ ...

  2. (5.5)mysql高可用系列——MySQL半同步复制(实践)

    关键词,mysql半同步复制 [0]实验环境 操作系统:CentOS linux 7.5 数据库版本:5.7.24 数据库架构:主从复制,主库用于生产,从库用于数据容灾和主库备机,采用默认传统的异步复 ...

  3. Servlet请求和响应

    在Java Web中Servlet.请求和响应是最基本的三个对象,在Web容器的管理下,这三者能够完成基本的HTTP请求处理. Servlet的作用是为客户提供服务.servlet的角色是接受一个客户 ...

  4. Redis缓存击穿

    缓存击穿 缓存击穿,是指一个key非常热点,在不停的扛着大并发,大并发集中对这一个点进行访问,当这个key在失效的瞬间,持续的大并发就穿破缓存,直接请求数据库,就像在一个屏障上凿开了一个洞. 比如在做 ...

  5. C++多线程基础学习笔记(四)

    一.创建多个子线程 前面三章讲的例子都是只有一个子线程和主线程,然而实际中有多个子线程.那么下面介绍如何创建多个子线程. #include <iostream> #include < ...

  6. python 变量 (全面不一样的变量)

    变量 一:什么是变量? 变量即变化的量,核心是"变"与"量"二字,变即变化,量即衡量状态 变:状态是会发生改变的 量:记录现实世界中的状态,让计算机能够像人一样 ...

  7. Https接口调用工具类

    ClientUtil.java import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org. ...

  8. php enum 数字类型插入失败的解决办法

    今天遇到一个问题,我设置mysql字段 existence 为enum ,值为 ''0','1'. 当使用 php 进行查找 where existence = 1 的时候,并没有查询出什么数据. 后 ...

  9. 互联网安全架构之常见的Web攻击手段及解决办法

    一.Web 安全常见攻击手段 XSS(跨站脚本攻击) SQL 注入 CSRF(跨站请求伪造) 上传漏洞 DDoS(分布式拒绝服务攻击)等 二.攻击手段原理及解决方案 1.XSS攻击 原理:XSS 攻击 ...

  10. ERROR qos-server can not bind localhost:22222

    dubboe版本2.7.1 spring cloud alibaba最新官网examples 根据readme中说明文档依次启动 1.nacos,默认用户名密码nacos/nacos 2.启动spri ...