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
 #include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN=1e6+;
int Q[MAXN],a[MAXN];
vector<int>MAX;
vector<int>MIN;
int n,m;
void get_MIN()
{
int head=,tail=;
Q[tail]=;
for (int i = ; i <=n ; ++i) {
while(head<=tail&&a[i]<a[Q[tail]]) tail--;
Q[++tail]=i;
while(head<=tail&&Q[head]<i-m+) head++;
if(i>=m)
MIN.push_back(a[Q[head]]);
}
} void get_MAX()
{
int head,tail;
head=;tail=;
Q[tail]=;
for (int i = ; i <=n ; ++i) {
while(head<=tail&&a[i]>a[Q[tail]]) tail--;
Q[++tail]=i;
while(head<=tail&&Q[head]<i-m+)
head++;
if(i>=m)
MAX.push_back(a[Q[head]]);
}
} int main()
{
scanf("%d%d",&n,&M);
for (int i = ; i <=n ; ++i) {
scanf("%d",&a[i]);
}
get_MIN();
for (int i = ; i <MIN.size() ; ++i) {
printf("%d ",MIN[i]);
}
printf("\n");
get_MAX();
memset(Q,, sizeof(Q));
for (int i = ; i <MAX.size() ; ++i) {
printf("%d ",MAX[i]); }
printf("\n");
return ;
}

Sliding Window POJ - 2823的更多相关文章

  1. Sliding Window POJ - 2823 单调队列模板题

    Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...

  2. AC日记——Sliding Window poj 2823

    2823 思路: 单调队列: 以前遇到都是用线段树水过: 现在为了优化dp不得不学习单调队列了: 代码: #include <cstdio> #include <cstring> ...

  3. 单调栈(G - Sliding Window POJ - 2823 )

    题目链接:https://cn.vjudge.net/contest/276251#problem/G 题目大意:给你n和m,然后问你对于(m,n)这中间的每一个数,(i-m+1,i)这个区间的最小值 ...

  4. sliding windows (poj 2823) 题解

    [问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: [样例输入] 8 3 1 3 -1 -3 5 3 6 7 [样例输 ...

  5. poj 2823 Sliding Window (单调队列入门)

    /***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...

  6. POJ 2823 Sliding Window + 单调队列

    一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1)   从队首删除 (2)   从队尾删除 (3)   从队尾插入 (4)   ...

  7. POJ 2823 Sliding Window 题解

    POJ 2823 Sliding  Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...

  8. 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)

    To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...

  9. POJ 2823 Sliding Window ST RMQ

    Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...

随机推荐

  1. SpringMVC07SelfException 自定义异常

    1.配置web.xml文件 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  2. FormsAuthentication IsAuthenticated一直为false

    解决办法: 在Web.Config中添加一下红框的内容

  3. C 碎片七 指针

    一.地址和指针 程序在编译过程中,系统会根据变量类型分配一定长度的内存单元.内存区中的每个字节都有一个编号,该内存单元的初始编号就是变量的"地址/指针",该内存单元的长度就是变量的 ...

  4. DockerSwarm 微服务部署

    一.简介 之前<服务Docker化>中,使用 docker-compose.yml 来一次配置启动多个容器,在 Swarm 集群中也可以使用 compose 文件 (docker-comp ...

  5. 超链接显示网站 A,访问后进入网站 B

    #前端黑魔法# 出一个思考题:如何用最少的字符实现下图效果.即超链接显示网站 A,访问后进入网站 B. 当然这个是上古时代的黑魔法了,稍懂前端的都知道原理.所以这里只问最短的实现~ 一个简单的演示:( ...

  6. mysql配置jdbc

    jdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/database?useUnicode=t ...

  7. IT技能等级

    IT技能 低:会使用,会简单维修(操作) 中:能跟据实际业务需求扩展(技改.完善) 中高:找产品,能组合,能设计创造(出方案) 高:能规划(规划)

  8. 实战:ADFS3.0单点登录系列-集成MVC

    本文将讲解如何让MVC应用程序与ADFS集成,完成认证的过程. 目录: 实战:ADFS3.0单点登录系列-总览 实战:ADFS3.0单点登录系列-前置准备 实战:ADFS3.0单点登录系列-ADFS3 ...

  9. Python核心编程——正则表达式

    本篇博客都是我认真读之后写的,有些地方和书上的语句一样…… 1.简介 正则表达式为高级的文本模式匹配.抽取.与/或文本形式的搜索和替换功能提供了基础.简单地说,正则表达式(简称regex)是一些由字符 ...

  10. 定义多个属性 Object.defineProperties()

    var book = {} Object.defineProperties(book,{ _year:{ value:2004 }, editable:{ value:1 }, year:{ get: ...