codevs4373 窗口==poj2823 Sliding Window
Time Limit: 12000MS | Memory Limit: 65536K | |
Total Submissions: 53676 | Accepted: 15399 | |
Case Time Limit: 5000MS |
Description
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
Output
Sample Input
- 8 3
- 1 3 -1 -3 5 3 6 7
Sample Output
- -1 -3 -3 -3 3 3
- 3 3 5 5 6 7
给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表:
Window position | Min value | Max value |
[ 1 3 -1 ] -3 5 3 6 7 | -1 | 3 |
1 [ 3 -1 -3 ] 5 3 6 7 | -3 | 3 |
1 3 [ -1 -3 5 ] 3 6 7 | -3 | 5 |
1 3 -1 [ -3 5 3 ] 6 7 | -3 | 5 |
1 3 -1 -3 [ 5 3 6 ] 7 | 3 | 6 |
1 3 -1 -3 5 [ 3 6 7 ] | 3 | 7 |
你的任务是找出窗口在各位置时的max value, min value.
第1行n,k,第2行为长度为n的数组
2行
第1行每个位置的min value
第2行每个位置的max value
8 3
1 3 -1 -3 5 3 6 7
-1 -3 -3 -3 3 3
3 3 5 5 6 7
数据范围:20%: n<=500; 50%: n<=100000;100%: n<=1000000;
分类标签 Tags 点此展开

2017-03-27
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- using namespace std;
- const int N=1e6+;
- inline int read(){
- int x=,f=;char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
- while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
- return x*f;
- }
- int n,m,a[N],q[N],id[N];
- int main(){
- n=read();m=read();
- for(int i=;i<=n;i++) a[i]=read();
- int h=,t=;
- for(int i=;i<m;i++){
- while(h<t&&i-id[h]>=m) h++;
- while(h<t&&q[t-]>a[i]) t--;
- q[t]=a[i];id[t++]=i;
- }
- for(int i=m;i<=n;i++){
- while(h<t&&i-id[h]>=m) h++;
- while(h<t&&q[t-]>a[i]) t--;
- q[t]=a[i];id[t++]=i;
- if(h<t) printf("%d ",q[h]);
- }
- putchar('\n');
- h=,t=;
- for(int i=;i<m;i++){
- while(h<t&&i-id[h]>=m) h++;
- while(h<t&&q[t-]<a[i]) t--;
- q[t]=a[i];id[t++]=i;
- }
- for(int i=m;i<=n;i++){
- while(h<t&&i-id[h]>=m) h++;
- while(h<t&&q[t-]<a[i]) t--;
- q[t]=a[i];id[t++]=i;
- if(h<t) printf("%d ",q[h]);
- }
- return ;
- }
- #include<cstdio>
- using namespace std;
- inline int read(){
- register int x=;bool f=;
- register char ch=getchar();
- while(ch<''||ch>''){if(ch=='-')f=;ch=getchar();}
- while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
- return f?x:-x;
- }
- const int N=1e6+;
- int n,k,a[N],num[N],q[N];
- int main(){
- n=read();k=read();
- for(int i=;i<=n;i++) a[i]=read();
- int l=,r=;
- for(int i=;i<k;i++){
- for(;l<=r&&i-num[l]>=k;l++);
- for(;l<=r&&a[i]<q[r];r--);
- q[++r]=a[i];num[r]=i;
- }
- for(int i=k;i<=n;i++){
- for(;l<=r&&i-num[l]>=k;l++);
- for(;l<=r&&a[i]<q[r];r--);
- q[++r]=a[i];num[r]=i;
- printf("%d ",q[l]);
- }
- putchar('\n');
- l=,r=;
- for(int i=;i<k;i++){
- for(;l<=r&&i-num[l]>=k;l++);
- for(;l<=r&&a[i]>q[r];r--);
- q[++r]=a[i];num[r]=i;
- }
- for(int i=k;i<=n;i++){
- for(;l<=r&&i-num[l]>=k;l++);
- for(;l<=r&&a[i]>q[r];r--);
- q[++r]=a[i];num[r]=i;
- printf("%d ",q[l]);
- }
- return ;
- }
codevs4373 窗口==poj2823 Sliding Window的更多相关文章
- POJ2823 Sliding Window (单调队列)
POJ2823 Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 38342 Accepte ...
- 滑动窗口(Sliding Window)技巧总结
什么是滑动窗口(Sliding Window) The Sliding Problem contains a sliding window which is a sub – list that run ...
- [Swift]LeetCode239. 滑动窗口最大值 | Sliding Window Maximum
Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...
- 数据流滑动窗口平均值 · sliding window average from data stream
[抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...
- 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)
作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...
- POJ2823 Sliding Window
Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 53086 Accepted: 15227 Case Time Limi ...
- POJ2823 Sliding Window(单调队列)
单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...
- [POJ2823]Sliding Window 滑动窗口(单调队列)
题意 刚学单调队列的时候做过 现在重新做一次 一个很经典的题目 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗 ...
- poj2823 Sliding Window luogu1886 滑动窗口 单调队列
模板题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...
随机推荐
- CodeForces 689E Mike and Geometry Problem (离散化+组合数)
Mike and Geometry Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/I Description M ...
- [转]directsound抓取麦克风PCM数据封装类
网上有很多方法从麦克风读取PCM数据,不想一一举例.只是在这里发布一个我自己写的directsound的麦克风PCM数据采集类,通过它,可以很方便的利用directsound技术把麦克风的数据采集到, ...
- 备份spfile 中的一个误区
某书载在备份控制文件的时候,也会自动的备份初始化参数文件,抱着愚钝的 完事亲力亲为的态度,做了如下的小验证. RMAN> list backup of controlfile; specific ...
- 内容输出Linux文件系统的的实现:创建一个文件的过程
题记:写这篇博客要主是加深自己对内容输出的认识和总结实现算法时的一些验经和训教,如果有错误请指出,万分感谢. 考虑上面这个命令: who > userlist 当这个命令完成后,文件系统增加l一 ...
- Webserver推送技术
server推送(Server Push) 推送技术的基础思想是将浏览器主动查询信息改为server主动发送信息.server发送一批数据,浏览器显示这些数据,同一时候保证与server的连接.当se ...
- 几种server模型
TCP測试用客户程序 每次执行客户程序,在命令行參数指定server的ip地址,port,发起连接的子进程数,和一个待发送的字符串数据,客户程序将模拟多个客户依据指定的子进程数创建子进程来并发的连接到 ...
- [AngularJS] ui-router: Abstract States
ui-router has the powerful ability to define abstract states, or states that can't be navigated to, ...
- iOS开发——网络编程Swift篇&(八)SwiftyJSON详解
SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...
- 【BZOJ1486】【HNOI2009】最小圈 分数规划 dfs判负环。
链接: #include <stdio.h> int main() { puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网 ...
- phpstorm 和web storm汉化
http://www.jincaimao.com/cms-phpstorm-index.html phpStorm汉化方法: B1).找到X:\Program Files\JetBrains\PhpS ...