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 3
4 1000 1000 1000 1000
0

Sample Output

8
4000 思路:单调队列板子题,注意在单调性出队的时候统计长度,将新入队的长度变长 提供一组数据 7 4 5 6 2 3 3 3 (14不是12
typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int buf[maxm], q[maxm], len[maxm]; int main() {
ios::sync_with_stdio(false), cin.tie();
int n;
while(cin >> n && n) {
LL ans = ;
for(int i = ; i <= n; ++i) {
cin >> buf[i];
len[i] = ;
}
buf[n+] = ;
int l = , r = -, cnt;
bool flag = false;
for(int i = ; i <= n+; ++i) {
cnt = ; flag = false;
while(l <= r && buf[q[r]] > buf[i]) {
flag = true;
cnt += len[q[r]];
ans = max(ans, 1LL*cnt*buf[q[r]]);
r--;
}
q[++r] = i;
if(flag) len[i] = cnt+;
}
cout << ans << "\n";
} return ;
}

也可以用笛卡尔树来做,注意这里并不是严格的笛卡尔树,因为是有相同权值的节点的,一个点的长度就是他后裔的个数+自身(1),搜索时使用记忆化,不然会T

typedef long long LL;
typedef pair<LL, LL> PLL; const int maxm = 1e5+; int buf[maxm], q[maxm], Left[maxm], Right[maxm], length[maxm]; int getlen(int u) {
if(length[u]) return length[u];
int len = ;
if(Left[u]) len += getlen(Left[u]);
if(Right[u]) len += getlen(Right[u]);
return length[u] = len;
} int main() {
ios::sync_with_stdio(false), cin.tie();
int n;
while(cin >> n && n) {
LL ans = ;
for(int i = ; i <= n; ++i) Left[i] = Right[i] = length[i] = ;
for(int i = ; i <= n; ++i)
cin >> buf[i];
int top = ;
for(int i = ; i <= n; ++i) {
while(top && buf[q[top]] > buf[i]) {
Left[i] = q[top];
top--;
}
if(top)
Right[q[top]] = i;
q[++top] = i;
}
for(int i = ; i <= n; ++i) {
int len = ;
len = getlen(i);
ans = max(ans, 1LL*len*buf[i]);
}
cout << ans << "\n";
} return ;
}

好像dp也可以,兄弟题1505好像需要,补了再来

Day8 - C - Largest Rectangle in a Histogram HDU - 1506的更多相关文章

  1. Largest Rectangle in a Histogram HDU - 1506 (单调栈)

    A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rec ...

  2. V - Largest Rectangle in a Histogram HDU - 1506

    两种思路: 1 单调栈:维护一个单调非递减栈,当栈为空或者当前元素大于等于栈顶元素时就入栈,当前元素小于栈顶元素时就出栈,出栈的同时计算当前值,当前值所包含的区间范围为从当前栈顶元素到当前元素i的距离 ...

  3. HDU 1506 Largest Rectangle in a Histogram (dp左右处理边界的矩形问题)

    E - Largest Rectangle in a Histogram Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  4. HDU 1506 Largest Rectangle in a Histogram set+二分

    Largest Rectangle in a Histogram Problem Description: A histogram is a polygon composed of a sequenc ...

  5. hdu 1506 Largest Rectangle in a Histogram 构造

    题目链接:HDU - 1506 A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  6. HDU——T 1506 Largest Rectangle in a Histogram|| POJ——T 2559 Largest Rectangle in a Histogram

    http://acm.hdu.edu.cn/showproblem.php?pid=1506  || http://poj.org/problem?id=2559 Time Limit: 2000/1 ...

  7. HDU 1506 Largest Rectangle in a Histogram(区间DP)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1506 题目: Largest Rectangle in a Histogram Time Limit: ...

  8. DP专题训练之HDU 1506 Largest Rectangle in a Histogram

    Description A histogram is a polygon composed of a sequence of rectangles aligned at a common base l ...

  9. Hdu 1506 Largest Rectangle in a Histogram 分类: Brush Mode 2014-10-28 19:16 93人阅读 评论(0) 收藏

    Largest Rectangle in a Histogram Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

随机推荐

  1. werkeug的WSGI服务器解析

    werkeug的WSGI服务器解析 1.      WSGI 1.1.    wsgi与flask flask默认的wsgi引用自wekurg 声明app:FLASK对象 app.run() run_ ...

  2. GO第归

    Go 语言递归函数 递归,就是在运行的过程中调用自己. 语法格式如下: func recursion() {    recursion() /* 函数调用自身 */ } func main() {   ...

  3. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:中型和大型设备

    <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - 中型和大型设备</title> &l ...

  4. intellij idea设置(字体大小、背景)

    1. 配置信息说明 Intellij Idea: 2017.2.5 2.具体设置 <1> 设置主题背景.字体大小 File---->Settings----->Appearan ...

  5. pandas读取文件的read_csv()方法

    import pandas as pd pd.read_csv(filepath_or_buffer,header,parse_dates,index_col) 返回数据类型:DataFrame:二维 ...

  6. ArrayQueue(队列)

    code1: #include <stdio.h> #include <conio.h> #include <stdlib.h> #define MAXSIZE 6 ...

  7. WKWebView单个界面添加请求头

    https://www.jianshu.com/p/14b9ea4bf1d4 https://github.com/Yeatse/NSURLProtocol-WebKitSupport/blob/ma ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:强调

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. 1.HTTP与HTTPS区别

    1.HTTP与HTTPS区别 HTTP协议默认采用80端口 HTTPS协议默认采用443 HTTPS相较于HTTP协议更安全一些,采用SSL+安全证书,但是效率低 2.使用代码格式模拟HTTP请求(H ...

  10. 学习笔记(6)- pytext训练会话助手

    https://github.com/facebookresearch/pytext https://pytext.readthedocs.io/en/master/ https://pytext.r ...