Largest Rectangle in a Histogram POJ
来源:互联网 发布:天津中年同志软件 编辑:程序博客网 时间:2024/06/02 03:22
Largest Rectangle in a Histogram
Time Limit: 1000MS
Memory Limit: 65536KTotal Submissions: 21505 Accepted: 6932Description
A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the heights 2, 1, 4, 5, 1, 3, 3, measured in units where 1 is the width of the rectangles:
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.
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
The input contains several test cases. Each test case describes a histogram and starts with an integern, denoting the number of rectangles it is composed of. You may assume that1<=n<=100000. Then follow n integers h1,...,hn, where0<=hi<=1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is1. A zero follows the input for the last test case.
Output
For each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must be aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 34 1000 1000 1000 10000
Sample Output
84000
Hint
Huge input, scanf is recommended.
Source
Ulm Local 2003
单调栈,打比赛的时候碰到的,结果果断不会,主要是以前没有接触过这类的题型,还是多刷题多思考吧!!!
题目的意思是给你一堆宽度相同的举行,让你从中切割一个最大的矩形。
用单调栈,如果一个进栈元素比栈顶元素的高度要低的话,就要进栈(单调栈顾名思义,栈中元素始终单调),遇到比它小的就要出栈,直到进栈元素比栈顶元素的值要大。
这里给出代码,理解一下,还是比较好理解的
#include<stack>#include<iostream>#include<stdio.h>using namespace std;struct aa{ long long time; long long wid;} x;int main(){ int n; while(scanf("%d",&n)!=EOF&&n) { stack<struct aa>ycq; long long maxx=-1; for(int i=0; i<n; i++) { long long temp; scanf("%lld",&temp); x.wid=temp; int c=0; while(!ycq.empty()&&ycq.top().wid>=x.wid) { if((c+ycq.top().time)*ycq.top().wid>maxx) maxx=(c+ycq.top().time)*ycq.top().wid; c+=ycq.top().time; ycq.pop(); } x.time=c+1; ycq.push(x); } int c=0; while(!ycq.empty()) { if((c+ycq.top().time)*ycq.top().wid>maxx) maxx=(c+ycq.top().time)*ycq.top().wid; c+=ycq.top().time; ycq.pop(); } printf("%lld\n",maxx); }}
0 0
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 - Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ:2556 Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- 【poj 2559】Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- POJ 2559Largest Rectangle in a Histogram
- POJ 2559 Largest Rectangle in a Histogram
- poj:Largest Rectangle in a Histogram
- poj 2559 Largest Rectangle in a Histogram
- LeetCode 142. Linked List Cycle II
- LeetCode | 15. 3Sum(三数和为定值)
- android sdk配置
- Java开发中的23种设计模式详解
- Python源码学习笔记 3 字符串对象
- Largest Rectangle in a Histogram POJ
- 关于JS的逻辑运算题之if的使用
- Hibernate总结笔记之三
- eclipse git 提交时提示 “There are no staged files”
- leetcode 230 Kth Smallest Element in a BST C++
- Python编程:从入门到实践读书笔记-7 用户输入和while循环
- 扫雷小游戏
- 【ZOJ3953 The 17th Zhejiang University Programming Contest F】【贪心 or 费用流】Intervals 删最少线段使得每点最多2次覆盖
- LeetCode- 108/109. Convert Sorted Array/List to Binary Search Tree (JAVA)