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