单调队列单调栈

本文介绍了单调栈和单调队列这两种数据结构及其算法思想,通过具体的编程实例展示了如何利用它们解决最大值和最小值查询问题。单调栈用于处理具有单调性的序列,而单调队列则在滑动窗口中寻找最大值和最小值。这两个数据结构在处理动态维护区间最值的问题时非常有效。
摘要由CSDN通过智能技术生成

单调栈算法

单调栈是指一个栈内部的元素是具有严格单调性的一种数据结构,分为单调递增栈和单调递减栈。
单调栈算法,借助单调性处理问题的思想在于及时排除不可能的选项,抱持策略集合的高度有限性和秩序性,从而为我们做出决策提供更多的条件和可能方法。

例题
Feel Good

Bill is developing a new mathematical theory for human emotions. His recent investigations are dedicated to studying how good or bad days influent people’s memories about some period of life.

A new idea Bill has recently developed assigns a non-negative integer value to each day of human life.

Bill calls this value the emotional value of the day. The greater the emotional value is, the better the daywas. Bill suggests that the value of some period of human life is proportional to the sum of the emotional values of the days in the given period, multiplied by the smallest emotional value of the day in it. This schema reflects that good on average period can be greatly spoiled by one very bad day.

Now Bill is planning to investigate his own life and find the period of his life that had the greatest value. Help him to do so.

Input
The first line of the input contains n - the number of days of Bill’s life he is planning to investigate(1 <= n <= 100 000). The rest of the file contains n integer numbers a1, a2, … an ranging from 0 to 106 - the emotional values of the days. Numbers are separated by spaces and/or line breaks.

Output
Print the greatest value of some period of Bill’s life in the first line. And on the second line print two numbers l and r such that the period from l-th to r-th day of Bill’s life(inclusive) has the greatest possible value. If there are multiple periods with the greatest possible value,then print any one of them.

Sample Input
6
3 1 6 4 5 2

Sample Output
60
3 5

AC代码:

#include <cstdio>
#include <stack>
#include <cstring>
using namespace std;

typedef long long ll;
const int N = 1e5 + 10;
ll n, pre[N] = { 0 };
ll l[N], r[N], s[N], a[N], top = 0;

int main()
{
	scanf("%lld", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%lld", &a[i]);
		pre[i] = pre[i - 1] + a[i];
		l[i] = i;
		r[i] = i;
	}
	a[n + 1] = -1;
	for (int i = 1; i <= n + 1; i++)
	{
		while (top && a[s[top]] > a[i])
		{
			r[s[top]] = i - 1;
			l[i] = l[s[top]];
			top--;
		}
		s[++top] = i;
	}
	ll ans = -1, ansr, ansl;
	for (int i = 1; i <= n; i++)
	{
		ll k = (pre[r[i]] - pre[l[i] - 1]) * a[i];
		if (ans < k)
		{
			ans = k;
			ansl = l[i];
			ansr = r[i];
		}
	}
	printf("%lld\n%lld %lld\n", ans, ansl, ansr);
	return 0;
}

单调队列算法

单调队列是指一个队列内部的元素具有严格单调性的一种数据结构,分为单调递增队列和单调递减队列。
单调队列算法的思想也是在决策集合(队列)及时排除不一定是最优解的选择。

例题
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. Following is an example:
The array is [1 3 -1 -3 5 3 6 7], and k is 3.
在这里插入图片描述

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

AC代码:

#include <cstdio>
#include <cstring>
using namespace std;

const int N = 1e6 + 10;
int n, k;
int a[N], max[N], min[N];
int que[N], head, tail;

int main()
{
	scanf("%d %d", &n, &k);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
	}
	for (int i = 1; i <= n; i++)
	{
		while (tail > head && que[head + 1] + k <= i)
			head++;
		while (tail > head && a[que[tail]] > a[i])
			tail--;
		que[++tail] = i;
		if (i >= k)
			min[i - k + 1] = a[que[head + 1]];
	}
	head = tail = 0;
	for (int i = 1; i <= n; i++)
	{
		while (tail > head && que[head + 1] + k <= i)
			head++;
		while (tail > head && a[que[tail]] < a[i])
			tail--;
		que[++tail] = i;
		if (i >= k)
			max[i - k + 1] = a[que[head + 1]];
	}
	for (int i = 1; i <= n - k + 1; i++)
	{
		printf("%d%c", min[i], i == n - k + 1 ? '\n' : ' ');
	}
	for (int i = 1; i <= n - k + 1; i++)
	{
		printf("%d%c", max[i], i == n - k + 1 ? '\n' : ' ');
	}

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值