stack(单调栈) POJ 2082 Terrible Sets
题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少
分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度都累积到当前的矩形中,这样最后再扫描一遍,算面积很方便,这题应该算是 POJ 2559 的强化版了
收获:stack的应用,求矩形面积,矩阵相乘,表达式计算
代码:
/************************************************
* Author :Running_Time
* Created Time :2015/9/9 星期三 13:50:48
* File Name :L.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 5e4 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
struct M {
int w, h;
}m[N]; int main(void) {
int n;
while (scanf ("%d", &n) == 1) {
if (n == -1) break;
for (int i=1; i<=n; ++i) {
scanf ("%d%d", &m[i].w, &m[i].h);
}
stack<M> S;
int ans = 0, lasth = 0;
for (int i=1; i<=n; ++i) {
if (m[i].h >= lasth) {
S.push (m[i]); lasth = m[i].h;
}
else {
int totw = 0, area = 0;
while (!S.empty () && S.top ().h > m[i].h) {
totw += S.top ().w;
area = totw * S.top ().h;
if (area >= ans) ans = area;
S.pop ();
}
m[i].w += totw;
S.push (m[i]); lasth = m[i].h;
}
}
int totw = 0, area = 0;
while (!S.empty ()) {
totw += S.top ().w;
area = totw * S.top ().h;
if (area >= ans) ans = area;
S.pop ();
}
printf ("%d\n", ans);
} return 0;
}
stack(单调栈) POJ 2082 Terrible Sets的更多相关文章
- POJ 2082 Terrible Sets(单调栈)
[题目链接] http://poj.org/problem?id=2082 [题目大意] 给出一些长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题解] 我们 ...
- POJ 2082 Terrible Sets
Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 2747 Accepted: 1389 Des ...
- POJ 2082 Terrible Sets(栈)
Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...
- PKU 2082 Terrible Sets(单调栈)
题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(0,0). #include<cstdio> #include<stack> ...
- 「面向 offer 学算法」笔面试大杀器 -- 单调栈
目录 前言 单调栈 初入茅庐 小试牛刀 打怪升级 出师试炼 前言 单调栈是一种比较简单的数据结构.虽然简单,但在某些题目中能发挥很好的作用. 最近很多大厂的笔试.面试中都出现了单调栈的题目,而还有不少 ...
- POJ-2081 Terrible Sets(暴力,单调栈)
Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4113 Accepted: 2122 Descrip ...
- Poj 3250 单调栈
1.Poj 3250 Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...
- Terrible Sets_单调栈
Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...
- poj 3415 Common Substrings(后缀数组+单调栈)
http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS Memory Limit: 65536K Total Sub ...
随机推荐
- 杭电 1596 find the safest road (最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的.仅仅只是是把+改成了*,输入输出有些不一样而已. find t ...
- 自定义UISearchDisplayController的“No Results“标签和”Cancel“按钮
本文转载至 http://www.cnblogs.com/pengyingh/articles/2350154.html - (void)searchDisplayControllerWillBegi ...
- eclipse debug configurations arguments指定文件路径参数
1 eclipse debug configurations arguments指定文件路径参数 使用绝对路径,但是这个文件必须要放在该project的源码路径的外面才行,否则eclipse不认这个文 ...
- Spring的声明式事务
1.与hibernate集成 <bean id="sessionFactory" class="org.springframework.orm.hibernate3 ...
- LIS n^2&nlogn模板
LIS nlogn模板 http://acm.hdu.edu.cn/showproblem.php?pid=1950 #include <iostream> #include <st ...
- Android ADB实现解析【转】
本文转载自:http://blog.csdn.net/u010223349/article/details/41120255 ADB是Android系统提供的调试工具,整个ADB工具由三部分组成: ...
- Android ViewDragHelper及移动处理总结
概述 2013年谷歌i/o大会上介绍了两个新的layout: SlidingPaneLayout和DrawerLayout,现在这俩个类被广泛的运用.我们知道在我们实际的开发中往往会涉及到很多的拖动效 ...
- Python中的sort() key含义
sorted(iterable[, cmp[, key[, reverse]]]) iterable.sort(cmp[, key[, reverse]]) 参数解释: (1)iterable指定要排 ...
- 禁止屏幕旋转并同时解决以至于导致Activity重启的方法
1.禁止屏幕旋转在AndroidManifest.xml的每一个需要禁止转向的Activity配置中加入android:screenOrientation属性. //landscape(横向)port ...
- hdu-5675 ztr loves math(数学)
题目链接: ztr loves math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...