1 条题解
-
0
解题思路
这个问题要求我们在给定的多个矩形中,找到可以放置的最大矩形的面积。这些矩形按照给定的顺序排列,每个矩形的宽度为,高度为。我们需要找到一个矩形,其宽度和高度可以任意调整,但必须完全包含在给定的矩形组合中。
关键点在于如何高效地计算这些矩形的组合中能够容纳的最大矩形面积。由于矩形的排列顺序固定,我们需要考虑每个矩形的高度以及其相邻矩形的高度,以确定可以扩展的最大宽度。
解题方法
单调栈的应用:使用单调栈来维护一个递增的高度序列。这样可以在遍历每个矩形时,快速找到可以向左或向右扩展的最大宽度。
动态计算面积:对于每个矩形,计算以当前矩形的高度为高的最大矩形面积。这需要知道当前矩形可以向左右两侧扩展的最大宽度。
处理剩余栈元素:在遍历完所有矩形后,栈中可能还剩下一些矩形,需要对这些矩形进行同样的面积计算,确保所有可能的矩形都被考虑到。
C++代码实现:
#include <iostream> #include <memory.h> #include <vector> #include <map> #include <algorithm> #include <cstdio> #include <math.h> #include <queue> #include <string> #include <stack> using namespace std; typedef long long LL; const int MAXN = 5e4 + 10; struct Squere { int _w; int _h; }squ[MAXN]; int main() { int n; while (scanf("%d", &n)) { if (n == -1) break; for (int i = 1;i <= n;i++) scanf("%d%d", &squ[i]._w, &squ[i]._h); stack<Squere> r; int res = 0; for (int i = 1;i <= n;i++) { int tmp = 0; while (!r.empty() && r.top()._h > squ[i]._h) { Squere f = r.top(); r.pop(); res = max(res, f._h * (f._w + tmp)); squ[i]._w += f._w; tmp += f._w; } r.push(squ[i]); } while (!r.empty()) { Squere f = r.top(); r.pop(); res = max(res, f._h * f._w); if (!r.empty()) r.top()._w += f._w; } printf("%d\n", res); } return 0; }
- 1
信息
- ID
- 1083
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 1
- 已通过
- 1
- 上传者