题意:给定一个序列,求一个最短区间,使得这个区间包含所有的种类数。

析:最近刚做了几个滑动窗口的题,这个很明显也是,肯定不能暴力啊,时间承受不了啊,所以

我们使用滑动窗口来解决,要算出所有的种数,我用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 (滑动窗口)的更多相关文章

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

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

  2. POJ 3320 Jessica's Reading Problem

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

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

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

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

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

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

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

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

  7. <挑战程序设计竞赛> poj 3320 Jessica's Reading Problem 双指针

    地址 http://poj.org/problem?id=3320 解答 使用双指针 在指针范围内是否达到要求 若不足要求则从右进行拓展  若满足要求则从左缩减区域 代码如下  正确性调整了几次 然后 ...

  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 a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is co ...

随机推荐

  1. S+ hidden tray with window start

    官方论坛上有个帖子给出了答案: I forgot that this is supported in the code, but it requires a little editing of the ...

  2. VC 判断网络连接函数

    IsNetworkAlive Bool IsNetworkAlive( _Out_  LPDWORD lpdwFlags ); Header Sensapi.h Library Sensapi.lib ...

  3. keras图像预处理-ImageDataGenerator

    相关参数描述:http://keras-cn.readthedocs.io/en/latest/preprocessing/image/其中validation_split参数(官方上使用方法未描述) ...

  4. pandas 一行文本拆多行,一列拆多列

    https://zhuanlan.zhihu.com/p/28337202 一列拆多列: http://blog.csdn.net/qq_22238533/article/details/761875 ...

  5. win64+anaconda+xgboost(转)

    Windows下安装python版的XGBoost(Anaconda)          XGBoost是近年来很受追捧的机器学习算法,由华盛顿大学的陈天奇提出,在国内外的很多大赛中取得很不错的名次, ...

  6. Js获取iframe子页面全局变量

    项目中通过iframe内嵌了一个子页面,子页面定义了一些全局变量,父页面需要获取子页面的全局变量,做了一些测试(我的环境IE10和Firefox32.0.3),得出如下结论: IE下: window. ...

  7. mysql datetime与timestamp精确到毫秒的问题

    CREATE TABLE `tab1` (`tab1_id` VARCHAR(11) DEFAULT NULL,`create` TIMESTAMP(3) NULL DEFAULT NULL,`cre ...

  8. Gradle with Android

    [Gradle with Android] The Android Studio build system is based on Gradle, and the Android plugin for ...

  9. DNS使用的是TCP协议还是UDP协议

    原文:http://benbenxiongyuan.iteye.com/blog/1088085 DNS同时占用UDP和TCP端口53是公认的,这种单个应用协议同时使用两种传输协议的情况在TCP/IP ...

  10. js:二级联动示例

    联动原理 当用户点击省级的下拉选项,选择所在省,下一个下拉选项里的选项,则变成用户选择省下的所有市的信息,不会出现其它省市的信息. 省市数据 把省市数据,保存在js文件中,以json形式保存,以便读取 ...