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 ...
随机推荐
- Raspberry Pi 3 --- GPIO control
Before input 'gpio readall', need install wiringPi download "wiringPi":git clone git://git ...
- ng-switch
<p>ng-switch : </p> <div ng-switch="isShow"><!--isShow是boolean值--> ...
- 分享一个.NET实现的简单高效WEB压力测试工具
在Linux下对Web进行压力测试的小工具有很多,比较出名的有AB.虽然AB可以运行在windows下,但对于想简单界面操作的朋友有点不太习惯.其实vs.net也提供压力测试功能但显然显得太重了,在测 ...
- 在Application中集成Microsoft Translator服务之开发前准备
第一步:准备一个微软账号 要使用Microsoft Translator API需要在Microsoft Azure Marketplace(https://datamarket.azure.com/ ...
- Solr的主界面加登录权限
如题:效果如下图zu 只需两步: 1.tomcat-users.xml 下添加 <user username="admin" password="new-pas ...
- codevs1021 玛丽卡
题目描述 Description 麦克找了个新女朋友,玛丽卡对他非常恼火并伺机报复. 因为她和他们不住在同一个城市,因此她开始准备她的长途旅行. 在这个国家中每两个城市之间最多只有一条路相通,并且我们 ...
- codevs1316 文化之旅
题目描述 Description 有一位使者要游历各国,他每到一个国家,都能学到一种文化,但他不愿意学习任何一种文化超过一次(即如果他学习了某种文化,则他就不能到达其他有这种文化的国家).不同的国家可 ...
- NOSDK--一键打包的实现(一)
所谓一键打包,包含五个流程: 刷新mk,这个只有在文件数目改变的时候才会需要: 编译,在实现了统一接入以后,只需要编译一次就可以打多个包,这个以后再介绍: 拷贝资源,这个使用的是cocos2d-x自带 ...
- JSP动作元素之useBean、setProperty、getProperty指令
简介 这三个指令都是与JavaBean相关的指令,其中useBean指令用于在JSP页面中初始化一个Java实例:setProperty指令用于为JavaBean实例的属性设置值:getPropert ...
- log4jdbc 使用
像 hibernate, mybatis 都是不打印真正执行的sql的,所以借助 log4jdbc 打印sql以及统计执行时间是非常方便的. 配置log4jdbc 有很多种方法,比如通过spring的 ...