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 思路:单调栈 什么是单调栈? 单调栈,顾名思义,就是单调的栈,也就是占中存的东西永远是单调(也就是递增或递减)的 如何实现一个单 ...
随机推荐
- C++——对象和类
最重要的OOP特性: *抽象: *封装和数据隐藏: *多态: *继承: *代码的可重用性: 一.抽象和类 1.类型 指定基本类型完成了三项工作:1).决定数据对象需要的内存数量:2).决定如何解释内存 ...
- 20160805_CentOS6_控制台切换
1. Ctrl + Alt + F1~F6 Ctrl + Alt + F1 是 图形界面(如果装了的话),后面的是 控制台界面 2. 3.
- ifstream,fstream 读写文件问题,read读取字节不够
从网上下的一个读写bmp文件的接口使用了ifstream和fstream. 发现了在使用read读的时候,读取的字节达不到期望的size. 原因是打开文件的时候使用了ios::in 或ios::out ...
- git 10.8
git clone xxxx.git生成一个本地的文件夹acd agit checkout -b abcgit checkout mastergit pull然后数据全部由更新 但是是远程的更新 不能 ...
- python核心编程第六章练习6-8
6-8.列表.给出一个整型值,返回代表该值得英文,比如输入89会返回“eight-nine”.附加题:能够返回符合英文语法规律的新式,比如输入89会返回“eighty-nine”.本练习中的值假定在0 ...
- jq手风琴---点击时列表的左边距逐渐减小
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- validate插件:验证密码没有空格 用户名是5-10位 至少包含数字和大小写字母中的两种字符
//校验密码是否含有空格 jQuery.validator.addMethod("notblank", function(value, element) { var pwdblan ...
- js高级程序设计(四)变量、作用域和内存问题
基本类型和引用类型的值 ECMAScript 变量可能包含两种不同数据类型的值:基本类型值和引用类型值.基本类型值指的是 Undefined . Null . Boolean . Number 和 S ...
- css--block formatting context
block formatting context(块级格式化上下文) 如何产生BFC:当一个HTML元素满足下面条件的任何一点,都可以产生Block Formatting Context: float ...
- Qt之窗口动画(下坠、抖动、透明度)
简述 前面几节中我们介绍了关于动画的基本使用,有属性动画.串行动画组.并行动画组.这节我们来实现一些特效,让交互更顺畅. 简述 示例 效果 源码 更多参考 示例 下面,我们以geometry.pos. ...