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 ...
随机推荐
- TypeScript Generics(泛型)
软件工程的一个主要部分就是构建组件,构建的组件不仅需要具有明确的定义和统一的接口,同时也需要组件可复用.支持现有的数据类型和将来添加的数据类型的组件为大型软件系统的开发过程提供很好的灵活性. 在C#和 ...
- iOS提交后申请加急审核
链接:https://developer.apple.com/appstore/contact/appreviewteam/index.html 在i would like to里选择加急审核 然后填 ...
- CPU
多核处理器 http://baike.baidu.com/link?url=6LwImqyaZqI15gVqcGstOA5S73g-Gj2hakrCbFGc_Jh1NIPPZLkahpuI5OSLoi ...
- linux集群时钟问题
一.ntpd与ntpdate的区别: 摘自:ntpd与ntpdate的区别 - 百事乐 - 博客园 http://www.cnblogs.com/liuyou/archive/2012/07/29/ ...
- Linux中挂载window7的共享文件
window7主机: 设置要共享的文件夹 Linux Fedora: 0 su su root 1 samba-client yum install samba-client 2 cifs-utils ...
- Bash 中 SHLVL 变量为 1000 的时候
SHLVL 环境变量代表 Shell 嵌套执行的深度. $ echo $SHLVL 1 $ bash $ echo $SHLVL 2 $ bash $ echo $SHLVL 3 在 Bash 里,这 ...
- Java开发规范摘录
对于规范的 JAVA 派生类,尽量用 eclipse工具来生成文件格式,避免用手写的头文件/实现文件. 尽量避免一行的长度超过 200 个字符,因为很多终端和工具不能很好处理之.缩进8格 ,impor ...
- Java poi读取,写入Excel,处理row和cell可能为空的情况
首先需要导入包 import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NP ...
- django orm字段和参数
字段 1.models.AutoField 自增列 = int(11) 如果没有的话,默认会生成一个名称为 id 的列,如果要显示的自定义一个自增列,必须将给列设置为主键 primary_key=Tr ...
- Retroactive priority queues
http://erikdemaine.org/papers/Retroactive_TALG/paper.pdf 明天写..大概就是通过一些结论发现这个东西其实就是往最后的集合里加入或删除一些可以被快 ...