博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU1506
阅读量:4702 次
发布时间:2019-06-09

本文共 2709 字,大约阅读时间需要 9 分钟。

Largest Rectangle in a Histogram

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 16728    Accepted Submission(s): 4948

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
1 //2016.8.23 2 #include
3 #include
4 #define ll __int64 5 6 using namespace std; 7 8 const int N = 100005; 9 ll h[N];10 int l[N], r[N];//l[i]表示i点向左能够扩展的最远点下标,r[i]表示i点向右最远点下标11 12 int main()13 {14 int n;15 while(cin>>n&&n)16 {17 for(int i = 1; i <= n; i++)18 scanf("%I64d", &h[i]);19 l[1] = 1, r[n] = n;//初始化边界20 for(int i = 2; i <= n; i++)//只要高度比h[i]高,一直向左扩展21 {22 int tmp = i;23 while(tmp>1 && h[tmp-1] >= h[i])tmp = l[tmp-1];24 l[i] = tmp;25 }26 for(int i = n-1; i >= 1; i--)//只要高度比h[i]高,一直向右扩展27 {28 int tmp = i;29 while(tmp
= h[i])tmp = r[tmp+1];30 r[i] = tmp;31 }32 ll ans = 0;33 for(int i = 1; i <= n; i++)34 {35 ll tmp = h[i]*(r[i]-l[i]+1);//矩形面积,高×底36 if(ans < tmp)ans = tmp;37 }38 printf("%I64d\n", ans); 39 }40 41 return 0;42 }

 

转载于:https://www.cnblogs.com/Penn000/p/5800163.html

你可能感兴趣的文章
Prime Game Gym - 101981J(网络流/二分图)
查看>>
Teamwork Gym - 101492E (dp)
查看>>
No Link, Cut Tree! Gym - 101484F(dp)
查看>>
Coprimes Gym - 101492C(bitset)
查看>>
Partial Tree UVALive - 7190(完全背包)
查看>>
『深度应用』NLP机器翻译深度学习实战课程·零(基础概念)
查看>>
『开发技术』Windows极简安装使用face_recognition实现人脸识别
查看>>
『深度应用』NLP命名实体识别(NER)开源实战教程
查看>>
『开发技术』GPU训练加速原理(附KerasGPU训练技巧)
查看>>
『深度应用』NLP机器翻译深度学习实战课程·壹(RNN base)
查看>>
『深度应用』一小时教你上手MaskRCNN·Keras开源实战(Windows&Linux)
查看>>
『王霸之路』从0.1到2.0一文看尽TensorFlow奋斗史
查看>>
『TensorFlow2.0正式版教程』极简安装TF2.0正式版(CPU&GPU)教程
查看>>
Python中单线程、多线程和多进程的效率对比实验
查看>>
Centos 05 系统目录讲解
查看>>
几个多项式的题
查看>>
大爆炸(Big Bang)
查看>>
iOS 按frame截取部分图像
查看>>
iOS 音频开发之CoreAudio
查看>>
C#读写三菱PLC和西门子PLC数据 使用TCP/IP 协议
查看>>