POJ 2559 Program C
Description
Usually, histograms are used to represent discrete distributions, e.g., the frequencies of characters in texts. Note that the order of the rectangles, i.e., their heights, is important. Calculate the area of the largest rectangle in a histogram that is aligned at the common base line, too. The figure on the right shows the largest aligned rectangle for the depicted histogram.
Input
Output
Sample Input
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
0
Sample Output
8
4000
- #include <iostream>
- #include <cstdio>
- using namespace std;
- const int N = 100005;
- struct Elem
- {
- int height;
- int count;
- };
- Elem stack[N];
- 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;
- }
POJ 2559 Program C的更多相关文章
- [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
http://poj.org/problem?id=2559 题意:就是找出可以完整连接的最大的矩形面积. 思路:找出单独的一块矩形,往两边延伸,记录两边的比他高的矩形是在哪个位置,然后最右的位置减去 ...
- 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 (单调栈)
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 [题目大意] 给出一些宽度为1的长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题 ...
- 【POJ 2559】 Largest Rectangle in a Histogram
[题目链接] http://poj.org/problem?id=2559 [算法] 单调栈 [代码] #include <algorithm> #include <bitset&g ...
- 题解 POJ 2559【Largest Rectangle in a Histogram】(单调栈)
题目链接:http://poj.org/problem?id=2559 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...
随机推荐
- Android 异步加载图片,使用LruCache和SD卡或手机缓存,效果非常的流畅
Android 高手进阶(21) 版权声明:本文为博主原创文章,未经博主允许不得转载. 转载请注明出处http://blog.csdn.net/xiaanming/article/details ...
- 【服务器防护】VPN的ip变更,导致无法连接服务器,解决方法【阿里云ECS】
在阿里云的管理控制台,云服务器ECS - 对应服务器 - 选“管理” - “连接管理终端” 通过这个入口,可以进入Linux云服务器,修改防火墙限制的IP即可
- 转:Unicode汉字编码表
转自:http://blog.csdn.net/huangxy10/article/details/10012119 Unicode汉字编码表 1 Unicode编码表 Unicode只有一个字符集 ...
- 安卓App热补丁动态修复技术介绍
版权声明:本文由johncz原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/169 来源:腾云阁 https://www.q ...
- SAP 物料主数据屏幕增强
1. 用事务代码OMT3C,(或者用SPRO进入,后勤-常规->物料主记录->配置物料主记录->创建定制子屏幕的程序)创建函数功能组,如ZMGD1,然后保存. 2. 用事务代码SE8 ...
- 【hdu2196】Computer
hdu 2196 computer 题意 给你一棵树,边有权值. 对于每一个点,求其与其距离最远的点的距离. 分析 思路1:树的直径 利用直径的性质进行求解,网上资料很多,这里不赘述. #includ ...
- 小例子(二)、winform窗体间的关系
写一个关于winform窗体间的关系 1.登陆,思路:登陆后隐藏登陆窗体,关闭Form2时结束整个应用程序. //登陆窗体 private void button2_Click(object send ...
- VMware Workstation 无法连接到虚拟机
"This PC(我的电脑)":右键"manage(管理)": "Service and Applications(服务和应用)":&quo ...
- 148. Sort List -- 时间复杂度O(n log n)
Sort a linked list in O(n log n) time using constant space complexity. 归并排序 struct ListNode { int va ...
- div中的字符换行
div中的字符换行 转载自:http://blog.sina.com.cn/s/blog_6a79bc480100tizi.html 1.强制不换行,同时以省略号结尾. <div style ...