博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Largest Rectangle in a Histogram
阅读量:6541 次
发布时间:2019-06-24

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

Largest Rectangle in a Histogram

 
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.

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 Input

7 2 1 4 5 1 3 34 1000 1000 1000 10000

Sample Output

84000

我先提出两种做法,一种是dp统计向左向右扩展的最大距离

这个之前有个合唱队的

N位同学站成一排,音乐老师要请其中的(N-K)位同学出列,使得剩下的K位同学不交换位置就能排成合唱队形。 

合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1, 2, …, K,他们的身高分别为T1, T2, …, TK,则他们的身高满足T1 < T2 < … < Ti , Ti > Ti+1 > … > TK (1 <= i <= K)。 
你的任务是,已知所有N位同学的身高,计算最少需要几位同学出列,可以使得剩下的同学排成合唱队形。 
Input输入的第一行是一个整数N(2 <= N <= 100),表示同学的总数。第一行有n个整数,用空格分隔,第i个整数Ti(130 <= Ti <= 230)是第i位同学的身高(厘米)。Output输出包括一行,这一行只包含一个整数,就是最少需要几位同学出列。Sample Input

8186 186 150 200 160 130 197 220

Sample Output

4
#include
int main() { int a[105],l[105],r[105],n; scanf("%d",&n); for(int i=1; i<=n; i++) scanf("%d",&a[i]); for(int i=1; i<=n; i++) { l[i]=r[n-i+1]=1; for(int j=1; j
a[j]&&l[i]<=l[j]) l[i]=l[j]+1; if(a[n-i+1]>a[n-j+1]&&r[n-i+1]<=r[n-j+1]) r[n-i+1]=r[n-j+1]+1; } } int ans=0; for(int i=1; i<=n; i++) if(l[i]+r[i]>ans)ans=l[i]+r[i]; printf("%d\n",n-ans+1); return 0;}

 


这个直接这样也可以过的,无非就是乘了个高度和个数

单调栈水过

#include
#include
typedef __int64 LL;const int maxn = 100005;LL a[maxn],sta[maxn],left[maxn];int main(){ int N; while (~scanf("%d",&N)) { if(!N)break; LL res = 0,tmp; memset(sta,0,sizeof(sta)); memset(left,0,sizeof(left)); for (int i = 1; i <= N; i++) scanf("%I64d",&a[i]); a[++N] = -1; int top = 0; for (int i = 1; i <= N; i++) { if (!top||a[i]>a[sta[top-1]]) { sta[top++] = i; left[i] = i; continue; } if (a[i] == a[sta[top-1]]) continue; while (top > 0 && a[i] < a[sta[top-1]]) { top--; tmp = a[sta[top]]*((i-1)- (left[sta[top]]-1)); res = res

 

转载于:https://www.cnblogs.com/BobHuang/p/7363876.html

你可能感兴趣的文章
WSDP
查看>>
Memory Management
查看>>
The Packaging Process in Yocto/OE
查看>>
JQUERY 对 表格中的数据重排序
查看>>
程序员常用借口指南
查看>>
关于PXE网络安装linux系统中碰到的个别问题
查看>>
awk 常用方法
查看>>
Android网络框架实现之【Retrofit+RxJava】
查看>>
Android文件的加密与解密
查看>>
SOAP webserivce 和 RESTful webservice 对比及区别
查看>>
【原】记录一句话
查看>>
Android标题栏,状态栏
查看>>
Windows下安装Memcached for PHP
查看>>
hdu 1040 As Easy As A+B
查看>>
java笔记:SpringSecurity应用(二)
查看>>
php记录代码执行时间
查看>>
【C】strcpy()需谨慎使用;
查看>>
用Adobe Flash Professional CS6创建一个iOS应用程序
查看>>
简简单单几段代码让自己变成最合格的网站管理员
查看>>
Slim Text 0.0.9 发布, 代码开源!
查看>>