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

本文共 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

你可能感兴趣的文章
double free or corruption (!prev): 0x080644c8 ***
查看>>
在VMware上搭建iPhone开发环境(转)
查看>>
MongoCola使用教程 1 - MongoDB的基本操作和聚合功能
查看>>
2012年3月份30个优秀的jquery插件集合 功能强大
查看>>
公共的Json操作C#类
查看>>
WebService如何调试及测试工具
查看>>
HDU-2091 水题
查看>>
【转】条件编译#ifdef的妙用详解_透彻
查看>>
jQuery.autocomplete 支持中文输入
查看>>
配置ubuntu的mac主题
查看>>
makefile文件的两种常用书写格式(搜索路径式+递归式)
查看>>
Android4.0蓝牙使能的详细解析
查看>>
Freemarker常用技巧(三)
查看>>
Java Gradle入门指南之依赖管理(添加依赖、仓库、版本冲突) (转)
查看>>
关闭注册表编辑器,重新启动计算机后生效。这样资源管理器重启后就不会自动重启了。...
查看>>
Knockout应用开发指南(完整版) 目录索引
查看>>
A example that using JQuery clone
查看>>
随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )的公式对比、实现对比...
查看>>
Android 使用JSON格式与服务器交互 中文乱码问题解决
查看>>
_DataStructure_C_Impl:链串
查看>>