POJ 2559 Largest Rectangle in a Histogram ——笛卡尔树
【题目分析】
本来是单调栈的题目,用笛卡尔树可以快速的水过去。
把每一个矩阵看成一个二元组(出现的顺序,高度)。
然后建造笛卡尔树。
神奇的发现,每一个节点的高度*该子树的大小,就是这一块最大的子矩阵的可能解。
用二元组的第一个下标来限制,使它们在一块儿,然后堆的性质又限制了宽度以及高度。
计算,取最值即可。
【代码】
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <vector>
#include <iostream>
#include <queue>
using namespace std;
#define maxn 100005
#define ll long long
int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0'&&ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
}
struct node{
int id,h;
}a[maxn];
int sta[maxn],top=0,ch[maxn][2],fa[maxn],siz[maxn],n,rt;
void dfs(int k)
{
if (!k) return ;
dfs(ch[k][0]);
dfs(ch[k][1]);
siz[k]=siz[ch[k][0]]+siz[ch[k][1]]+1;
}
int main()
{
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;
ch[1][0]=ch[1][1]=fa[1]=0; top=0;
sta[++top]=1;
siz[1]=1;
rt=1;
for (int i=2;i<=n;++i)
{
int flag=0,now;
while (top&&a[sta[top]].h>a[i].h) now=sta[top--],flag=1;
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[a[sta[top]].id][1]=i;
fa[i]=a[sta[top]].id;
ch[i][0]=z;
fa[z]=i;
sta[++top]=i;
}
else
{
fa[now]=i;
rt=i;
ch[i][0]=now;
sta[++top]=i;
}
}
}
dfs(rt);
ll ans=0;
for (int i=1;i<=n;++i) ans=max(ans,(ll)siz[i]*(ll)a[i].h);
printf("%lld\n",ans);
}
}
POJ 2559 Largest Rectangle in a Histogram ——笛卡尔树的更多相关文章
- hdu 1506 Largest Rectangle in a Histogram——笛卡尔树
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1506 关于笛卡尔树的构建:https://www.cnblogs.com/reverymoon/p/952 ...
- [hdu1506 Largest Rectangle in a Histogram]笛卡尔树
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1506 如图,求最大的矩形面积 思路: 笛卡尔树:笛卡尔树是一棵二叉树,树的每个节点有两个值,一个为key, ...
- [POJ 2559]Largest Rectangle in a Histogram 题解(单调栈)
[POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- stack(数组模拟) POJ 2559 Largest Rectangle in a Histogram
题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...
- poj 2559 Largest Rectangle in a Histogram (单调栈)
http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 6 ...
- POJ 2559 Largest Rectangle in a Histogram -- 动态规划
题目地址:http://poj.org/problem?id=2559 Description A histogram is a polygon composed of a sequence of r ...
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
随机推荐
- python2-gst0.10制作静态包的补丁
gst制作成了静态库,而python2的gst有多个动态库引用gst的库 因此,想了一个办法将python2所需要的gst打包成一个单独的共享库 办法就是,将python2_gst所有的.so先制作成 ...
- thinkphp标签
1.volist标签 随后一条不一样的输出<volist name="pagehead" id="vo"> <if condition=&qu ...
- nginx 和 IIS 实现负载均衡
Nginx的作用和优点,这里不必多说,今天主要是nginx负载均衡实验,把做的步骤记录下来,作为一个学习笔记吧,也可以给大家做下参考. 1.Nginx安装 1.下载地址:http://nginx.or ...
- tyvj1193 括号序列
描述 定义如下规则序列(字符串):1.空序列是规则序列:2.如果S是规则序列,那么(S)和[S]也是规则序列:3.如果A和B都是规则序列,那么AB也是规则序列. 例如,下面的字符串都是规则 ...
- mysql数据库 中文乱码
看到一篇很好的文章,转录于此 中文乱码似乎是程序编写中永恒的一个话题和难点,就比如MySQL存取中文乱码,但我想做任何事情,都要有个思路才行,有了思路才知道如何去解决问题,否则,即使一时解决了问题,但 ...
- C#常用类库(100多个)
http://download.csdn.net/download/dxf1213cs/8238153
- jquery--常用的函数2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- jquery写简单的div切换
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- testng 教程
Testng 简介: Testng是一套开源测试框架,是从Junit继承而来,testng意为test next generation,主要有以下特性: annotations 注释,如 @test ...
- C#通过事件跨类调用WPF主窗口中的控件
xaml.cs文件: using System; using System.Timers; using System.Windows; using System.Windows.Forms; name ...