【poj2823】 Sliding Window
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的更多相关文章
- 【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个区间的最大值和最小 ...
- 【原创】Sliding Window Maximum 解法分析
这道题是lintcode上的一道题,当然leetcode上同样有. 本题需要寻找O(N)复杂度的算法. 解体思路比较有特点,所以容易想到参考 最小栈 的解题办法. 但是最小栈用栈维护最小值很直观,这道 ...
- 【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 ...
- 【POJ 2823】【Luogu P1886】Sliding Window 滑动窗口
POJ 2823 Luogu P1886 [解题思路] 这是一个单调队列算法的经典题目,几乎学习单调队列的人都接触过这题. 利用单调队列算法求出每一个固定区间内的最(大/小)值. 以下以最大值为例: ...
- 【POJ 2823 Sliding Window】 单调队列
题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...
- 【POJ 2823】Sliding Window(单调队列/堆)
BUPT2017 wintertraining(16) #5 D POJ - 2823 题意 给定n,k,求滑窗[i,i+k-1]在(1<=i<=n)的最大值最小值. 题解 单调队列或堆. ...
- 【翻译】Flink window
本文翻译自flink官网:https://ci.apache.org/projects/flink/flink-docs-release-1.9/dev/stream/operators/window ...
- 【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 ...
- 【转载】利用window.performance.timing进行性能分析
利用window.performance.timing进行性能分析 性能分析... window.performance.timing中相关属性语义: // .navigationStart 准备 ...
随机推荐
- 轉發XML
Delphi 中的 XMLDocument 类详解(3) - 读取 xml 文件 先虚拟一个测试文件: test.xml; 放在 c:\temp\ 下备用. <?xml version=&quo ...
- AI图片剪切
来源:http://tieba.baidu.com/p/1203332701?pid=14163166977&cid=78618096662&from=prin#78618096662 ...
- POJ 3259 Wormholes (判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...
- PHP中$_SERVER的详细参数与说明
$_SERVER['PHP_SELF'] #当前正在执行脚本的文件名,与 document root相关. $_SERVER['argv'] #传递给该脚本的参数. $_SERVER['argc'] ...
- Could not load file or assembly 'System.Data.SQLite' or one of its dependencies
试图加载格式不正确的程 异常类型 异常消息Could not load file or assembly 'System.Data.SQLite' or one of its dependencies ...
- Java 基础命名空间
java.lang (提供利用 Java 编程语言进行程序设计的基础类)java.lang.annotation(提供了引用对象类,支持在某种程度上与垃圾回收器之间的交互)java.lang.inst ...
- c语言中%s与%c对读入字符串的区别
对于scanf函数,需求%s类型时,\n是不会影响scanf内容的对于需求%c类型时,\n也是字符,自然会有影响.
- Java的性能优化
http://www.toutiao.com/i6368345864624144897/?tt_from=mobile_qq&utm_campaign=client_share&app ...
- mysql full text全文索引必要条件
show variables like 'ft_m%' 'ft_max_word_len', '84''ft_min_word_len', '4' 对于英文来说, ft_min_word_len=4是 ...
- go linux 学习记录
1 yum install mercurial 安装mercurial包 2 yum install git 安装git包 3 yum install gcc 安装gcc 4 然后就可以下载gola ...