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:控件WebView显示网页
WebView可以使得网页轻松的内嵌到app里,还可以直接跟js相互调用. webview有两个方法:setWebChromeClient 和 setWebClient setWebClient:主要 ...
- c++ 对象内存分配和虚函数
1. c++类对象(不含虚函数)在内存中的分布 c++类中有四种成员:静态数据.非静态数据.静态函数.非静态函数. 1. 非静态数据成员放在每个对象内部,作为对象专有的数据成员 2. 静态数据成员被抽 ...
- redis+tomcat+nginx session共享
http://www.cnblogs.com/zhrxidian/p/5432886.html系列课程 简洁明了 http://blog.csdn.net/grhlove123/article/det ...
- VC++多工程项目
目录 第1章 VC++6.0 1 1.1 设置依赖关系 1 1.2 编译顺序 2 1.3 自动连接 3 1.4 静态库 3 1.4.1 嵌入 3 1.4.2 替换 ...
- MYSQL 表结构的修改
-- 修改字段 默认值 ALTER TABLE house_owner ALTER COLUMN addr SET DEFAULT ""; -- 修改字段 名称 COMMENT & ...
- ajax正确的简单封装“姿势”
window.meng = window.meng || {}; (function ($) { function getAjaxDate(url, apikey) { var datas; $.aj ...
- jq layer插件使用
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 简单的Hibernate入门简介
其实Hibernate本身是个独立的框架,它不需要任何web server或application server的支持.然而,大多数的Hibernate入门介绍都加入了很多非Hibernate的东西, ...
- hdu------(4302)Holedox Eating(树状数组+二分)
Holedox Eating Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- 【待整理】PS切图基础教程
http://www.w3cfuns.com/article-442-1-1.html http://www.w3cfuns.com/article-443-1-1.html 其他专题研究: floa ...