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 数据库升级解决方案
转自:http://blog.csdn.net/leehong2005/article/details/9128501 请考虑如下情况: 在数据库升级时,不同版本的数据库,他们定义的表结构完全可能是不 ...
- C#_抓包HttpWebRequest跟HttpWebResponse
1.第一招,根据URL地址获取网页信息 这招是入门第一式, 特点: 1.最简单最直观的一种,入门课程. 2.适应于明文,无需登录,无需任何验证就可以进入的页面. 3.获取的数据类型为HTML文档. ...
- HBase之创建表
import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...
- javascript强制转换详解
转换成数值 Number函数强制转换成数值 数值->转换成原来的值 字符串->如果可以解析为数值,则转换成数值:否则转换成NaN或者0 true->1,falSe->0 und ...
- Jni碰到的一个异常
Java与C++都有String对象,而c没有,只有char类型,所以在向C传入String类型的时候,如何处理需要注意一点 jstring Java_com_skymaster_hs_test4_M ...
- VC++ 中使用 std::string 转换字符串编码
目录 第1章说明 1 1.1 代码 1 1.2 使用 4 第1章说明 VC++中宽窄字符串的相互转换比较麻烦,借助std::string能大大减少代码量. 1.1 代码 函数声明如下 ...
- java 多线程8(守护线程)
比如:后台偷偷运行的那些,qq下载更新包 如果一个进程中只剩下了守护线程,那么守护线程也会死亡.. 一个线程默认都不是守护线程. 判断是否是守护线程:例:d.isDaemon(); 当一个线程随着你的 ...
- java 模板
模板模式: 解决某类事情的步骤有些是固定的,有些是会发生变化的,这时我们提供 一个模板代码,从而提高效率. 模板模式的作用: 1.解决这类事情其中一件的解决方案. 2.分析代码,把发生变化的代码抽象取 ...
- dedecms 模板文件不存在,无法解析文档"的终极各种解决办法
方法一:[此对应喜欢把模板文件使用".html"的格式,] /include/arc.archives.class.php 556行 if (!preg_match(&qu ...
- 网页上记录鼠标的点击次数和一段有用的php代码,自己学习使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...