原题链接:https://www.acwing.com/problem/content/description/133/

1、暴力法(超时)

package mainimport "fmt"func main() {for {var n intfmt.Scan(&n)if n == 0 {break}var a []intfor i := 0; i < n; i++ {var c intfmt.Scan(&c)a = append(a, c)}ans := 0for i := 0; i < n; i++ {h := a[i]for j := i; j < n; j++ {w := j - i + 1h = min(h, a[j])s := h * wans = max(ans, s)}}fmt.Printf("%d\n", ans)}
}func min(a int, b int) int {if a > b {return b}return a
}func max(a int, b int) int {if a > b {return a}return b
}

2、单调栈

#include <iostream>
#include <algorithm>using namespace std;typedef long long ll;
const int N = 100010;
int h[N], l[N], r[N], s[N];
int n;void get(int a[N]) {int tt = 0;for (int i = 1; i <= n; i ++ ) {while (h[i] <= h[s[tt]]) tt -- ;a[i] = s[tt];s[ ++ tt] = i;}
}int main() {while(cin >> n, n) {h[0] = -1;for (int i = 1; i <= n; i ++ ) cin >> h[i];get(l);reverse(h + 1, h + 1 + n);get(r);ll res = 0;for (int i = 1, j = n; i <= n; i ++, j -- ) {res = max(res, (i - l[i] + j - r[j] - 1ll) * h[j]);}cout << res << endl;}return 0;
}

acwing算法题--直方图中最大的矩形相关推荐

  1. 直方图中最大的矩形(单调栈2)

    题目描述 直方图是由在公共基线处对齐的一系列矩形组成的多边形. 矩形具有相等的宽度,但可以具有不同的高度. 例如,图例左侧显示了由高度为2,1,4,5,1,3,3的矩形组成的直方图,矩形的宽度都为1: ...

  2. AcWing算法题常用代码模板

    一.基础算法 1.常用代码模板 快速排序算法模板 -- 模板题 AcWing 785. 快速排序 算法步骤: ​ ①确定分界点 ​ ②调整区间 ​ ③递归处理左右两段 Tips: ​ ①如果输入数据量 ...

  3. 直方图中最大的矩形(遍历与单调栈)

    给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定的高度为 [ ...

  4. 算法——AcWing算法提高课中代码和题解

    文章目录 第一章 动态规划 (完成情况:64/68) 数字三角形模型 最长上升子序列模型 背包模型 状态机模型 状态压缩DP 区间DP 树形DP 数位DP 单调队列优化DP 斜率优化DP 第二章 搜索 ...

  5. leetcode算法题--矩阵中的幸运数

    原题链接:https://leetcode-cn.com/problems/lucky-numbers-in-a-matrix/ class Solution {public:vector<in ...

  6. acwing算法题--铁路与公路

    原题链接:https://www.acwing.com/problem/content/description/4077/ #include <iostream> #include < ...

  7. acwing算法题--看图做题

    原题链接:https://www.acwing.com/problem/content/3992/ 找规律题 #include<iostream>using namespace std;i ...

  8. acwing算法题--不同的数

    原题链接:https://www.acwing.com/problem/content/3991/ #include <iostream> #include <unordered_m ...

  9. acwing算法题--二维费用的背包问题

    原题链接:https://www.acwing.com/problem/content/8/ #include <iostream>using namespace std;const in ...

最新文章

  1. 【高并发】一个工作了7年的朋友去面试竟然被Thread类虐的体无完肤
  2. 如何用Word 2007写Blog
  3. parseInt(),parseFloat(),parse()
  4. 成员函数 静态变量做默认参数_Scala系列 (二)Scala的独有特性提高开发效率学会之后玩转函数式与OOP!!...
  5. Python语言程序设计基础(2)—— Python程序实例解析
  6. NTC3950-10K温度传感器
  7. 【lucene】Lucene的lukeall工具
  8. SQLServer 维护脚本分享(05)内存(Memory)
  9. iOS 多媒体(一)音频播放
  10. Javascript-放大镜实现原理分析
  11. vue项目引入字体.ttf
  12. c语言创意程序题目,成都scratch趣味编程100例
  13. CellPress | 人工智能在临床试验中的应用
  14. 2020-01-07
  15. 【原】Oracle 11.2.0.1 64bit for RHEL6.0 Server x86_64 静默安装
  16. MSBuild 常用参数
  17. app上架可以使用软件著作权电子版权认证证书,加急3-5工作日
  18. 强项目经理,弱项目总监
  19. WIN7远程桌面连接--发生身份验证错误。要求的函数不受支持
  20. RSA算法中的大素数-素性测试

热门文章

  1. python如何导入txt数据集-终于找到python入门到实践数据集
  2. python读取大文件-python读取大文件
  3. python3语法错误-关于在python3.7当中的语法错误!
  4. 上海交通大学python教材答案-上海交通大学python期末考试样题加解析.doc
  5. python创建新文件-Python创建文件和追加文件内容实例
  6. python是什么编程教程-编程python是什么_谁的Python教程最好?
  7. python爬虫案例-python爬虫案例
  8. python输出数据到excel-使用python将大量数据导出到Excel中的小技巧分享
  9. UVa1344 - Tian Ji -- The Horse Racing(贪心)
  10. python爬虫系列之数据的存储(二):csv库的使用