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 ...
随机推荐
- UI5-文档-4.15-Nested Views
我们的面板内容变得越来越复杂,现在是时候将面板内容移动到一个单独的视图中了.使用这种方法,应用程序结构更容易理解,应用程序的各个部分可以重用. Preview The panel content is ...
- LINQ to SQL语句(1)Select查询的九种形式
目录 说明 简单形式 匿名类型形式 条件形式 指定类型形式 筛选形式 Shaped形式 嵌套形式 本地调用方法形式 Distinct形式 说明 与SQL命令中的select作用相似但位置不同,查询表达 ...
- ios 避免navigationcontroller出现时scrollview内容被resize
viewDidLoad中设置以下属性 self.automaticallyAdjustsScrollViewInsets = NO;
- 行矩阵列矩阵D3D&GL&U3D
void Start () { //矩阵函数原型:Matrix4x4(Vector4 colum0, Vector4 colum1, Vector4 colum2, Vector4 colum3),这 ...
- WAL 【转】
重做日志:每当有操作执行前,将数据真正更改时,先前相关操作写入重做日志.这样当断电,或者一些意外,导致后续任务无法完成时,系统恢复后,可以继续完成这些更改 撤消日志:当一些更改在执行一半时,发生意外, ...
- Retrofit2.0+RxJava2.0问题
问题1:java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 2 path ...
- TOMCAT Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level.
十二月 31, 2014 2:32:45 下午 org.apache.coyote.http11.AbstractHttp11Processor process信息: Error parsing HT ...
- error CS1010 CS8025 CS1012 CS1525 常见文档错误解决
error CS1010: Newline in constant error CS8025: Parsing error error CS1012: Too many characters in c ...
- [leetcode]318. Maximum Product of Word Lengths单词长度最大乘积
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the tw ...
- php 下载生成word文件
方案一 $html2 ='数字'; header("Content-type: application/octet-stream"); header("Accept-Ra ...