Sliding Window POJ - 2823
Description
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
Window position | Minimum value | Maximum value |
---|---|---|
[1 3 -1] -3 5 3 6 7 | -1 | 3 |
1 [3 -1 -3] 5 3 6 7 | -3 | 3 |
1 3 [-1 -3 5] 3 6 7 | -3 | 5 |
1 3 -1 [-3 5 3] 6 7 | -3 | 5 |
1 3 -1 -3 [5 3 6] 7 | 3 | 6 |
1 3 -1 -3 5 [3 6 7] | 3 | 7 |
Your task is to determine the maximum and minimum values in the sliding window at each position.
Input
Output
Sample Input
8 3
1 3 -1 -3 5 3 6 7
Sample Output
-1 -3 -3 -3 3 3
3 3 5 5 6 7
#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;
const int MAXN=1e6+;
int Q[MAXN],a[MAXN];
vector<int>MAX;
vector<int>MIN;
int n,m;
void get_MIN()
{
int head=,tail=;
Q[tail]=;
for (int i = ; i <=n ; ++i) {
while(head<=tail&&a[i]<a[Q[tail]]) tail--;
Q[++tail]=i;
while(head<=tail&&Q[head]<i-m+) head++;
if(i>=m)
MIN.push_back(a[Q[head]]);
}
} void get_MAX()
{
int head,tail;
head=;tail=;
Q[tail]=;
for (int i = ; i <=n ; ++i) {
while(head<=tail&&a[i]>a[Q[tail]]) tail--;
Q[++tail]=i;
while(head<=tail&&Q[head]<i-m+)
head++;
if(i>=m)
MAX.push_back(a[Q[head]]);
}
} int main()
{
scanf("%d%d",&n,&M);
for (int i = ; i <=n ; ++i) {
scanf("%d",&a[i]);
}
get_MIN();
for (int i = ; i <MIN.size() ; ++i) {
printf("%d ",MIN[i]);
}
printf("\n");
get_MAX();
memset(Q,, sizeof(Q));
for (int i = ; i <MAX.size() ; ++i) {
printf("%d ",MAX[i]); }
printf("\n");
return ;
}
Sliding Window POJ - 2823的更多相关文章
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- AC日记——Sliding Window poj 2823
2823 思路: 单调队列: 以前遇到都是用线段树水过: 现在为了优化dp不得不学习单调队列了: 代码: #include <cstdio> #include <cstring> ...
- 单调栈(G - Sliding Window POJ - 2823 )
题目链接:https://cn.vjudge.net/contest/276251#problem/G 题目大意:给你n和m,然后问你对于(m,n)这中间的每一个数,(i-m+1,i)这个区间的最小值 ...
- sliding windows (poj 2823) 题解
[问题描述] 给你一个长度为N的数组,一个长为K的滑动的窗体从最左移至最右端,你只能见到窗口的K个数,每次窗体向右移动一位,如下表: [样例输入] 8 3 1 3 -1 -3 5 3 6 7 [样例输 ...
- poj 2823 Sliding Window (单调队列入门)
/***************************************************************** 题目: Sliding Window(poj 2823) 链接: ...
- POJ 2823 Sliding Window + 单调队列
一.概念介绍 1. 双端队列 双端队列是一种线性表,是一种特殊的队列,遵守先进先出的原则.双端队列支持以下4种操作: (1) 从队首删除 (2) 从队尾删除 (3) 从队尾插入 (4) ...
- POJ 2823 Sliding Window 题解
POJ 2823 Sliding Window 题解 Description An array of size n ≤ 106 is given to you. There is a sliding ...
- 洛谷P1886 滑动窗口(POJ.2823 Sliding Window)(区间最值)
To 洛谷.1886 滑动窗口 To POJ.2823 Sliding Window 题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每 ...
- POJ 2823 Sliding Window ST RMQ
Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is m ...
随机推荐
- jquery.validate+jquery.form表单验证提交
1.通过jquery.validate的submitHandler选项,即当表单通过验证时运行回调函数.在这个回调函数中通过jquery.form来提交表单: <script type=&quo ...
- Java学习笔记--类和对象
1.介绍面向对象的编程 面向对象是现在主流的编程样例,它替代了以前C语言使用时的“结构体”,Java是一门面向对象的语言,所以需要熟悉面向对象的概念.面向对象的程序由很多对象组成,每 ...
- css 03
DIV+CSS盒子模型 一.盒子模型css height width padding 内边距 margin 外边距 border 1.margin 外边距 margin-top:15px; marg ...
- bank conflct 一句话总结
由于最新的多播模式区别于原来的广播模式,原来同一个warp不同线程访问同一个bank的相同地址不再是bank conflict, 现在总结为:只要同一个 warp 的不同线程会访问到同一个 bank ...
- Android 超简单的拖动按钮 悬浮按钮 吸附按钮
第一种 第二种 第一种实现方法 xml布局 <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...
- SpringBoot JUnit4的断言和注解
Junit4的断言常用方法: assertArrayEquals( new Object[]{ studentService.likeName("小明2").size() > ...
- VS功能扩展--扩展介绍
使用Eclipse的朋友都知道Eclipse是一个完全可扩展的IDE,那么在windows程序开发时,我们常使用的IDE(Visual studio)是否具有功能的扩展性呢?毫无疑问,回答是肯定的.我 ...
- centOS7虚拟机连接大网
1.启动vm服务 如果遇到无法启动时,需要还原vm默认配置解决 2.更改vm设置为NAT模式 3.centOS开启DHCP
- GitLab-CE-8.9.4 (OpenLogic CentOS 7.2)
平台: CentOS 类型: 虚拟机镜像 软件包: gitlab-8.9.4 bug tracking collaboration commercial development devops git ...
- python内存泄露的诊断(转)
本篇文章非原创,转载自:http://rstevens.iteye.com/blog/828565 . 对于一个用 python 实现的,长期运行的后台服务进程来说,如果内存持续增长,那么很可能是有了 ...