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. threading模块

    threading — Higher-level threading interface¶ Source code: Lib/threading.py This module constructs h ...

  2. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  3. [支付]微信NATIVE扫码支付JAVA实现

    步骤: 1.预订单 2.接受微信返回的url 3.将url转为二维码显示到页面上 4.扫码支付 5.接收微信的异步通知,在这步修改订单的状态 6.收到异步通知的同时给微信返回指定数据,告知对方已成功处 ...

  4. 改变Oracle数据库连接端口

    Oracle数据库默认数据库监听与连接端口是1521, 但是有时候项目中需要更改默认端口 这样做很多时候客户要求,基于安全考虑. 以Oracle 11g 为例, 更改Listener的端口大致 需要以 ...

  5. cocos2d-x CCScale9Sprite

    转自:http://www.cocos2dev.com/?p=295 前段时间看CCEditBox的时候,发现里面有个利用9宫格图缩放图片的,也就是缩放带圆角的图片. 这个比较有用处,很多游戏中有很多 ...

  6. python的一些总结1

    1.安装环境 window用户下载 python :https://www.python.org/downloads/release/python-2710/ 安装不解释.. 配置环境变量  找到 P ...

  7. java笔记-关于一些常用 且实用的开源包

    作为一只从.net转java的程序猿..表示有些jdk原生的类很不习惯.. 1.时间处理 代替原生Calendar   joda-time 框架.地址https://github.com/JodaOr ...

  8. 使用EA逆向生成数据库E-R图

    1. 创建ODBC数据源 2. 逆向工程

  9. Codeforces Gym 100418A A - A+-B java高精度

    A - A+-BTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.acti ...

  10. IOS AutoLayout 文章

    开始iOS 7中自动布局教程(一) 开始iOS 7中自动布局教程(二) 代码的方式自动布局 自动布局时计算Cell高度