POJ 2559 Largest Rectangle in a Histogram -- 动态规划

时间:2022-05-07 00:33:33

题目地址:http://poj.org/problem?id=2559

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:

POJ 2559 Largest Rectangle in a Histogram -- 动态规划

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

如果确定了长方形的左端点L和右端点R,那么最大可能的高度就是min{hi|L <= i < R}。

L[i] = (j <= i并且h[j-1] < h[i]的最大的j)

R[i] = (j > i并且h[j] < h[i]的最小的j)

#include <stdio.h>

#define MAX_N 100000

int n;
int h[MAX_N];
int L[MAX_N], R[MAX_N];
int stack[MAX_N]; long long max(long long a, long long b){
return (a > b) ? a : b;
} void solve(){
//计算L
long long ans = 0;
int t = 0;
int i;
for (i = 0; i < n; ++i){
while (t > 0 && h[stack[t-1]] >= h[i]) t--;
L[i] = (t == 0) ? 0 : (stack[t-1] + 1);
stack[t++] = i;
} //计算R
t = 0;
for (i = n - 1; i >= 0; --i){
while (t > 0 && h[stack[t-1]] >= h[i]) t--;
R[i] = (t == 0) ? n : stack[t-1];
stack[t++] = i;
} for (i = 0; i < n; ++i){
ans = max(ans, (long long)h[i] * (R[i] - L[i]));
}
printf("%lld\n", ans);
} int main(void){
int i;
while (scanf("%d", &n) != EOF && n != 0){
for (i = 0; i < n; ++i)
scanf("%d", &h[i]);
solve();
} return 0;
}

参考资料:挑战程序设计竞赛(第2版)

POJ 2559 Largest Rectangle in a Histogram -- 动态规划的更多相关文章

  1. &lbrack;POJ 2559&rsqb;Largest Rectangle in a Histogram 题解(单调栈)

    [POJ 2559]Largest Rectangle in a Histogram Description A histogram is a polygon composed of a sequen ...

  2. poj 2559 Largest Rectangle in a Histogram 栈

    // poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...

  3. stack&lpar;数组模拟&rpar; POJ 2559 Largest Rectangle in a Histogram

    题目传送门 /* 题意:宽度为1,高度不等,求最大矩形面积 stack(数组模拟):对于每个a[i]有L[i],R[i]坐标位置 表示a[L[i]] < a[i] < a[R[i]] 的极 ...

  4. poj 2559 Largest Rectangle in a Histogram (单调栈)

    http://poj.org/problem?id=2559 Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 6 ...

  5. POJ 2559 Largest Rectangle in a Histogram &lpar;单调栈或者dp&rpar;

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15831 ...

  6. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

  7. POJ 2559 Largest Rectangle in a Histogram&lpar;单调栈)

    传送门 Description A histogram is a polygon composed of a sequence of rectangles aligned at a common ba ...

  8. POJ 2559 Largest Rectangle in a Histogram

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18942   Accepted: 6083 Description A hi ...

  9. 题解报告:poj 2559 Largest Rectangle in a Histogram(单调栈)

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

随机推荐

  1. &lbrack;LeetCode&rsqb; Design Phone Directory 设计电话目录

    Design a Phone Directory which supports the following operations: get: Provide a number which is not ...

  2. 一个简易的MysQL性能查询脚本

    如下: #!/bin/sh mysqladmin -P3306 -uroot -p ext |\ awk -F"|" \ "BEGIN{ count=0; }" ...

  3. Java之方法重载(笔记)

    Java是根据参数类型和个数的不同实现重载. 1.当参数类型是基本类型,但不完全匹配. void test(int i) { } void test(float f) { } public stati ...

  4. git delete repository

  5. &period;net中判断该应用程序是否已经启动&comma;防止重复启动,监控程序启动是否正常

    //获取配置文件中的需要监控项 private static string MonitorServe = ConfigurationSettings.AppSettings["Monitor ...

  6. OC2&lowbar;使用系统协议

    // // Dog.h // OC2_使用系统协议 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxuem ...

  7. &lbrack;Flask&rsqb;学习Flask第三天笔记总结

    from flask import Flask,render_template,request from others import checkLogin app = Flask(__name__) ...

  8. linux&lowbar;根据关键词&lowbar;路径下递归查找code

    1:进入想查找的项目根目录 2:根据关键词查找 find . -name "*" |xargs grep -F '10.26'

  9. 分享一个低配VPS下运行的mysql配置文件

    在各种内存CPU核心只有1/2核,内存只有512M/1G的vps下,内存.CPU.硬盘都不是太充裕.因此主要思路是,禁止吃内存大户innodb引擎,默认使用MyISAM.禁止吃硬盘大户log-bin, ...

  10. Java语言写出水仙花数,

    package com.llh.demo;/** * 水仙花数 * @author llh * */public class Demo14 {    public static void main(S ...