单调队列——Poj Sliding Window

[POJ2823]Sliding Window(单调队列)by_zgx 博客链接 http://blog.youkuaiyun.com/CABI_ZGX/article/details/52701138 立即下载

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 33379 Accepted: 9924
Case Time Limit: 5000MS

Description

An array of size  n ≤ 10 6 is given to you. There is a sliding window of size  k which is moving from the very left of the array to the very right. You can only see the  knumbers in the window. Each time the sliding window moves rightwards by one position. Following is an example: 
The array is  [1 3 -1 -3 5 3 6 7], and  k is 3.
Window positionMinimum valueMaximum value
[1  3  -1] -3  5  3  6  7 -13
 1 [3  -1  -3] 5  3  6  7 -33
 1  3 [-1  -3  5] 3  6  7 -35
 1  3  -1 [-3  5  3] 6  7 -35
 1  3  -1  -3 [5  3  6] 7 36
 1  3  -1  -3  5 [3  6  7]37

Your task is to determine the maximum and minimum values in the sliding window at each position. 

Input

The input consists of two lines. The first line contains two integers  n and  k which are the lengths of the array and the sliding window. There are  n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window at each position, from left to right, respectively. The second line gives the maximum values. 

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

题意:

给定含有n个元素的无序序列a[],和一个整数k,要求求出a[]中,从左向右每连续k个元素组成的序列中的最小值(或最大值),这样的值可能有1个或n-k+1个。

思路:

利用单调队列来求解,需要注意的是,单调队列的最大长度为k。单调队列

代码:

#include <stdio.h>

#define MAXN 1000000

int mq[MAXN+5];		//单调队列,存储元素的索引
int f;				//队首指针
int r;				//队尾指针

int a[MAXN+5];		//数字串
int n;				//数字个数
int k;				//滑动窗口大小

void Push_Asc(int i)	//按升序进队
{
	while (r > f && a[i] < a[mq[r-1]])
	{
		r--;
	}
	mq[r++] = i;
}

void Push_Desc(int i)	//按降序进队
{
	while (r > f && a[i] > a[mq[r-1]])
	{
		r--;
	}
	mq[r++] = i;
}

inline int Front(void)
{
	return mq[f];
}

inline bool IsEmpty(void)
{
	return f == r;
}

inline void Pop(void)
{
	f++;
}

void SlidingWindow(bool ascending)
{
	f = r = 0;	//初始化队列
	void (*Push)(int) = ascending ? Push_Asc : Push_Desc;	//判断单调队列是上升还是下降

	int i;
	for (i = 1; i <= k && i <= n; i++)	//让前k个数进队,k有可能大于n
	{
		Push(i);
	}
	printf("%d", a[Front()]);

	for (; i <= n; i++)
	{
		while (!IsEmpty() && Front() + k <= i)	//弹出不在滑动窗口内的元素
		{
			Pop();
		}
		Push(i);
		printf(" %d", a[Front()]);
	}
	putchar('\n');
}

int main(void)
{
	while (scanf("%d%d", &n, &k) != EOF)
	{
		int i;
		for (i = 1; i <= n; i++)
		{
			scanf("%d", &a[i]);
		}

		SlidingWindow(true);
		SlidingWindow(false);
	}
	return 0;
}

camera 知识速查 本文总结了Camera开发中的常见问题排查方法和关键技术点。主要内容包括:I2C/MIPI不通的排查步骤、时序问题分析方法、Sensor/AF/Flash/PDAF的Bring Up流程、OTP调试方法、PDAF原理及调试、CTS/ITS问题解决思路、AE震荡解决方案、Systrace性能分析方法、Flicker产生原理、Tombstone问题定位等。还涉及V4L2框架解析、问题定位分层方法、Sensor Setting注意事项、时钟信号概念、第三方应用卡顿排查、大光圈概念、Monkey测试方法。 阅读详情

相关推荐

ESXI 7 :创建统信V20_1050d服务器系统虚拟机

记录一下在 X86 架构创建统信V20_1050d 虚拟机遇到的问题。已遇到的问题有①:常规服务器环境下网络连接失败重点:按照桌面版本,有问题处理简单。

guyuelin123的博客 1967

A - Sliding Window(双向队列)

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window moves rightwards by one position. F.

