【Sliding Window】单调队列
题目描述
给你一个长度为 N 的数组,一个长为 K 的滑动的窗体从最左移至最右端,你只能见到窗口的 K 个整数,每次窗体向右移动一位,如下表:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAj8AAACeCAYAAADdc2tVAAAMiklEQVR4nO3d0ZLrJhIA0J1U/v+XszuVuNaRJVmCBgF9zlNyx5bBbkSbRvLPX//zHwCAJP54ugEAAD1JfgCAVCQ/AEAqf77/z8/Pz1PtAABo5n2L859nf4TV/Sb8Yp5sxD3ZbBd3lL0AgFQ+Vn749MoYS74plTx37zm+qfGkqyXxVjF6NI5qxiZ8Exn3UXNB6bH4N8nPP64E+dFjzgLw92+/z4tIXq4c4+6+LYOHO47i5W7clZ68j15n++/imkhRcV9K3MeT/AQ7GwxXAvVoUribRL0e8/747X/DXeKGjMT9eiQ/G9vkYFt62ntMK1ey/SvfSI7+G+564hvw2SqR5X96EPfrkfz8Yy/J2XvM++rLt3LXkatLmCUMFFoqjdFvK5Z3ysJ7bbjyhQBKlZZ17/ytZEuDuC8n+XmzF0jbxOE9AXr/9xai9kN8+7tBwzc9YmQvAbJSyZOeOjeK+/YkP29eic2vbfDtBWPkwPi2GnRWfvvWHis/lKg5AV9ZSa19fYkSLUTEfc0KvLjvQ/Jz4MrenzMlAfmefNVwZQCz2dvQ/21vnZM+KxL3fUh+TuxtFG6VMBwdt+ZS+acvz2RuR18A7oyFluPGN2BaiIj7lsR9DMnPge3+n8j759QEa8nEAxFal4LvJvrimx4i4n77RbpmnIj7GJKfE9ukJyrj7/XNwcoPkfa+Ab9O5Gcx1Wu1VFzTQmncHx2r9qa34j6G5OfA3h6fiLs017blDoOCSHc25b9ElWrvtAcilcT92eNqEyBxH0Pyc+LuTQ2ja8JX7xVxtl/o7nHhyHYclH77jW7Pi7imhZq4P7syN2ovp7gvI/k5cHRX5FED7dsNsM6e8/QGPsZ1Fvvb5f8rJ/La8XNlLD61Qss6IuL+2/m1dNyI+xiSnwN3Nyy3SCQE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hD1+RntHvT9HyN2mNH/CzI+zEi+tqqT0oja5PskJXYH8cUKz+UmXWgzdpuAOZgzw8AkIrkBwBI5aPsZb8F2Yh5MhL3ZPav5MdeCwBgdcpeAEAqkh8AIJX/Au9b9E2oRX50AAAAAElFTkSuQmCC" alt="" />
你的任务是找出窗口在各位置时的最大值和最小值。
输入格式
输入的第 1 行是两个整数 n,k,第 2 行为长度为 n 的数组(即有 n 个整数)。
输出格式
输出 2 行,第 1 行是每个位置的最小值,第 2 行是每个位置的最大值。
样例数据 1
备注
【数据范围】
对于 20% 的数据:n<=500;
对于 50% 的数据:n<=100000;
对于 100% 的数据:n<=1000000;
题目分析
入门级别单调队列。分别维护一个单调递增和单调递减的队列,每次添加新数并维护单调性,然后删除队首的在区间外面的数,最后队首就是答案。
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std; const int N = , oo = 0x3f3f3f3f;
typedef pair<int, int> P;
int data[N], head, tail, n, k;
P que[N]; inline int read(){
int i = , f = ; char ch = getchar();
for(; (ch < '' || ch > '') && ch != '-'; ch = getchar());
if(ch == '-') f = -, ch = getchar();
for(; ch >= '' && ch <= ''; ch = getchar())
i = (i << ) + (i << ) + (ch - '');
return i * f;
} inline void wr(int x){
if(x < ) putchar('-'), x = -x;
if(x > ) wr(x / );
putchar(x % + '');
} int main(){
n = read(), k = read();
for(int i = ; i <= n; i++) data[i] = read();
tail = head = ;
que[head].second = oo;
for(int i = ; i <= n; i++){
if(data[i] < que[head].second)
que[head = tail = ] = P(i, data[i]);
else{
while(head < tail && que[tail].second > data[i]) tail--;
que[++tail] = P(i, data[i]);
}
while(que[head].first <= i - k) head++;
if(i >= k)
wr(que[head].second), putchar(' ');
// cout<<endl<<"!!";for(int j = head; j <= tail; j++) cout<<"("<<que[j].first<<") "<<que[j].second<<" ";cout<<endl;
}
putchar('\n');
tail = head = ;
que[head].second = -oo;
for(int i = ; i <= n; i++){
if(data[i] > que[head].second)
que[head = tail = ] = P(i, data[i]);
else{
while(head < tail && que[tail].second < data[i]) tail--;
que[++tail] = P(i, data[i]);
}
while(que[head].first <= i - k) head++;
if(i >= k)
wr(que[head].second), putchar(' ');
// cout<<endl<<"!!";for(int j = head; j <= tail; j++) cout<<que[j].first<<" "<<que[j].second<<" ";cout<<endl;
}
return ;
}
【Sliding Window】单调队列的更多相关文章
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- POJ2823 Sliding Window(单调队列)
单调队列,我用deque维护.这道题不难写,我第二次写单调队列,1次AC. -------------------------------------------------------------- ...
- POJ 2823:Sliding Window 单调队列
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 48930 Accepted: 14130 ...
- POJ 2823 Sliding Window (单调队列)
单调队列 加了读入挂比不加更慢.... 而且这份代码要交c++ 有大神G++跑了700ms..... orzorzorz #include<iostream> #include<cs ...
- POJ 2823 UESTCoj 1221 Sliding Window 单调队列 经典入门题
题意:给出一个序列,求出每连续k个数字中最大的数和最小的数. 这是道单调队列裸题,直接写就行了. 本来用deque写出来后,发现在poj上硬是超时了,在discuss上看很多人也在抱怨超时的问题,据说 ...
- poj 2823 Sliding Windows (单调队列+输入输出挂)
Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 73426 Accepted: 20849 ...
- poj2823Sliding Window——单调队列
题目:http://poj.org/problem?id=2823 单调队列模板. 代码如下: #include<iostream> #include<cstdio> usin ...
- 【t019】window(单调队列)
Time Limit: 2 second Memory Limit: 256 MB [问题描述] 给你一个长度为N 的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右 ...
- POJ2823 Sliding Window (单调队列)
POJ2823 Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 38342 Accepte ...
随机推荐
- GO语言学习(二十)Go 语言递归函数
Go 语言递归函数 递归,就是在运行的过程中调用自己. 语法格式如下: func recursion() { recursion() /* 函数调用自身 */ } func main() { recu ...
- java hadoop file system API
org.apache.hadoop.fs Class FileSystem java.lang.Object org.apache.hadoop.fs.FileSystem All Implement ...
- [D3] Adding Arrows to Links
svg.append('defs').selectAll('marker') .data(['end']).enter() .append('marker') .attr('id', String) ...
- angular material dialog应用
1. 打开弹窗的点击事件 project.component.html <button mat-icon-button class="action-button" (clic ...
- 微服务实战(一):微服务架构的优势与不足 - DockOne.io
原文:微服务实战(一):微服务架构的优势与不足 - DockOne.io [编者的话]本文来自Nginx官方博客,是微服务系列文章的第一篇,主要探讨了传统的单体式应用的不足,以及微服务架构的优势与挑战 ...
- Eclipse 出错 Error:Could not create the Java Virtual Machine Error:A fatal exception has occurred
提示如下: scala compile server. error:could not create the java machine.Error: A fatal exception has occ ...
- Redis学习笔记--Hash(五)
Redis的数据是通过key-value的方式存储的,对于value的数据类型有字符串.Hash.list.set.sortedSet在redis命令语句中,语句是忽略大小写的,但是key是不可以忽略 ...
- testng并发测试与测试并发
一.testng并发测试 通过xml文件中suit结点的parallel属性指定,如 <suite name="bundle-module-testabc" parallel ...
- 【27.85%】【codeforces 743D】Chloe and pleasant prizes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- [Elm] Installing and setting up Elm
Before writing any Elm we need to first install the runtime locally. In this lesson we install the E ...