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 ...
随机推荐
- iOS - 在工程中试玩状态模式
做了一个项目,项目中一个藏品详情界面针对不同用户,和用户所处于的状态的不同,展示的效果和操作的权限都会不同.想到了状态模式,从来没有用过,赶紧学一下然后用一用.期待兴奋 看了这么多的博客,终于找到一个 ...
- 1 云计算系列之云计算概述和KVM虚拟化简介
为什么会出现云之传统数据中学面临的问题 物理服务器的利用率非常低,浪费资源,且资源分配不合理,比如一台服务器CPU使用率不到40%,或者某个应用需要的硬件配置低但是服务器硬件配置高等等. 云计算概念 ...
- Structure And Representation Of MIB Object Names - SNMP Tutorial
30.8 Structure And Representation Of MIB Object Names We said that ASN.1 specifies how to represent ...
- windows系统在python3.5环境下安装mysql组件
折腾了一个多小时,终于把连接Mysql的模块装好了,由于我的环境是python3.5,Mysql官方支持到python3.4,后面google查到有pymysql模快支持python3.5,这个模块是 ...
- C语言基础(7)-float,double,long double类型
1.定义方式 3.14这个就是一个浮点常量,3f是一个浮点类型的常量 float a;//定义了一个浮点类型的小数变量,名字叫a double b;//定义了一个double类型的变量,名字叫b lo ...
- css之display:inline-block
display:inline-block: 作用:将对象呈现为inline对象,但是对象的内容作为block对象呈现.之后的内联对象会被排列在同一行内.比如我们可以给一个link(a元素)inline ...
- 大熊君{{bb}}移动开发之旅(第一季)
一,开篇概述 Hi,大家好!大熊君又和大家见面了,从这篇文章开始我要和大家聊聊移动开发的话题,这部分文章共8季,分别从不同角度来讲解什么是移动开发?移动开发涉及到什么方面的技术点以及移动开发中的常见问 ...
- Pandas-数据整理
Pandas包对数据的常用整理功能,相当于数据预处理(不包括特征工程) 目录 丢弃值 drop() 缺失值处理 isnull() & notnull() dropna() fillna() 值 ...
- multiple merge document
http://www.aspose.com/docs/display/wordsnet/How+to++produce+multiple+documents+during+mail+merge
- PHP获取指定月份的第一天开始和最后一天结束的时间戳函数
<?php /** * 获取指定月份的第一天开始和最后一天结束的时间戳 * * @param int $y 年份 $m 月份 * @return array(本月开始时间,本月结束时间) */ ...