http://poj.org/problem?id=2823 (题目链接)

题意

  维护滑动窗口最大最小值。

Solution

  sb单调队列

代码

// poj2823
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=1000010;
int n,m,a[maxn],ax[maxn],an[maxn]; int main() {
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&a[i]);
deque<int> qx,qn;
for (int i=1;i<=n;i++) {
while (!qx.empty() && i-qx.front()>=m) qx.pop_front();
while (!qn.empty() && i-qn.front()>=m) qn.pop_front();
while (!qx.empty() && a[qx.back()]<=a[i]) qx.pop_back();
while (!qn.empty() && a[qn.back()]>=a[i]) qn.pop_back();
qx.push_back(i);qn.push_back(i);
if (i>=m) ax[i-m+1]=a[qx.front()],an[i-m+1]=a[qn.front()];
}
for (int i=1;i<=n-m+1;i++) printf("%d ",an[i]);
puts("");
for (int i=1;i<=n-m+1;i++) printf("%d ",ax[i]);
return 0;
}

  

【poj2823】 Sliding Window的更多相关文章

  1. 【POJ2823】Sliding Window

    http://poj.org/problem?id=2823 题意:你有一个长度n的序列,分别询问[1,k],[2,k+1],[3,k+2],...,[n-k+1,n]这n-k+1个区间的最大值和最小 ...

  2. 【原创】Sliding Window Maximum 解法分析

    这道题是lintcode上的一道题,当然leetcode上同样有. 本题需要寻找O(N)复杂度的算法. 解体思路比较有特点,所以容易想到参考 最小栈 的解题办法. 但是最小栈用栈维护最小值很直观,这道 ...

  3. 【LeetCode 239】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. 【POJ 2823】【Luogu P1886】Sliding Window 滑动窗口

    POJ 2823 Luogu P1886 [解题思路] 这是一个单调队列算法的经典题目,几乎学习单调队列的人都接触过这题. 利用单调队列算法求出每一个固定区间内的最(大/小)值. 以下以最大值为例: ...

  5. 【POJ 2823 Sliding Window】 单调队列

    题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...

  6. 【POJ 2823】Sliding Window(单调队列/堆)

    BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...

  7. 【翻译】Flink window

    本文翻译自flink官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/stream/operators/window ...

  8. 【leetcode】Minimum Window Substring (hard) ★

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  9. 【转载】利用window.performance.timing进行性能分析

    利用window.performance.timing进行性能分析   性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备 ...

随机推荐

  1. php 正则匹配中文(转)

    我使用正则表达式来匹配中问的时候,出现了无法匹配的问题,问题如下 PCRE does not support \L, \l, \N{name}, \U, or \u at offset 2 我原来的匹 ...

  2. linux磁盘分区-系统安装

    零 系统下载: https://lists.centos.org/pipermail/centos-announce/2016-May/021895.html 往下拉可以看到 一 系统安装 1, 2, ...

  3. codevs 1531 山峰

    codevs 1531 山峰 题目描述 Description Rocky山脉有n个山峰,一字排开,从西向东依次编号为1, 2, 3, --, n.每个山峰的高度都是不一样的.编号为i的山峰高度为hi ...

  4. 关于Microsoft Visual Studio 2010系统自带的数据库

    转自:http://blog.sina.com.cn/s/blog_a570cca601012x5w.html 1.Visual studio Tools>命令提示 2.aspnet_regsq ...

  5. 端口扫描base

    #coding:utf8 import os import socket import sys def IsOpen(ip,port): s = socket.socket(socket.AF_INE ...

  6. Hibernate3.3.2 手动配置annotation环境

    简单记录Hibernate3.3.2如何快速配置环境 一.下载hibernate-distribution-3.3.2.GA-dist.zip文件,建立User libraries. 打开window ...

  7. MFC 调试方法

    AfxDebugBreak     MFC 提供特殊的 AfxDebugBreak 函数,以供在源代码中对断点进行硬编码:     AfxDebugBreak( ); 在 Intel 平台上,AfxD ...

  8. oracle 11g的企业管理器

    oracle 11g的企业管理器不同于其他版本,它是B/S的,默认登录为:https://localhost:1158/em,但我试了IE8跟360浏览器都不行,谷歌浏览器是可以访问的

  9. 更好的逐帧动画函数 — requestAnimationFrame 简介

    本文将会简单讲讲 requestAnimationFrame 函数的用法,与 setTimeout/setInterval 的区别和联系,以及当标签页隐藏时 requestAnimationFrame ...

  10. Asp.net Mvc中利用ValidationAttribute实现xss过滤

    在网站开发中,需要注意的一个问题就是防范XSS攻击,Asp.net mvc中已经自动为我们提供了这个功能.用户提交数据时时,在生成Action参数的过程中asp.net会对用户提交的数据进行验证,一旦 ...