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. [游戏模版8] Win32 透明贴图

    >_<:The same with previous introduction. In the InitInstance fanction make a little change: &g ...

  2. dubbo配置文件报错解决思路

    导入dubbo项目到Eclipse,配置文件报了如下异常: Multiple annotations found at this line: - cvc-complex-type.2.4.c: The ...

  3. mysql悲观锁总结和实践

    使用场景举例:以MySQL InnoDB为例商品t_goods表中有一个字段status,status为1代表商品未被下单,status为2代表商品已经被下单,那么我们对某个商品下单时必须确保该商品s ...

  4. Java-认识字符集-转载

    问题起源 对于计算机而言,它仅认识两个0和1,不管是在内存中还是外部存储设备上,我们所看到的文字.图片.视频等等“数据”在计算机中都是已二进制形式存在的.不同字符对应二进制数的规则,就是字符的编码.字 ...

  5. atitit.动态加载数据库配置in orm hibernate mybatis

    atitit.动态加载数据库配置in orm 1. 动态加载数据库配置的优点::: 1 1.1. 组合多个配置文件... 1 1.2. 连接多个数据库 1 2. 基本的流程:::getCfg内存对象, ...

  6. java继承3个小题

    1.实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty和Staff.具体要求如下: (1)Person类中的属性有:姓名name(String类型),地址 ...

  7. Python错误、调试和测试

    try: print('try...') r = 10 / int('a') print('result:', r) except ValueError as e: print('ValueError ...

  8. ADO.NET笔记20160322

    ####ADO.NET ####1 启用sa验证与窗体相关知识     - 启用sa验证     - ShowDialog() ---- ####2 连接字符串     Data Source=服务器 ...

  9. windows下clang的安装与使用

    我本意是想在windows下学习下C++11,而结果是我的Visual Studio 2012不完全支持,而我又懒得去安装2013/2015,太大了.公司运维也不允许我去下载- -,然后就想能不能在w ...

  10. J2EE Web开发入门—通过action是以传统方式返回JSON数据

    关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...