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. Ionic3 创建应用后,目录结构

    ionic start myApp blank (空项目) hooks --编译cordova时自定义的脚本命令,方便整合到我们的编译系统和版本控制系统中 node_modules --node各类依 ...

  2. A strange lift

    Problem Description There is a strange lift.The lift can stop can at every floor as you want, and th ...

  3. Leftmost Digit

    Problem Description Given a positive integer N, you should output the leftmost digit of N^N.   Input ...

  4. python字典学习笔记

    字典是一种可变容器模型,且可存储任意类型对象.键是不可变类型(且是唯一的),值可以是任意类型(不可变类型:整型,字符串,元组:可变类型:列表,字典).字典是无序的,没有顺序关系,访问字典中的键值是通过 ...

  5. window10下的eclipse用java连接hadoop执行mapreduce任务

    一.准备工作 1.eclipse连接hadoop的插件,需要版本匹配,这有几个常用的 2 版本的插件 hadoop2x-eclipse-plugin-master 密码:feg1 2.hadoop-c ...

  6. Oracle-1 - :超级适合初学者的入门级笔记,CRUD,事务,约束 ......

    Oracle 更改时间: 2017-10-25  -  21:33:49 2017-10-26  -  11:43:19 2017-10-27  -  19:06:57 2017-10-28  -  ...

  7. VS2015企业版序列号

    vs2015 企业版HM6NR-QXX7C-DFW2Y-8B82K-WTYJV2XNFG-KFHR8-QV3CP-3W6HT-683CH

  8. JavaScript系列-----对象基于哈希存储(<Key,Value>之Value篇) (3)

    JavaScript系列-----Objectj基于哈希存储<Key,Value>之Value 1.问题提出 在JavaScript系列-----Object之基于Hash<Key, ...

  9. 《天书夜读:从汇编语言到windows内核编程》三 练习反汇编C语言程序

    1) Debug版本算法反汇编,现有如下3×3矩阵相乘的程序: #define SIZE 3 int MyFunction(int a[SIZE][SIZE],int b[SIZE][SIZE],in ...

  10. <p></p>标签为什么不能包含块级标签?还有哪些特殊的HTML标签?

    最近,在码代码的时候,就是下面的这段代码,我犯了一个很不起眼,但犯了就致命的BUG. <body> <p> <ol> <li>Hello</li& ...