首页 技术 正文
技术 2022年11月21日
0 收藏 573 点赞 3,449 浏览 2312 个字

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 DescriptionA 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:
Largest Rectangle in a 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. InputThe 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. OutputFor 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 Input7 2 1 4 5 1 3 34 1000 1000 1000 10000 Sample Output84000 SourceUniversity of Ulm Local Contest 2003

单调栈经典题

 #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
*/
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848