poj 2559 单调栈 ***
2015-07-23 16:45
369 查看
给出一系列的1*h的矩形,求矩形的最大面积。
如图:
题解链接:点我
如图:
题解链接:点我
#include <iostream> #include <cstdio> using namespace std; const int N = 100005; struct Elem { int height; int count; }; Elem stack ; int top; int main() { int height, n; long long ans, tot, tmp; while (scanf("%d", &n) != EOF && n) { top = 0; ans = 0; for (int i = 0; i < n; ++i) { scanf("%d", &height); tmp = 0; while (top > 0 && stack[top - 1].height >= height) { tot = stack[top - 1].height * (stack[top - 1].count + tmp); if (tot > ans) ans = tot; tmp += stack[top - 1].count; --top; } stack[top].height = height; stack[top].count = 1 + tmp; ++top; } tmp = 0; while (top > 0) { tot = stack[top - 1].height * (stack[top - 1].count + tmp); if (tot > ans) ans = tot; tmp += stack[top - 1].count; --top; } printf("%lld\n", ans); } return 0; }
相关文章推荐
- Java 8 Lambda实现原理分析
- 关于storm的complete latency
- Linux中的开启端口供外网访问
- Git pull 冲突解决方案
- 【C语言】关于atoi,itoa与itob的重写和字符统计
- 太阳系
- 与人为善,于己为善
- git 冲突
- 关于运行时,获取私有变量和私有方法
- swift extension
- 虚拟机Ubuntu14.04+Caffe+libsvm对数据集进行分类
- 面向对象设计六大基本原则
- Matlab 2013a 和 VS2010 混合编程
- 链式队列
- sql 与linq的转换
- Canvas createRadialGradient API
- 通过微信扫描获取用户信息登陆网站
- Zabbix mstmp+Mutt发邮件注意权限
- 根据数据的父子关系创建树形结构并实现遍历
- js插件zClip实现复制到剪贴板功能