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. 在 sys.servers 中找不到服务器 '10.0.2.13'。请验证指定的服务器名称是否正确。

    工作中,因为需要,搭建同事的程序模块,附加了从同事那里拷过来的该程序使用的库.(C#.C/S..Net Framework4.0 .WCF.Win10.SQL Server 2014.VS2015) ...

  2. C++;STL--队列与栈;

    队列 queue模板类的定义在<queue>头文件中. queue 模板类也需要两个模板参数,一个是元素类型,一个容器类型,元素类型是必要的,容器类型是可选的,默认为deque 类型. 定 ...

  3. Android渐变色xml配置

    这里渐变色: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=&quo ...

  4. 安装Composer与PsySH

    Windows安装Composer 需要开启 openssl 配置:打开 php 目录下的 php.ini,将 extension=php_openssl.dll 前面的分号去掉就可以了. https ...

  5. CentOS 7 防火墙常用操作及常见问题处理

    一.常用操作 1.启动防火墙: systemctl start firewalld.service 2.关闭防火墙: systemctl stop firewalld.service 3.添加放行端口 ...

  6. Wannafly挑战赛19:C. 多彩的树

    传送门 $k$的范围非常小, $O(n2^k)$求出状态最多为$S$的路径数, 然后容斥. #include <iostream> #include <sstream> #in ...

  7. linux下vim常用命令 (更新中...)

    1.注释多行 1). 首先按esc进入命令行模式下,按下Ctrl + v,进入VISUAL BLOCK模式; 2). 在行首使用上下键选择需要注释的多行; 3). 按下键盘(大写)“I”键,进入插入模 ...

  8. centos安装配置LAMP,https,fastcgi

    Centos7 配置LAMP+fastcgi(Centos7.2+php7.0+mariadb+httpd)   环境:阿里云centos7.3 一.安装并配置数据库 1.安装数据库 #yum ins ...

  9. flume复习(二)

    一.简介:flume是一种分布式.可靠且可用的系统,能够用于有效的从不同的源收集.聚合和移动大量的日志数据到集中式数据存储.它具有基于流数据的简单灵活的架构,它具有健壮的可靠性机制和许多故障转移和恢复 ...

  10. Win7下配置IIS服务器以及网站发布

    本文摘至于:http://heavengate.blog.163.com/blog/static/202381053201391111512986/ 1.vsual Studio 2010下利用本地I ...