Largest Rectangle in a Histogram(附上几组测试数据)
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

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.
单调栈经典题
#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(附上几组测试数据)的更多相关文章
- poj 2559 Largest Rectangle in a Histogram - 单调栈
Largest Rectangle in a Histogram Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19782 ...
- 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 ...
- Largest Rectangle in a Histogram(DP)
Largest Rectangle in a Histogram Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K ...
- POJ 2559 Largest Rectangle in a Histogram(单调栈)
传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...
- Largest Rectangle in a Histogram(HDU1506)
Largest Rectangle in a Histogram HDU1506 一道DP题: 思路:http://blog.csdn.net/qiqijianglu/article/details/ ...
- POJ 2559 Largest Rectangle in a Histogram
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18942 Accepted: 6083 Description A hi ...
- Largest Rectangle in a Histogram
2107: Largest Rectangle in a Histogram Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 777 Solved: 22 ...
- HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)
E - Largest Rectangle in a Histogram Time Limit:1000MS Memory Limit:32768KB 64bit IO Format: ...
- hdu---1506(Largest Rectangle in a Histogram/dp最大子矩阵)
Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
随机推荐
- javascript对象讲解
js的数据类型 基本数据类型:string undefined null boolean number 引用数据类型:object 二者的区别: 基本数据类型就是简单的赋值,引用数据 ...
- python3 钉钉群机器人 webhook
import requests import json url='https://oapi.dingtalk.com/robot/send?access_token=替换成你自己的toten' pro ...
- ZooKeeper系列(2) 安装部署 (转)
原文地址:http://www.cnblogs.com/wuxl360/p/5817489.html 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模 ...
- sqoop产生背景及概述
sqoop产生背景 多数是用Hadoop技术处理大数据业务的企业有大量的数据存储在传统的关系型数据库(RDBMS)中:由于缺乏工具的支持.对Hadoop和传统数据库系统中的数据进行相互传输是一件十分困 ...
- 动态html处理和及其图像识别
爬虫(Spider),反爬虫(Anti-Spider),反反爬虫(Anti-Anti-Spider) 之间恢宏壮阔的斗争... Day 1 小莫想要某站上所有的电影,写了标准的爬虫(基于HttpCli ...
- create a bootable USB stick on Ubuntu
https://tutorials.ubuntu.com/tutorial/tutorial-create-a-usb-stick-on-ubuntu?_ga=2.141187314.17572770 ...
- HTML5 Canvas ( 线段端点的样式 ) lineCap
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- leetcode29
class Solution { public int divide(int dividend, int divisor) { if (dividend == Integer.MIN_VALUE &a ...
- web 复制功能和span光标
参考文章:https://www.cnblogs.com/tugenhua0707/p/7395966.html https://blog.csdn.net/woshinia/article/deta ...
- 基于OpenGL编写一个简易的2D渲染框架-08 重构渲染器-整体架构
事实上,前面编写的渲染器 Renderer 非常简陋,虽然能够进行一些简单的渲染,但是它并不能满足我们的要求. 当渲染粒子系统时,需要开启混合模式,但渲染其他顶点时却不需要开启混合模式.所以同时渲染粒 ...