Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 53676   Accepted: 15399
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
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

The input consists of two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

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

4373 窗口

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 黄金 Gold
 查看运行结果
 
 
 
 
 
题目描述 Description

给你一个长度为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.

输入描述 Input Description

第1行n,k,第2行为长度为n的数组

输出描述 Output Description

2行

第1行每个位置的min value

第2行每个位置的max value

样例输入 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

数据范围及提示 Data Size & Hint

数据范围: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 ;
}
AC代码(裸的单调队列)c++提交
#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的更多相关文章

  1. POJ2823 Sliding Window (单调队列)

    POJ2823 Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 38342   Accepte ...

  2. 滑动窗口(Sliding Window)技巧总结

    什么是滑动窗口(Sliding Window) The Sliding Problem contains a sliding window which is a sub – list that run ...

  3. [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 ...

  4. 数据流滑动窗口平均值 · sliding window average from data stream

    [抄题]: 给出一串整数流和窗口大小,计算滑动窗口中所有整数的平均值. MovingAverage m = new MovingAverage(3); m.next(1) = 1 // 返回 1.00 ...

  5. 【LeetCode】480. 滑动窗口中位数 Sliding Window Median(C++)

    作者: 负雪明烛 id: fuxuemingzhu 公众号: 每日算法题 本文关键词:LeetCode,力扣,算法,算法题,滑动窗口,中位数,multiset,刷题群 目录 题目描述 题目大意 解题方 ...

  6. POJ2823 Sliding Window

    Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 53086   Accepted: 15227 Case Time Limi ...

  7. POJ2823 Sliding Window(单调队列)

    单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...

  8. [POJ2823]Sliding Window 滑动窗口(单调队列)

    题意 刚学单调队列的时候做过 现在重新做一次 一个很经典的题目 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗 ...

  9. poj2823 Sliding Window luogu1886 滑动窗口 单调队列

    模板题 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

随机推荐

  1. codeforces 630C Lucky Numbers

    C. Lucky Numbers time limit per test 0.5 seconds memory limit per test 64 megabytes input standard i ...

  2. 系统级性能分析工具 — Perf

    从2.6.31内核开始,linux内核自带了一个性能分析工具perf,能够进行函数级与指令级的热点查找. perf Performance analysis tools for Linux. Perf ...

  3. UVA 624 - CD (01背包 + 打印物品)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. Intel HAXM

    Hardware Accelerated Execution Manager的缩写. intel的硬件加速执行管理器,是一款可以使用英特尔虚拟化技术(VT)加快 Android* 开发速度的硬件辅助虚 ...

  5. session cookie原理及应用

    一.术语session在我的经验里,session这个词被滥用的程度大概仅次于transaction,更加有趣的是transaction与session在某些语境下的含义是相同的. session,中 ...

  6. C#IEnumerator.MoveNext 方法 ()

    将枚举数推进到集合的下一个元素. 命名空间:   System.Collections程序集:  mscorlib(mscorlib.dll 中) 语法: bool MoveNext() 返回值 Ty ...

  7. Meta键盘

    由于著名的编辑器Emacs中用到Meta键,但如今大多国人所用键盘上实际并无此键,想必多有不明之处,故多方收集资料撰写此文,简要描述了Meta键及相关键盘的发展始末,至于在Emacs上如何使用国人键盘 ...

  8. CustomerSOList

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  9. VB连接Mysql数据库

    当然机器装有mysql数据库 然后下载安装Mysql,odbc驱动 须要加入ado'菜单"project"->"引用" 找 Microsoft Activ ...

  10. PI-利用SoapUI 测试web service的方法介绍

    在运用webservice调用数据的过程中,非常关键的一个步骤就是获取到webservice的地址,并测试webservice的连通情 况,webservice的连通测试主要是两个方面:1,查看web ...