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

Description

Let N be the set of all natural numbers {0 , 1 , 2 , . . . }, and R be the set of all real numbers. wi, hi for i = 1 . . . n are some elements in N, and w0 = 0. 
Define set B = {< x, y > | x, y ∈ R and there exists an index i > 0 such that 0 <= y <= hi ,∑0<=j<=i-1wj <= x <= ∑0<=j<=iwj} 
Again, define set S = {A| A = WH for some W , H ∈ R+ and there exists x0, y0 in N such that the set T = { < x , y > | x, y ∈ R and x0 <= x <= x0 +W and y0 <= y <= y0 + H} is contained in set B}. 
Your mission now. What is Max(S)? 
Wow, it looks like a terrible problem. Problems that appear to be terrible are sometimes actually easy. 
But for this one, believe me, it's difficult.

Input

The input consists of several test cases. For each case, n is given in a single line, and then followed by n lines, each containing wi and hi separated by a single space. The last line of the input is an single integer -1, indicating the end of input. You may assume that 1 <= n <= 50000 and w1h1+w2h2+...+wnhn < 109.

Output

Simply output Max(S) in a single line for each case.

Sample Input

3
1 2
3 4
1 2
3
3 4
1 2
3 4
-1

Sample Output

12
14
题目大意:给出一系列矩形的宽度和高度,矩形沿着x轴对齐,求这些矩形组成的连续矩形区域的最大面积。
解题方法:这是一道非常好的题,用栈保存矩形,如果高度递增则不断入栈,如果遇到当前输入的比栈顶高度小,则从栈顶开始不断出栈并且计算最大面积,直到栈顶高度小于当前输入高度则停止出栈,并把开始出栈矩形的宽度累加得到totalw,把totalw和当前输入的矩形宽度相加得到当前输入矩形的宽度,并入栈,这样栈中保存的永远都是高度递增的矩形,最后输入完了之后如果栈不为空,则依次出栈并计算最大面积。
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <stack>
using namespace std; typedef struct
{
int w;
int h;
}Node; int main()
{
stack<Node> Stack;
int totalw, ans, w, h, n;
while(scanf("%d", &n) != EOF && n != -)
{
ans = ;
for (int i = ; i < n; i++)
{
scanf("%d%d", &w, &h);
if (Stack.empty())//如果栈为空,则入栈
{
Node temp;
temp.w = w;
temp.h = h;
Stack.push(temp);
}
else
{
totalw = ;
if (h >= Stack.top().h)//如果当前矩形高度大于栈顶矩形高度,入栈
{
Node temp;
temp.w = w;
temp.h = h;
Stack.push(temp);
}
else
{
//如果当前输入矩形高度小于栈顶矩形高度,出栈并计算最大面积
while(!Stack.empty() && Stack.top().h > h)
{
//宽度从栈顶开始依次累加
totalw += Stack.top().w;
if (ans < totalw * Stack.top().h)
{
//得到最大面积
ans = totalw * Stack.top().h;
}
Stack.pop();
}
Node temp;
//出栈完毕之后,栈为空或者栈顶矩形高度小于当前输入高度,
//以保证栈中的矩形高度递增
temp.w = w + totalw;//加上开始出栈的所有矩形宽度之和,即为当前输入矩形的宽度
temp.h = h;
Stack.push(temp);
}
}
}
totalw = ;
//如果栈不为空,则依次出栈并计算最大面积
while(!Stack.empty())
{
totalw += Stack.top().w;
if (ans < totalw * Stack.top().h)
{
ans = totalw * Stack.top().h;
}
Stack.pop();
}
printf("%d\n", ans);
}
return ;
}
 

POJ 2082 Terrible Sets的更多相关文章

  1. POJ 2082 Terrible Sets(单调栈)

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

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

  3. stack(单调栈) POJ 2082 Terrible Sets

    题目传送门 题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少 分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度 ...

  4. PKU 2082 Terrible Sets(单调栈)

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

  5. Terrible Sets

    Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3017   Accepted: 1561 Des ...

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

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

  7. POJ2082 Terrible Sets

    Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5067   Accepted: 2593 Des ...

  8. 计算最大矩形面积,POJ(2082)

    题目链接:http://poj.org/problem?id=2082 把矩形按照高度一次递增的循序排列,当违反这一规则的时候,更新ans,用新的data替换之前的矩形.然后最后扫一遍. #inclu ...

  9. poj2082 Terrible Sets(单调栈)

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

随机推荐

  1. Linux 进程

    Linux 进程 在用户空间,进程是由进程标识符(PID)表示的.从用户的角度来看,一个 PID 是一个数字值,可惟一标识一个进程.一个 PID 在进程的整个生命期间不会更改,但 PID 可以在进程销 ...

  2. 我所常用的ajax调用格式

    ajax: $.ajax({    type: "post",    datatype: "json",    contentType: "appli ...

  3. Spring AOP(配置文件方式)

    spring配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="h ...

  4. H5图片裁剪升级版(手机版)

    前段时间做了个跟裁剪相关的活动<用H5中的Canvas等技术制作海报>,这次公司要做个与奥运相关的活动,扫车牌赢奖. 于是我就在上一个活动的基础上,将代码重新封装一下,并且将计算方式写的更 ...

  5. 在Debian下安装LAMP

    准备工作: 1 sudo apt-get install build-essential 第一步:安装Apache 1 sudo apt-get install apache2 第二步:安装MySQL ...

  6. Django基础——Model篇(三)

    一 Django ORM中的概念 ORM —— 关系对象映射,是Object Relational Mapping的简写,是用来简化数据库操作的框架 Django ORM遵循Code Frist原则, ...

  7. Prototype in JavaScript

    声明 本文旨在入门,简单了解下何为prototype & __proto__ 原型对象 我们创建每个函数都有个prototype(原型)属性,该属性是一个指针,指向一个对象,而这对象的用途是包 ...

  8. Java 线程 — JMM Java内存模型

    JMM Java Memory Model,Java内存模型,属于语言级的内存模型 并发编程中存在的问题: 如何通信:用于线程之间交换信息.两种方式:共享内存,消息传递 如何同步:用于控制不同线程间操 ...

  9. 利用Httponly提升web应用程序安全性

    随着www服务的兴起,越来越多的应用程序转向了B/S结构,这样只需要一个浏览器就可以访问各种各样的web服务,但是这样也越来越导致了越来越 多的web安全问题.www服务依赖于Http协议实现,Htt ...

  10. td 自动换行

    Two solutions for cell width:1. Omit words: <td style="width:60px;"><div style=&q ...