http://poj.org/problem?id=2823

题意:你有一个长度n的序列,分别询问[1,k],[2,k+1],[3,k+2],...,[n-k+1,n]这n-k+1个区间的最大值和最小值。

单调队列入门题。用两个单调队列分别维护当前最大值和最小值的最优解、次优解、……K优解。

每次拓展一个数就不断将队尾的劣解出队,保持队列的单调性。然后不断将队首的过气解(即距离当前位置大于等于k)出队。之后队列的队首就是最优解了。

#include <iostream>
#include <deque>
#define maxn 1000005
using namespace std;
int n, k, a[maxn];
deque<int> mx, mn; // 分别存最大值的最优解和最小值的最优解
int mxans[maxn], mnans[maxn];
int main()
{
ios::sync_with_stdio(false);
cin >> n >> k;
for (int i = ; i <= n; i++)
cin >> a[i];
for (int i = ; i < k; i++) // 先处理前k-1个数
{
// 将队尾的劣解出队
while (!mn.empty() && a[mn.back()] > a[i])
mn.pop_back();
mn.push_back(i);
while (!mx.empty() && a[mx.back()] < a[i])
mx.pop_back();
mx.push_back(i); // 插入当前解
}
for (int i = k; i <= n; i++)
{
// 将队尾的劣解出队
while (!mn.empty() && a[mn.back()] > a[i])
mn.pop_back();
mn.push_back(i);
while (!mx.empty() && a[mx.back()] < a[i])
mx.pop_back();
mx.push_back(i); // 将队首的过气解出队
while (!mx.empty() && i - mx.front() >= k)
mx.pop_front();
while (!mn.empty() && i - mn.front() >= k)
mn.pop_front(); mxans[i] = mx.front();
mnans[i] = mn.front();
}
for (int i = k; i <= n; i++)
cout << a[mnans[i]] << ' ';
cout << endl;
for (int i = k; i <= n; i++)
cout << a[mxans[i]] << ' ';
cout << endl;
return ;
}

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

  1. 【poj2823】 Sliding Window

    http://poj.org/problem?id=2823 (题目链接) 题意 维护滑动窗口最大最小值. Solution sb单调队列 代码 // poj2823 #include<algo ...

  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. CodeForces - 385C Bear and Prime Numbers (埃氏筛的美妙用法)

    Recently, the bear started studying data structures and faced the following problem. You are given a ...

  2. 1.Introduction 介绍

    Welcome to Log4j 2! Introduction Almost every large application includes its own logging or tracing ...

  3. 【转载】目前主流过滤XSS的三种技术

    目前主流过滤XSS的三种技术 过滤 过滤,顾名思义,就是将提交上来的数据中的敏感词汇直接过滤掉.例如对"<script>"."<a>". ...

  4. 和为S的两个数

    题目 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的. 输出描述: 对应每个测试案例,输出两个数,小的先输出. 思考 注 ...

  5. 数据结构--汉诺塔递归Java实现

    /*汉诺塔递归 * 1.将编号0-N-1个圆盘,从A塔座移动到B上面 * 2.将编号N的1个圆盘,从A移动到C上面 * 3.最后将B上面的N-1个圆盘移动到C上面 * 注意:盘子的编号从上到下1-N ...

  6. 利用Python循环(包括while&for)各种打印九九乘法表

    一.for循环打印九九乘法表 #注意:由于缩进在浏览器不好控制,请大家见谅,后续会有图片传入. 1.1 左下角 for i in range(1,10): for j in range(1,i+1): ...

  7. HTML5对音视频的处理

      前  言 现在网上有许多的框架和插件,能够满足程序猿的各种需求,慢慢的,就有些忽视最基础的东西. 比如,大多数视频是通过插件(比如 Flash)来显示的.然而,并非所有浏览器都拥有同样的插件. H ...

  8. tomcat不编译webapps下的war包的解决办法

    1.首先看看tomcat是否能正常启动,如果启动tomcat一闪而过那么就使用dos命令启动tomcat看看报什么错 如果是端口占用的错误.使用netstat -ano命令查看占用端口的程序 然后用任 ...

  9. 重构手法之Introduce Explaining Variable(引用解释性变量)

    返回总目录 6.5Introduce Explaining Variable(引用解释性变量) 概要 你有一个复杂的表达式. 将该复杂表达式(或其中一部分)的结果放进一个临时变量,以此变量名称来解释表 ...

  10. Python之hashlib模块

    hashlib 在做一个授权管理系统,需要生产动态生成密码,故使用hashlib >>> import time >>> import hashlib >&g ...