POJ 2559 Largest Rectangle in a Histogram 【栈】
来源:互联网 发布:吸入麻醉药的mac 编辑:程序博客网 时间:2024/06/05 14:56
题目链接:http://poj.org/problem?id=2559
题意:有许多矩形,求其中连成最大矩形的面积。
题解:
这题真坑。
记 l[i] r[i] 为 以 i 为高度的建筑可以向左向右延展最长到哪里。
用栈维护,上一次所在的位置,如果栈顶元素所在建筑的高度 大于当前建筑高度,就延展。
但这样还是会被卡T,注意到相邻建筑只有连续两个,可以把 >= 抽成 > ,然后用一个 if 单独判断 == 的情况就可以了。
代码:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <map> // STL#include <string> #include <vector>#include <queue>#include <stack>#define mpr make_pair#define debug() puts("okkkkkkkk")using namespace std;typedef long long LL;const int inf = 1 << 26;int n;int a[300005];int stk[300005];int l[300005], r[300005];int main(){ while( scanf("%d", &n), n ) { int top = 0; for ( int i = 1; i <= n; i ++ ) scanf("%d", &a[i]); a[++ n] = -1; for ( int i = 1; i <= n; i ++ ) { // 对于每个 a[i] ,求这栋建筑高最多能延伸的长度 l[] 和 r[] l[i] = r[i] = i; while(top && a[stk[top]] > a[i]) { l[i] = l[stk[top]]; r[stk[top]] = i-1; -- top; } if (top && a[stk[top]] == a[i]) l[i] = l[stk[top]]; stk[++ top] = i; } LL res = 0; for ( int i = 1; i < n; i ++ ) { res = max(res, (r[i]-l[i]+1LL)*a[i]); } printf("%lld\n", res); } return 0;}
阅读全文
0 0
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 - Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- 【poj 2559】Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- Largest Rectangle in a Histogram(POJ-2559)
- 文件上传设置 弹框选择文件的 格式
- 1.Arduino点亮LED灯
- 编程题
- Qt 信号和槽机制 优点 效率的详解
- 异常值检测
- POJ 2559 Largest Rectangle in a Histogram 【栈】
- git与svn
- 最可爱的最可爱的可爱的留言板ヽ(^ω^)ノ
- 通信码元速率和带宽理解
- 1、什么是RUP? 2、什么是极限编程XP? 3、什么是敏捷过程?
- C# Time.deltaTime 增量时间
- 1065. 单身狗(25)
- DFS解207. Course Schedule(判断有向图是否存在环)
- 从地球看太空