Largest Rectangle in a Histogram

http://acm.hdu.edu.cn/showproblem.php?pid=1506

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 23458    Accepted Submission(s): 7362

Problem Description
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.
 
Input
The input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. You may assume that 1 <= n <= 100000. Then follow n integers h1, ..., hn, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles of the histogram in left-to-right order. The width of each rectangle is 1. 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 3
4 1000 1000 1000 1000
0
 
Sample Output
8
4000
 
Source

单调栈经典题

 #include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<string>
#include<vector>
#include<cstdio>
#include<queue>
#include<stack>
using namespace std; struct sair{
long long v;
int pos;
}a[]; int main(){
std::ios::sync_with_stdio(false);
int n;
while(cin>>n){
if(!n) break;
for(int i=;i<=n;i++){
cin>>a[i].v;
a[i].pos=i;
}
stack<sair>st;
long long ans=;
long long cnt,pos;
for(int i=;i<=n;i++){
if(st.empty()){
st.push(a[i]);
ans=max(ans,st.top().v);
}
else{
while(!st.empty()&&st.top().v>=a[i].v){
cnt=st.top().v;
pos=st.top().pos;
st.pop();
if(st.empty()){
ans=max(ans,cnt*(i-));
}
else{
ans=max(ans,cnt*(i--st.top().pos));
}
}
st.push(a[i]);
}
}
int Last;
if(!st.empty()){
Last=st.top().pos;
}
while(!st.empty()){
cnt=st.top().v;
pos=st.top().pos;
st.pop();
if(st.empty()){
ans=max(ans,cnt*n);;
}
else{
ans=max(ans,cnt*(Last-st.top().pos));
}
}
cout<<ans<<endl;
}
system("pause");
}
/*
6 2 5 2 5 5 2
5 1 2 3 4 4
7 2 1 4 5 1 3 3
4 1000 1000 1000 1000
3 2 1 2
4 2 2 5 5
*/

Largest Rectangle in a Histogram(附上几组测试数据)的更多相关文章

  1. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  2. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  3. Largest Rectangle in a Histogram(DP)

    Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K ...

  4. POJ 2559 Largest Rectangle in a Histogram(单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  5. Largest Rectangle in a Histogram(HDU1506)

    Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...

  6. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

  7. Largest Rectangle in a Histogram

    2107: Largest Rectangle in a Histogram Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 777  Solved: 22 ...

  8. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  9. hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. javascript对象讲解

    js的数据类型 基本数据类型:string   undefined   null   boolean    number 引用数据类型:object 二者的区别: 基本数据类型就是简单的赋值,引用数据 ...

  2. python3 钉钉群机器人 webhook

    import requests import json url='https://oapi.dingtalk.com/robot/send?access_token=替换成你自己的toten' pro ...

  3. ZooKeeper系列(2) 安装部署 (转)

    原文地址:http://www.cnblogs.com/wuxl360/p/5817489.html 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模 ...

  4. sqoop产生背景及概述

    sqoop产生背景 多数是用Hadoop技术处理大数据业务的企业有大量的数据存储在传统的关系型数据库(RDBMS)中:由于缺乏工具的支持.对Hadoop和传统数据库系统中的数据进行相互传输是一件十分困 ...

  5. 动态html处理和及其图像识别

    爬虫(Spider),反爬虫(Anti-Spider),反反爬虫(Anti-Anti-Spider) 之间恢宏壮阔的斗争... Day 1 小莫想要某站上所有的电影,写了标准的爬虫(基于HttpCli ...

  6. create a bootable USB stick on Ubuntu

    https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu?_ga=2.141187314.17572770 ...

  7. HTML5 Canvas ( 线段端点的样式 ) lineCap

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. leetcode29

    class Solution { public int divide(int dividend, int divisor) { if (dividend == Integer.MIN_VALUE &a ...

  9. web 复制功能和span光标

    参考文章:https://www.cnblogs.com/tugenhua0707/p/7395966.html https://blog.csdn.net/woshinia/article/deta ...

  10. 基于OpenGL编写一个简易的2D渲染框架-08 重构渲染器-整体架构

    事实上,前面编写的渲染器 Renderer 非常简陋,虽然能够进行一些简单的渲染,但是它并不能满足我们的要求. 当渲染粒子系统时,需要开启混合模式,但渲染其他顶点时却不需要开启混合模式.所以同时渲染粒 ...