Prime me的博客 241

从放弃到再入门之拉格朗日对偶问题推导

普通同学的解法 无约束条件:求导就可以了 等式约束:代入消元,再求导 不等式约束:分情况讨论(在边界上和不在边界上),分别对应1,2的情况 然而发现,有些情况消元特别复杂,甚至不能求解 聪明同学的解法 发现:在最优点的情况下,约束曲面的法向量和目标函数的梯度反向必相同或相反 拉格朗日乘子法如何理解? (在2维里可以形象的解释为z=f(x,y)在约束条件g(x,y)=c中,极值...

qq_34564612的博客 1万+

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 array to the very right. You can only see the k numbers in the window. Each time the sliding window...

LOVETEDA的博客 352

Golang中使用Qt库(therecipe/qt)+QtDesigner + Goland (一) 环境搭建

搭建的目的只是刚好看到有这么一个模块, 还有给使用Go的人需要用到调试界面的时候用一下,真正开发界面并不建议用,没必要, 还有缺点就是在windows平台下编译偏慢, 还有其他一些UI纯Go开发的UI -

听我一言的博客 1万+

将任意文件转换为Pcap网络数据包传输

一款实用的工具用于将任意文件转换为pcap网络数据包

村中少年的专栏 3188

《leetCode》:Sliding Window Maximum

题目Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time the sliding window

wojiushimogui的博客 590

10、X射线聚焦与光学元件技术解析

本文深入解析了X射线聚焦与光学元件技术,重点讨论了椭圆镜的聚焦原理及局限性,多层镜的设计优化及其突破性应用,以及X射线波导的基础理论和模式求解方法。文章还总结了这些技术的优势、局限及其在材料科学、生物学和医学等领域的广泛应用前景。通过理论推导与技术改进,这些光学元件为X射线成像技术的发展提供了重要支撑。

pz890123的博客 121

【leetcode每日刷题】239. Sliding Window Maximum

Given an arraynums, there is a sliding window of sizekwhich is moving from the very left of the array to the very right. You can only see theknumbers in the window. Each time the sliding window m...

菜菜鸟的博客 177

使用Python实现卷积神经网络(CNN)

卷积神经网络是一种专门用于处理具有网格状拓扑结构数据(如图像、声音)的深度学习模型。CNN的核心组件是卷积层和池化层,它们能够有效地从图像中提取特征并实现空间不变性,使得模型能够对图像中的物体进行识别和分类。我们定义一个简单的卷积神经网络模型,包括卷积层、池化层和全连接层。return x# 创建模型实例我们选择交叉熵损失函数作为分类任务的损失函数,并使用随机梯度下降(SGD)作为优化器。

Echo_Wish 4489

遥感图像地物覆盖分类,数据集制作-分类模型对比-分类保姆级教程

1.新建shp文件地理坐标系保持和影像一致,面类型2.打开属性表3.添加字段这里分类6类,点击添加值添加添加完毕开始人工选地物类型,制作数据集开始标注,标注的时候可以借助谷歌地图来看标记足够多的样本打开面转栅格工具设置nodata值为15。

热爱分享,热爱记录 2524

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 array to the very right. You can only see the k numbers in the window. Each time the sliding window m...

ZTao-z的博客 278

ESD防护设计宝典(十):RE(辐射发射)超标问题排查与解决

RE测试是EMC认证的“头号杀手”。当屏幕上出现一根根超标频谱线时,如何快速定位元凶并有效整改?本讲将带你化身电磁侦探,完成一次完美的RE问题“缉凶”。

m0_56451176的博客 394

[单调队列] poj 2823 Sliding Window

题意: 给你n个数和一个k 把n分成连续的n-k+1个区间 第一行按顺序输出每个区间的最小值,第二行是最大值。 思路: 单调队列的模板题,这里注意的是插入队尾的时候需要二分加速 代码: #include"stdio.h" #include"algorithm" #include"string.h" #include"iostream" #include"queue" #include

Enmity_Dark 709

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

POJ 2823 Sliding Window 单调队列

解题思路: 维护一个递增的单调队列和一个递减的单调队列,基础题。 代码: #include #include #include #include #include #include #include #include #include #include #include #define LL long long #define FOR(i,x,y) for(int i=

蘑菇--走在通往梦想的道路上。 852

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ 2823 Sliding Window(单调队列)

题意  长度为n的数组上有个长度为k的滑窗从左向右移动  求每次移动后滑窗区间的最小值和最大值  输出两行  第一行所有最小值  第二行所有最大值 可以用线段树来做  但是单调队列更简单  单调递增队列: 队尾单调入队(入队元素大于队尾元素时直接入队  否则队尾出队直到队尾元素小于入队元素或者队列为空)  队首队尾都可以出队 求最小值时 先判断队首元素是否在滑窗之内  不在队首就出队  然后

Deng's site \# 2787

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

POJ2823 Sliding Window 单调队列

题目链接 http://poj.org/problem?id=2823 分析 单调队列模板,在从队尾出队时,可以用二分快速找到最终位置,但效果并不明显。 AC代码 #include <cstdio> inline int read() { int num = 0, flag = 1; char c = getchar(); while (c < '0' || c >...

Kevin的博客 255

poj2823 Sliding Window(单调队列)

题意: 给定一个长度为 NNN 的数字序列,要求你求出每个长度为 KKK 的区间内的最小值与最大值。 单调队列做法: const int N = 1e6 + 10; const int mod = 998244353; int a[N], maxx[N], minn[N], queue1[N], queue2[N];//queue存位置 int i, n, k, front1, front2, tail1, tail2, cnt; int main() { sdd(n, k); cn..

邵光亮的博客 14万+

ACM-单调队列之Sliding Window——poj2823

ACM 单调队列 Sliding Window poj2823 单调队列详解

继续激情,继续奋斗。 2507

[POJ2823]Sliding Window(单调队列)

[POJ2823]Sliding Window(单调队列)-ZGX

私は Mocha!! 692

POJ - 2823 Sliding Window(单调队列)

题目链接:https://vjudge.net/problem/POJ-2823 题意:给定一个数列,从左至右输出每个长度为k的数列段内的最小数和最大数。 思路:单调队列维护两端,它的头端可以出数,尾部可以进数,通常用于解决局部性的最值问题。单调栈只维护一端,通常维护全局的单调性。单调栈大小没有上限,而单调队列通常有大小限制。所以本题选择用单调队列解。 代码中数组的解释: 数组用来表示一个单调队列 数组表示单调队列里每个元素在原数组中的下标 本题只需维护两个队列(单调递减队列,头部肯定是最大值,.

sugarbliss 441

poj2823Sliding Window——单调队列

题目:http://poj.org/problem?id=2823 单调队列模板。 代码如下: #include<iostream> #include<cstdio> using namespace std; int n,k,a[1000005],mx[1000005],mn[1000005]; int main() { scanf("%d...

aodan5477的博客 117

poj2823 Sliding Window 单调队列

Sliding Window Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k n

ly59782的博客 403

poj 2823 Sliding Window 单调队列

Language: Default Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 36485   Accepted: 10805 Case Time Limit: 5000MS Description An arr

AC_kereo的专栏 798

POJ - 2823 Sliding Window 单调队列

题目链接:http://poj.org/problem?id=2823 题目分析 单调队列问题的经典例题,题意:有一串数,编号为 1- n , 问从 1 开始,求当前位置及其后共k个数中的最大值和最小值,每次位置向后移动一位,如图: n = 8 , k = 3, v[] = { 1, 3 , -1 , -3 , 5 , 3 , 6 , 7 } 解题思路: 首先,这个题目肯...

天际苍穹的博客 192

[POJ 2823]Sliding Window[单调队列]

题目链接:[POJ 2823]Sliding Window[单调队列] 题意分析: 包含n个数字的数列,用一个长度为k的窗口移动从1开始向右移动,每次输出窗口中的最小值和最大值。分两行输出结果:最小值输出到上行,最大值输出到下行。 解题思路: 考虑每次移动窗口要进行的操作,总共有两种,增加一个数,删去一个数。考虑到需要涉及到删除操作,我们使用单调队列来进行维护。 L代表队列的左端,R代表

闲停 571
上一篇: 单调队列
下一篇: 优先队列——合并果子
庞老板
博客等级 码龄15年 127粉丝 66原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值