题目链接:HDU - 1506

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.
题意描述:给出一些宽度为1,高度大于等于0的矩形,求出最大的矩形面积。
算法分析:看到n的范围,就确定不能n*n了,想了想,对于一个矩形i ,找到最左方和最右连续都比它高的位置之后,对于矩形i+1来说,如果i+1高度比i小,那么对i+1来往左扩展找出左方连续比它高的最左方的位置时候,就没有必要再次比较i+1和i、i+1和i-1、、、因为前面有一部分由 i 已经比较过了,所以我们只需要从上次的记录的位置开始往左比较即可。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long LL;
const int maxn=+; int n;
int l[maxn],r[maxn],an[maxn]; int main()
{
while (scanf("%d",&n)!=EOF && n)
{
for (int i= ;i<=n ;i++) {scanf("%d",&an[i]);l[i]=r[i]=i;}
an[]=an[n+]=-;
l[]=l[n+]=r[]=r[n+]=;
for (int i= ;i<=n ;i++)
{
while (an[l[i]- ]>=an[i])
l[i]=l[l[i]- ];
}
for (int i=n ;i>= ;i--)
{
while (an[r[i]+ ]>=an[i])
r[i]=r[r[i]+ ];
}
LL ans=;
for (int i= ;i<=n ;i++)
{
LL area=(LL)(r[i]-l[i]+)*(LL)an[i];
if (area>ans) ans=area;
}
printf("%I64d\n",ans);
}
return ;
}
 

最新文章

  1. ArcGIS Engine开发之属性查询
  2. jquery基本操作笔记
  3. 计划将项目中使用entity framework的要点记录到改栏目下
  4. twitter storm源码走读之2 -- tuple消息发送场景分析
  5. AMQP协议
  6. 转: utf16编码格式(unicode与utf16联系)
  7. VS2010手动添加外部工具和快捷键
  8. python 读写文本文件
  9. Dapper基本增删改查
  10. hdu 1242 Rescue(bfs)
  11. MySQL+SSM+Ajax上传图片问题
  12. Out of mind - 魔术纸
  13. java中表示二进制、八进制、十进制、十六进制
  14. sublime 挪移的第一层(插件的安装和使用)
  15. 删除表中的所有记录 ID从1开始
  16. Unity 读取资源(图片)
  17. socket(TCP-粘包)通讯之Python实现
  18. 逆元知识普及(进阶篇) ——from Judge
  19. 利用Clang(Python接口)来解析C++
  20. iReport 5.6.0 PDF导出中文不显示问题 解决方案

热门文章

  1. KMP与循环节相关题目
  2. [转]Android的网络与通信
  3. warning MSB3162: 所选的“Microsoft Report Viewer 2012 Runtime”项需要“Microsoft.SqlServer.SQLSysClrTypes.11.0”。在“系统必备”对话框中选择缺少的系统必备组件,或者为缺少的系统必备组件创建引导程序包。
  4. 学习go语言第一天
  5. 思梦PHP-阿里大鱼手机验证码
  6. [洛谷P2216][HAOI2007]理想的正方形
  7. 原 HBase 常用Shell命令
  8. BZOJ4031 [HEOI2015]小Z的房间 【矩阵树定理 + 高斯消元】
  9. Codeforces Round #359 (Div. 2) C
  10. 2.LXC和namespace介绍