题意

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. jquery中的clone()方法使用

    clone([Even[,deepEven]]) 描述: 克隆匹配的DOM元素并且选中这些克隆的副本. 在想把DOM文档中元素的副本添加到其他位置时这个函数非常有用. 1:一个布尔值(true 或者 ...

  2. leetcode-Maximum Subarray

    https://leetcode.com/problems/maximum-subarray/ Find the contiguous subarray within an array (contai ...

  3. bootstrap学习总结-css组件(三)

    今天我们来看看css组件效果以及其中比较重要的类,这些类都不难,关键要熟练掌握,搭配使用,灵活运用.关于前两篇中,css样式和布局的文章,大家可以在首页进行阅读.http://www.cnblogs. ...

  4. Colorable Fantasy UI

    Colorable Fantasy UI URL:https://www.assetstore.unity3d.com/#/content/7563 环境要求 Requires Unity 4.0.1 ...

  5. [转] Linux学习之CentOS(十三)--CentOS6.4下Mysql数据库的安装与配置

    from:  http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得 ...

  6. man命令中的文本操作

    转自:http://www.cnblogs.com/chengmo/archive/2010/10/26/1861809.html 下面说下less命令操作: 光标移动操作: e  ^E  j  ^N ...

  7. isScroll代码

    html: <div class="wrap"> <div class="content"> //内容-必须为第一个子元素 </d ...

  8. Gvim使用

    VIM删除空白行 命令:g/^\s*$/d :g 代表在全文档范围内 ^代表行的开始 \s*代表空白字符 &代表行的结束 d代表删除 用//将3段代码隔开

  9. python 3 安装 scrapy 并运行成功

    今天,python 3 安装 scrapy, 并运行成功.特此纪念! 我的环境:windows 10(64位) + python 3.5.2(64位) 其中几个要点说明一下: 1.有几个依赖库需要事先 ...

  10. 10个鲜为人知的WordPress函数

    WordPress功能强大,非常适合开发者使用.说到 WordPress,那么,我们不得不说他的钩子函数.今天,要为大家推荐10个WordPress函数.大多数,都是我们常用的功能,不过,经常不知道如 ...