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 ...
随机推荐
- 【原】fiddler修改线上的内容
摘要:当我们线上的代码出bug了,咋办呢?有时候本地的代码跟线上的代码还是运行环境还是有区别的.比如有些封装的方法需要运动到手机上可以调试,而浏览器是无法调试的.如果不想每次修改完再放上到测试环境看效 ...
- git push如何至两个git仓库
分别有仓库 A(github),B(JAE 的 git),本机为C. 假设以 a 仓库作为最终的使用仓库, b为发布仓库.分支都为 dev 第一步,增加远程仓库 git remote add orig ...
- insert into output使用
declare @t table (logId int,customerId int,amount int) insert into log( customerId,amount) output in ...
- webpack-dev-server
webpack-dev-server是一个小型的node.js Express服务器,它使用webpack-dev-middleware中间件来为通过webpack打包生成的资源文件提供Web服务.它 ...
- zabbix3.0.4 邮件告警详细配置
sendEmail是一个轻量级,命令行的SMTP邮件客户端.如果你需要使用命令行发送邮件,那么sendEmail是非常完美的选择:使用简单并且功能强大.这个被设计用在php.bash perl和web ...
- [NHibernate]组件之依赖对象
目录 写在前面 文档与系列文章 组件之依赖对象 一个例子 总结 写在前面 周一至周四一直在成都出差,也一直没有更新博客了,一回到家第一件事就是扒一扒最近博客园更新的文章,然后把想看的收藏了,大概有20 ...
- 【06-23】js动画学习笔记01
<html> <head> <style> * { margin:0; padding:0; } #div1{ width:200px; height:200px; ...
- esnext:最后一个参数后面也允许加逗号了
https://jeffmo.github.io/es-trailing-function-commas 目前是一个 stage 3 的提案,Chakra 和 JSC 已经实现了,它允许我们在函数定义 ...
- [译]Testing Node.js With Mocha and Chai
原文: http://mherman.org/blog/2015/09/10/testing-node-js-with-mocha-and-chai/#.ViO8oBArIlJ 为什么要测试? 在此之 ...
- JDI tutorial (trace example)
Components Debugger Interfaces / |--------------| / | VM | debuggee ----( |--------------| <----- ...