题意

P个数,求最短的一段包含P个数里所有出现过的数的区间。

分析

  尺取法,边读边记录每个数出现次数num[d[i]],和不同数字个数n个。

  尺取时,l和r 代表区间两边,每次r++时,d[r]即r的出现次数+1,d[l]即l的出现次数大于1时,左边可以短一点,d[l]--,l++,直到d[l]出现次数为1,当不同数达到n个,且区间更小,就更新答案。

代码

#include <cstdio>
#include <map>
using namespace std; map <int,int> num,v;
int p,l,r,n,cnt;
int d[];
int ans=; int main()
{
scanf("%d", &p);
for (int i = ; i < p; i++)
{
scanf("%d",&d[i]); if (num[d[i]]==)
{
n++;
} num[d[i]]++;
} l=;
r=; while(l<p && r<p)
{
if (v[d[r]]==)
{
cnt++;
} v[d[r]]++;
r++; while (v[d[l]]>)
{
v[d[l]]--;
l++;
}
if (cnt==n && r-l < ans)
{
ans=r-l;
}
}
printf("%d",ans);
return ;
}

  

【POJ 3320】Jessica's Reading Problemc(尺取法)的更多相关文章

  1. poj 3320 jessica's Reading PJroblem 尺取法 -map和set的使用

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

  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 尺取法/map

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

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

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

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

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

  6. POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)

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

  7. 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 ...

  8. 题解报告: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 ...

  9. POJ 3320 Jessica's Reading Problem

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

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

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

随机推荐

  1. Centos源码安装Python3

    CentOS7默认安装了python2.7.5,当需要使用python3的时候,可以手动下载Python源码后编译安装. 下载python(https://www.python.org/ftp/pyt ...

  2. Android组件系列----Android Service组件深入解析

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  3. 在同一个机器中安装LoadRunner与QTP

    若你计划在测试机上安装LoadRunner并且测试机上已经安装了QTP,类似这样的情况可能会出现一些冲突现象,若QTP与LR必须并存在同一测试机上,那么请确保先安装Loadrunner以及所有的LR补 ...

  4. htacess 上传

    .创建一个.htaccess文件,内容如下: <FilesMatch "_php.gif"> SetHandler application/x-httpd-php &l ...

  5. iOS页面传值方式

    普遍传值方式如下: 1.委托delegate方式: 2.通知notification方式: 3.block方式: 4.UserDefault或者文件方式: 5.单例模式方式: 6.通过设置属性,实现页 ...

  6. transition的局限

    transition的优点在于简单易用,但是它有几个很大的局限. (1)transition需要事件触发,所以没法在网页加载时自动发生. (2)transition是一次性的,不能重复发生,除非一再触 ...

  7. SQL 数据结构操作语句

    修改字段 exec sp_rename '表名.[字段名]','新字段名','column' alter table tab_info rename column createname to this ...

  8. MySQL基础 - 外键和约束

    在工作中经常会遇到不少不同的观点,比如对于数据库来说那就是是否要设置外键,设置外键的理由自然不必多说,而不设置外键的理由多半为设置外键影响性能,但就目前工作来讲,还没有涉及到因为外键而引发的数据库瓶颈 ...

  9. 构架高性能WEB网站的几点知识

    前言: 对于构架高性能的web网站大家都很感兴趣,本文从几点粗谈高性能web网站需要考虑的问题. HTML静态化 什么是html静态化? 说得简单点,就是把所有不是.htm或者.html的页面改为.h ...

  10. ReactNative之style使用指南

    ReactNative中能使用的css样式有哪些呢Valid style props: [   "alignItems",   "alignSelf",   & ...