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. Nginx学习笔记(八) Nginx进程启动分析

    Nginx进程启动分析 worker子进程的执行循环的函数是ngx_worker_process_cycle (src/os/unix/ngx_process_cycle.c). 其中,捕获事件.分发 ...

  2. AngularJS快速入门指南09:SQL

    我们可以将之前章节中的代码用来从数据库中读取数据. 通过PHP Server从MySQL数据库中获取数据 <div ng-app="myApp" ng-controller= ...

  3. FIR.im Weekly - 上周微博热转资源精选

    LeakCanary: 让内存泄露无所遁形 Square 开源的 LeakCanary,国内开发者 @廖祜秋liaohuqiu 翻译了对应的官方博客,撰写了中文使用说明文档,同时还写了一个小 Demo ...

  4. MapReduce之单词计数

    最近在看google那篇经典的MapReduce论文,中文版可以参考孟岩推荐的 mapreduce 中文版 中文翻译 论文中提到,MapReduce的编程模型就是: 计算利用一个输入key/value ...

  5. javascript设计模式与开发实践阅读笔记(6)——代理模式

    代理模式:是为一个对象提供一个代用品或占位符,以便控制对它的访问. 代理模式的关键是,当客户不方便直接访问一个对象或者不满足需要的时候,提供一个替身对象来控制对这个对象的访问,客户实际上访问的是替身对 ...

  6. 在ArcGIS空间数据库中增加点数据的方法

    1.新建一个mxd(ArcMAP)文件 2.从ArcCatalog中把要编辑的图层拖到ArcMAP中 3.从ArcCatalog中拖一个参照图层到ArcMAP中,比如临沂市的县级区划图 4.打开Edi ...

  7. ASP.NET MVC中实现多个按钮提交的几种方法

    有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能,比如一个简单的审批功能. 如果是用webform那不需要讨论,但asp.net mvc中一个表单只能提交到一个Action处理,相对比较 ...

  8. 部分设备在微信内无法播放audio的解决方案

    临时接到一个紧急的需求,一个活动页面,在某台iPhone 5S设备上无法播放音频,其它设备均正常.我接到这个任务时,也是一脸懵逼,试过在audio标签上添加controls属性来显示audio,结果发 ...

  9. 了解 JavaScript (6)– 广告条(Banner)

    在 Web 上冲浪时,常常会见到定期在图像之间切换的广告条.我们可以用 JavaScript 来实现,重复循环显示它们. 创建循环的广告条 RotatingBanner.html 页面中在循环的广告条 ...

  10. 神舟K650c i7(W350STQ)上成功装好Mac OS X 10.9,兼谈如何安装WinXP、7、8.1、OSX、Ubuntu五系统(Chameleon、MBR)

    作者:zyl910 参考教程——http://bbs.pcbeta.com/viewthread-1432534-1-4.html笔记本SNB和IVY平台Win7/Win8/Win8.1安装OS X ...