题目传送门

题意:紧贴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的更多相关文章

  1. POJ 2082 Terrible Sets(单调栈)

    [题目链接] http://poj.org/problem?id=2082 [题目大意] 给出一些长方形下段对其后横向排列得到的图形,现在给你他们的高度, 求里面包含的最大长方形的面积 [题解] 我们 ...

  2. POJ 2082 Terrible Sets

    Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2747   Accepted: 1389 Des ...

  3. 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 ...

  4. PKU 2082 Terrible Sets(单调栈)

    题目大意:原题链接 一排紧密相连的矩形,求能构成的最大矩形面积. 为了防止栈为空,所以提前加入元素(0,0). #include<cstdio> #include<stack> ...

  5. 「面向 offer 学算法」笔面试大杀器 -- 单调栈

    目录 前言 单调栈 初入茅庐 小试牛刀 打怪升级 出师试炼 前言 单调栈是一种比较简单的数据结构.虽然简单,但在某些题目中能发挥很好的作用. 最近很多大厂的笔试.面试中都出现了单调栈的题目,而还有不少 ...

  6. POJ-2081 Terrible Sets(暴力,单调栈)

    Terrible Sets Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4113 Accepted: 2122 Descrip ...

  7. Poj 3250 单调栈

    1.Poj 3250  Bad Hair Day 2.链接:http://poj.org/problem?id=3250 3.总结:单调栈 题意:n头牛,当i>j,j在i的右边并且i与j之间的所 ...

  8. Terrible Sets_单调栈

    Description Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all re ...

  9. poj 3415 Common Substrings(后缀数组+单调栈)

    http://poj.org/problem?id=3415 Common Substrings Time Limit: 5000MS   Memory Limit: 65536K Total Sub ...

随机推荐

  1. linux下提示command not found

    首先就要考虑root 的$PATH里是否已经包含了这些环境变量. 主要是这四个:/bin ,/usr/bin,/sbin,/usr/sbin. 四个主要存放的东东: ./bin: bin为binary ...

  2. 浅谈JavaScript的函数表达式(闭包)

    前文已经简单的介绍了函数的闭包.函数的闭包就是有权访问另一个函数作用域的函数,也就是函数内部又定义了一个函数. var Super=function(num){ var count=num; retu ...

  3. 杭电(hdu)1181 变形课

    变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) Total Submi ...

  4. 使用iconv的包装类CharsetConverter进行编码转换的示例

    GitHub地址https://github.com/BuYishi/charset_converter_test charset_converter_test.cpp #include <io ...

  5. node-orm2

    最近应老大要求,对orm2进行再一步封装,所以记录下封装和使用心得(文中数据库:mysql). 数据库连接 var orm = require("orm"); orm.connec ...

  6. 20170228 Z_po_send_email

    FUNCTION zmm_po_send_email. function zmm_po_send_email. *"------------------------------------- ...

  7. 如何在 Ubuntu 云服务器上部署自己的 Rails 应用

    安装步骤  参考:https://ruby-china.org/topics/32851 在云服务器上安装Ruby|Rails : http://www.cnblogs.com/znsongshu/p ...

  8. Web前端性能优化经验分享

    最近一直有给新同学做前端方面的培训,也有去参与公司前端的招聘,所以把自己资料库里面很多高效且有用的知识做了些 规整分类,然后再分享一篇关于前端优化方面的总结.而且春节一过就又是招聘的高峰期了,在校的. ...

  9. I.MX6 NXP git 仓库

    /************************************************************************* * I.MX6 NXP git 仓库 * 说明: ...

  10. 【HDU 1754】 I Hate It

    [题目链接] 点击打开链接 [算法] 树状数组的最值查询 详见这篇文章 : https://blog.csdn.net/u010598215/article/details/48206959 [代码] ...