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

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

Source

 
【题解】
先看暴力做法
以每一个方块为起点,看他能向右拓展到什么地方,这块面积来更新答案,复杂度n^2
这种东西很套路,就是用单调栈维护。
我们以向左延伸为例
我们不难发现如果突然递减,那么小的那个将制约前面的向右延伸,前面比他高的就等价于与他同高度了
于是弹出来就可以了
答案需要在如下几个地方更新:
1、一整个大方块
2、弹出的时候,累加宽度,乘这个小的高度,表示向左能延伸到什么地方。然后用累加高度和这个笑的高度
虚拟一个新的方块压到栈里面
3、扫完后,把整个栈慢慢弹出,累加宽度更新答案
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#define max(a, b) ((a) < (b) ? (b) : (a)) const int MAXN = + ; inline void read(int &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} int n,w,h,stackw[MAXN],stackh[MAXN],top,ans; int main()
{
while(scanf("%d", &n) != EOF && n != -)
{
read(stackw[++top]), read(stackh[top]);
ans = stackw[top] * stackh[top];
register int lei = ;
for(register int i = ;i <= n;++ i)
{
read(w), read(h);
ans = max(ans, w * h);
if(h < stackh[top])
{
lei = ;
while(h < stackh[top])
{
lei += stackw[top];
ans = max(ans, lei * stackh[top]);
-- top;
}
stackh[++top] = h;
stackw[top] = lei;
}
stackh[++top] = h;
stackw[top] = w;
}
lei = ;
while(top)
{
lei += stackw[top];
ans = max(ans, lei * stackh[top]);
-- top;
}
printf("%d\n", ans);
}
return ;
}

POJ2082

 

POJ2082 Terrible Sets的更多相关文章

  1. poj2082 Terrible Sets(单调栈)

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

  2. POJ 2082 Terrible Sets

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

  3. Terrible Sets

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

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

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

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

  6. PKU 2082 Terrible Sets(单调栈)

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

  7. POJ 2082 Terrible Sets(单调栈)

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

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

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

  9. 别人整理的DP大全(转)

    动态规划 动态规划 容易: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ...

随机推荐

  1. SPRINGBOOT配置MYSQL,MYBATIS,DRUID

    配置 DRUID连接池 MYSQL数据库 MYBATIS持久层框架 添加依赖 <dependency> <groupId>mysql</groupId> <a ...

  2. 深度学习(二十六)Network In Network学习笔记

    深度学习(二十六)Network In Network学习笔记 Network In Network学习笔记 原文地址:http://blog.csdn.net/hjimce/article/deta ...

  3. Spring整合JUnit单元测试

    必须先有JUnit的环境(已经导入了Junit4的开发环境) 1.导入jar包 2.在测试类上添加注解 @RunWith(SpringJUnit4ClassRunner.class) @Context ...

  4. putty开源的ssh软件工具

    # 登录远程服务器需要ip和端口即可:还是开源工具用起来无忧无虑.无拘无束,这种感觉实在太舒服了,比起xshell开始免费.后来收费好太多太多,不用担心哪天过期了,想干嘛就干嘛. 软件下载地址:htt ...

  5. Windows 下 MQTT 服务器搭建之Apollo

    https://blog.csdn.net/wangh0802/article/details/84861226#%EF%BC%881%EF%BC%89%E4%B8%8B%E8%BD%BD%20Apo ...

  6. Android SDK 开发指南

    Android SDK 开发指南 视频详解 以下视频是对融云 Android SDK 开发使用的详细讲解,您可以在阅读文档时配合学习.   更多视频教程如下: CSDN 融云 Android SDK ...

  7. C++星号的含义

    [转载] [http://blog.sina.com.cn/s/blog_4a50d85b0100uk3c.html]   1.乘法运算符   2.定义指针 int *p = 0; 还是 int* p ...

  8. PAT甲级——A1049 Counting Ones

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

  9. PAT甲级——A1033 To Fill or Not to Fill

    With highways available, driving a car from Hangzhou to any other city is easy. But since the tank c ...

  10. 切片 及 range 点睛回炉

    print(list(range(-1,-5,-1))) # [-1, -2, -3, -4] # range(x,y,z) ''' 首先把 x的数值放在 数轴上 z 的正负决定 x 向前搜索(为正) ...