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 ...
随机推荐
- Entity Framework 6 Code First 实践系列(1):实体类配置-根据依赖配置关系和关联
EF实体类的配置可以使用数据注释或Fluent API两种方式配置,Fluent API配置的关键在于搞清实体类的依赖关系,按此方法配置,快速高效合理.为了方便理解,我们使用简化的实体A和B以及A.B ...
- SenTestingKit.framework的报错!
本文转载至http://www.cocoachina.com/ask/questions/show/106912 ld: building for iOS Simulator, but linking ...
- 锁粒度 Deadlocks
锁粒度 MySQL :: MySQL 5.7 Reference Manual :: 14.5.2.4 Locking Reads https://dev.mysql.com/doc/refman/5 ...
- FLTK 简介
FLTK,如同其名字所表达的:The Fast Light Tool Kit,一个轻量级的GUI开发库.但这轻量级并不代表功能的羸弱,相反,FLTK在具有基本的GUI功能之外,还拥有一些特 ...
- mysql 数据库导入错误:40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;/*!40101 SET @OLD_CHARACTER_SET_RES ...
- 用WaveX实现音频文件的录音
原文地址:https://blog.csdn.net/gongluck93/article/details/53096013 1.WaveInOpen waveInOpen MMRESULT wave ...
- 开发工具、Object类(java基础知识十一)
1.常见开发工具介绍 * A:操作系统自带的记事本软件 * B:高级记事本软件 * C:集成开发环境 IDE * (Integrated Development Environment) * ...
- 【NOIP 2003】 加分二叉树
[题目链接] 点击打开链接 [算法] 树形DP即可 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 50 in ...
- MyBatis缓存设计
和大多数ORM框架一样,为了尽可能减少数据库的访问,MyBatis设计支持缓存功能.设计上通过Cache接口提供SPI(服务提供接口),可以让第三方缓存提供具体的缓存实现,比如使用ehcache.Re ...
- 移动web开发-------meta
<meta content=”width=device-width; initial-scale=1.0; maximum-scale=1.0; user-scalable=0″ name=”v ...