单调栈模板题
题目传送门
奇技淫巧的解法,均摊O(n)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#define int long long
const int N = 100010;
int ans, n;
int a[N];
int l[N], r[N];
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
f = (ch == '-') ? -1 : 1, ch = getchar();
while (isdigit(ch))
x = x * 10 + (ch - '0'), ch = getchar();
return x * f;
}
main()
{
// freopen("in.txt", "r", stdin);
while (1)
{
n = read();
if (n == 0)
break;
ans = 0;
for (int i = 1; i <= n; i++)
a[i] = read(), l[i] = r[i] = i;
for (int i = 1; i <= n; i++)
while (l[i] > 1 && a[l[i] - 1] >= a[i])
l[i] = l[l[i] - 1];
for (int i = n; i >= 1; i--)
while (r[i] < n && a[r[i] + 1] >= a[i])
r[i] = r[r[i] + 1];
for (int i = 1; i <= n; i++)
ans = std::max(ans, (r[i] - l[i] + 1) * a[i]);
std::cout << ans << '\n';
}
return 0;
}
单调栈解法:
(解释部分转自此博客:https://blog.csdn.net/qq1169091731/article/details/52006440)
要想找到里面的最大的面积,一定会有这么一种情况,得出的矩形的高度一定为所包含的某一个高度一致的。所以我们可以对某一个柱子的高度为标准,尽量的向两头扩展,这样就可以找出以它高度为标准的,并包含它本身的最大矩形。然后对每一个柱子都做类似的工作,最后挑出里面最大的矩形。
OK,单从上述所说,一定会有重复的工作,如何剔除重复工作呢?而且什么叫做尽量向两头扩展呢?
重复工作之后再说。先说什么叫尽量向两头扩展。
如:2 1 4
第一个:2,以2为高度为准,向左右两头扩展,它只能想右扩展,因为1比2低了,就不可能扩展到1了。宽度只有1。
第二个:1,以1为高度为准,向左右两头扩展,向左,因为2比1高可以扩展过去;向右,因为4也高于1所以扩展到4,这样以1为高度的矩形宽为3了;
第三个:4,以4为高度为准,向左右两头扩展,向左,因为4比1、2都高,显然不可以扩展过去,这样以4为高度的矩形宽为1了。
所以要将其扩展过去,必须高度不低于当前扩展点的高度。
所以如果我们从第一个开始计算到第n个,计算到 i 时,如果我们可以快速的找出左边第一个(这里第一的意思是离i最近)比 i 的高度小的,就可以完成了向左扩展的工作,而向右扩展,我们本来就是一直向右走,所以直接扩展。这时候就轮到:单调栈出场了!
一直保持单调递增的栈。
思考刚才的例子:
2进栈,左边无比其小的元素,记录其最左扩展位置为1
1准备进栈,因为其进栈就破坏了单调栈的性质(2>1),这时2要出栈,因为1<2也说明了2不可能向右扩展,出栈,计算以2为准的矩形 2*1,然后1才进栈。1进栈前,发现其前一个元素,既是当前的栈顶2,比1高,而且2的左扩展从位置1开始,所以1也有理由从2的左起始位置开始(注意:2比1高),所以2出栈后,1进栈,其左扩展应为2的左扩展位置1;
4准备进栈,因为4>1直接进栈,其左扩展位置只能为3了。
最后要清空栈:4退栈,以为是最右的了,这此时右扩展只能为3了。左右扩展都为3,即是其本身,矩形为4*1;记录其位置以备后需;
1退栈,最右扩展只能是上一个退栈的元素位置,因为其高度比1高(单调栈的性质),所以利用刚才记录的位置,1的左右扩展就为1,3了,矩形1*3;
#include <bits/stdc++.h>
#define int long long
const int N = 100010;
int n;
int a[N];
std::stack<std::pair<int, int> >S;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
f = (ch == '-') ? -1 : 1, ch = getchar();
while (isdigit(ch))
x = x * 10 + (ch - '0'), ch = getchar();
return x * f;
}
main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int Max, m, x, y, temp;
while (1)
{
Max = m = x = y = temp = 0;
n = read();
if (!n)
break;
for (int i = 0; i < n; i++)
{
temp = 0;
x = read(), y = 1;
if (S.empty())
S.push(std::make_pair(x, y));
else if (x <= S.top().first)
{
while (x <= S.top().first && !S.empty())
{
S.top().second += temp;
m = S.top().first * S.top().second;
Max = std::max(m, Max);
temp = S.top().second;
S.pop();
}
y += temp;
S.push(std::make_pair(x, y));
}
else
S.push(std::make_pair(x, y));
}
temp = 0;
while (!S.empty())
{
S.top().second += temp;
m = S.top().second * S.top().first;
Max = std::max(Max, m);
temp = S.top().second;
S.pop();
}
std::cout << Max << '\n';
}
return 0;
}
笛卡尔树解法:
笛卡尔树:
可以用数组构造,一棵树在元素的值上满足堆的性质,位置上中序遍历的顺序和数组的遍历顺序相同,可以在O(n)的时间内构造完毕,非常高效且易懂
把每个点的高度和位置当做一个二元组,构造笛卡尔树,然后发现每个答案的可能解就是每个节点的高度值*子树大小(包含自己在内)
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#define int long long
const int N = 100010;
inline int read()
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
f = (ch == '-') ? -1 : 1, ch = getchar();
while (isdigit(ch))
x = x * 10 + (ch - '0'), ch = getchar();
return x * f;
}
struct Node
{
int id, h;
} a[N];
int sta[N], top, ch[N][2], fa[N], siz[N], n, rt;
void dfs(int k) /* dfs记录子树大小 */
{
if (!k)
return;
dfs(ch[k][0]);
dfs(ch[k][1]);
siz[k] = siz[ch[k][0]] + siz[ch[k][1]] + 1;
}
main()
{
// freopen("in.txt", "r", stdin);
while (scanf("%d", &n) != EOF && n)
{
for (int i = 1; i <= n; i++)
ch[i][0] = ch[i][1] = fa[i] = 0; /* 初始化儿子和父亲节点 */
for (int i = 1; i <= n; i++) /* 二元组记录 */
a[i].h = read(), a[i].id = i;
top = 0, sta[++top] = 1, siz[1] = 1, rt = 1; /* 根节点初始化 */
for (int i = 2; i <= n; i++)
{
bool flag = false;
int now = 0;
while (top && a[sta[top]].h > a[i].h) /* 如果出现的矩形变矮,那么之前比它高的矩形全部退栈 */
now = sta[top--], flag = true; /* 退栈 */
if (!flag) /* 如果可以直接插入,那么就直接记录 */
{
ch[a[sta[top]].id][1] = i; /* 右儿子是新插入的节点 */
fa[i] = a[sta[top]].id; /* 父节点记录 */
sta[++top] = i; /* 进栈 */
}
else
{
if (top)
{
int z = ch[a[sta[top]].id][1]; /* 保存原来的现在要被替换的元素,也就是最近一个退栈的元素,它作为新插入的元素的左左二子 */
ch[i][0] = z; /* 左儿子记录 */
ch[a[sta[top]].id][1] = i; /* 替换新的右儿子 */
fa[i] = a[sta[top]].id; /* 父节点记录 */
fa[z] = i; /* 同上 */
sta[++top] = i; /* 新元素进栈 */
}
else
{
fa[now] = i; /* 如果栈已经退空了,那么说明这个节点是目前为止最小的节点,它要作为根节点 */
rt = i; /* 作为根节点 */
ch[i][0] = now; /* 左二子是最近一个退栈的元素 */
sta[++top] = i; /* 压入栈中 */
}
}
}
dfs(rt); /* 从根节点开始遍历 */
int ans = 0;
for (int i = 1; i <= n; i++)
ans = std::max(ans, siz[i] * a[i].h);
std::cout << ans << '\n';
}
return 0;
}
其实这道题还有第四种解法,用RMQ+分治解决,但蒟蒻实在不想打了(逃)