POJ 3320 Jessica's Reading Problem (滑动窗口)
题意:给定一个序列,求一个最短区间,使得这个区间包含所有的种类数。
析:最近刚做了几个滑动窗口的题,这个很明显也是,肯定不能暴力啊,时间承受不了啊,所以
我们使用滑动窗口来解决,要算出所有的种数,我用set来计算的,当然也可以用别的,
由于要记录种类数,所以使用map来记录,删除和查找方便,说到这,这不就是水题了么。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <map> using namespace std;
typedef long long LL;
int a[1000007];
set<int> s;
map<int, int> mp; int main(){
int n;
while(~scanf("%d", &n)){
s.clear();
for(int i = 0; i < n; ++i){
scanf("%d", &a[i]);
s.insert(a[i]);
}
int len = s.size();
int ans = n, p = 0, q = 0;
while(true){
while(q < n && mp.size() < len) ++mp[a[q++]];
if(mp.size() < len) break;
ans = min(ans, q-p);
--mp[a[p]];
if(!mp[a[p]]) mp.erase(a[p]);
++p;
}
printf("%d\n", ans);
}
return 0;
}
POJ 3320 Jessica's Reading Problem (滑动窗口)的更多相关文章
- 尺取法 POJ 3320 Jessica's Reading Problem
题目传送门 /* 尺取法:先求出不同知识点的总个数tot,然后以获得知识点的个数作为界限, 更新最小值 */ #include <cstdio> #include <cmath> ...
- POJ 3320 Jessica's Reading Problem
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6001 Accept ...
- POJ 3061 Subsequence 尺取法 POJ 3320 Jessica's Reading Problem map+set+尺取法
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13955 Accepted: 5896 Desc ...
- POJ 3320 Jessica's Reading Problem 尺取法/map
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7467 Accept ...
- 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 ...
- POJ 3320 Jessica‘s Reading Problem(哈希、尺取法)
http://poj.org/problem?id=3320 题意:给出一串数字,要求包含所有数字的最短长度. 思路: 哈希一直不是很会用,这道题也是参考了别人的代码,想了很久. #include&l ...
- <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针
地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展 若满足要求则从左缩减区域 代码如下 正确性调整了几次 然后 ...
- 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 ...
- 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 ...
随机推荐
- 前端-CSS-9-文本和字体-背景颜色
字体属性 div{ width: 300px; height: 100px; /*background-color: red;*/ border: 1px solid red; /*设置字体大小 px ...
- python中关键字的总结
python中各种关键字的总结:用表格形式,解释关键字符号的作用和案例说明 关键字 ...
- How to read very large text files fast
Question Does anyone know the fastest way to read large text files (10Mb) into a string.Readln is ju ...
- Recursion递归
/*java.lang 核心包 如 String Math Integer System Thread等 拿来直接用 * java.awt 窗口工具 GUI * java.net 网络包 * java ...
- Andriod Studio adb 安装应用
原文链接:https://blog.csdn.net/u014608640/article/details/51833304 下面的命令安装.重新安装和卸载应用程序. 安装:adb -s HT9BYL ...
- Swagger+ springfox +Spring mvc
简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...
- UnicodeEncodeError: ‘ascii’ codec can’t encode
[UnicodeEncodeError: ‘ascii’ codec can’t encode] Python默认环境编码通过下面的方法可以获取: 基本上是ascii编码方式,由此Python自然调用 ...
- EasyUI 删除
<script type="text/javascript"> <!-- js --> /*================================ ...
- html标签对应的英文原文(转载)
标签 对应英文 说明 <!--> / 注释 <!DOCTYPE> document type 文档类型 <a> anchor 超链接 <abbr> a ...
- css3将图片、内容换为灰色
直接用filter属性-webkit-filter: grayscale(100%);-moz-filter: grayscale(100%);-ms-filter: grayscale(100%); ...