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. [异常] openCV安装和配置

    http://blog.csdn.net/mygis2005/article/details/10472717 >_<" 这个链接亲测可行,我试了很多次,找了很多个都不行,最后怀 ...

  2. jenkins插件 查看job修改历史

    文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciandcd 插件jobConfigHistory(https:/ ...

  3. [BTS] The external credentials in the SSO database are more recent.

    For test some interface. We change BizTalk Server time to a feature date. For example, change to nex ...

  4. Bean的作用域及生命周期

    指定bean的作用域通过scope属性 singleton单实例模式,从初始化容器就初始化bean,除非延迟初始化lazy-init=true prototype每次从容器获取bean是新的对象,从g ...

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

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

  6. iOS开发-友盟分享(1)

    1.集成友盟分享,需要先注册一个友盟账号,注册地址 友盟开发者平台官网  友盟集成文档 友盟sdk下载地址友盟sdk下载地址 2,成功下载sdk集成后,微信分享需要配置一下 新浪微博 之类到同样配置就 ...

  7. 在github上写博客

    在github上混了几个月,收获颇多.作为一个开源的坚定信仰者,深深觉得每一个码农都应该参与到开源社区中,github提供了一个平台,让你为开源项目提交代码变得异常简单和直接.以前由于工作异常繁忙和繁 ...

  8. HEXO+PAGE 搭建个性博客

    新博客地址: http://javen205.oschina.io https://javen205.github.io Hexo 是高效的静态站点生成框架,她基于 Node.js. 通过 Hexo ...

  9. 转:电子取证中AVI文件的文件雕复

    电子取证中AVI文件的文件雕复 收藏本文 分享 1引言在电子取证工作中,恢复数字设备中被删除的数据是极为重要的工作之一,恢复数据又分依赖系统元信息的传统数据恢复技术和不依赖系统元信息的文件雕刻.文件雕 ...

  10. 比较好的文件复制工具fastcopy和校验工具

    fastcopy http://ipmsg.org/tools/fastcopy.html.en extractfile --可以选用ADLER32计算模式,更快速.