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. mac下载百度云盘大文件及断点续传的方法

    问题 作为资源共享平台, 百度云做的还是很出色的, "xxx site:pan.baidu.com"就可以找到很丰富的资源. 然而, 下载百度云上的文件就略蛋疼了. 早在12年的时 ...

  2. CentOS 7 service systemnctl

    在之前的版本中关闭防火墙等服务的命令是 service iptables stop /etc/init.d/iptables stop 在RHEL7中,其实没有这个服务 [root@rhel7 ~]# ...

  3. MYSQL子查询与连接

    37:子查询与连接SET 列名 gbk;//改变客户端数据表的编码类型. 子查询子查询(Subquery)是指出现在其他SQL语句内的SELECT子句例如SELECT * FROM t1 WHERE ...

  4. SSL在https和MySQL中的原理思考

    之前对HTTPS通信过程有过了解,HTTPS是应用HTTP协议使用SSL加密的版本,在TCP和HTTP之间增加SSL协议.通过握手阶段认证双方身份,协商对称秘钥对通信信息进行加密.此处只描述常用的服务 ...

  5. text-size-adjust属性

    在慕课上无意中看到-webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%;这两段代码,居然发现自己完全不理解,然后就去问度娘了,以下是一些 ...

  6. 修復jquery的tablesorter对加了千分位的数字无法正确排序的bug

    找到函数: function getElementText(config, node) { var text = ""; if (!node) return "" ...

  7. [轉載]史上最强php生成pdf文件,html转pdf文件方法

    之前有个客户需要把一些html页面生成pdf文件,然后我就找一些用php把html页面围成pdf文件的类.方法是可谓是找了很多很多,什么html2pdf,pdflib,FPDF这些都试过了,但是都没有 ...

  8. 让Asp.Net WebAPI支持OData查询,排序,过滤。

    让Asp.Net WebAPI支持OData后,就能支持在url中直接输入排序,过滤条件了. 一.创建Asp.Net WebAPI项目: 二.使用NuGet安装Asp.Net WebAPI 2.2和O ...

  9. “Adobe Flash Player因过期而遭到阻止”的解决办法

    谷歌浏览器总是提示“Adobe Flash Player因过期而遭到阻止”,然后点进去,又更新不了,因为伟大的TC已经把谷歌屏蔽了. 解决办法就是到flash官网更新到最新的Flash https:/ ...

  10. LDO/DC-DC区别总结(转)

    电源是一个电子系统中不可缺少的非常重要的一部分.但是外接的电源通常不能够完全提供系统中需要的所有的电源种类.因此带来了电源电压的变换问题.常用的电源电压的变换芯片包括LDO和DC-DC两种.下面对这两 ...