POJ2082 Terrible Sets

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

Shanghai 2004 Preliminary

【题解】

先看暴力做法

以每一个方块为起点,看他能向右拓展到什么地方,这块面积来更新答案,复杂度n^2

这种东西很套路,就是用单调栈维护。

我们以向左延伸为例

我们不难发现如果突然递减,那么小的那个将制约前面的向右延伸,前面比他高的就等价于与他同高度了

于是弹出来就可以了

答案需要在如下几个地方更新:

1、一整个大方块

2、弹出的时候,累加宽度,乘这个小的高度,表示向左能延伸到什么地方。然后用累加高度和这个笑的高度

虚拟一个新的方块压到栈里面

3、扫完后,把整个栈慢慢弹出,累加宽度更新答案

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #define max(a, b) ((a) < (b) ? (b) : (a))
 6
 7 const int MAXN = 60000 + 10;
 8
 9 inline void read(int &x)
10 {
11     x = 0;char ch = getchar(), c = ch;
12     while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();
13     while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();
14     if(c == ‘-‘)x = -x;
15 }
16
17 int n,w,h,stackw[MAXN],stackh[MAXN],top,ans;
18
19 int main()
20 {
21     while(scanf("%d", &n) != EOF && n != -1)
22     {
23         read(stackw[++top]), read(stackh[top]);
24         ans = stackw[top] * stackh[top];
25         register int lei = 0;
26         for(register int i = 2;i <= n;++ i)
27         {
28             read(w), read(h);
29             ans = max(ans, w * h);
30             if(h < stackh[top])
31             {
32                 lei = 0;
33                 while(h < stackh[top])
34                 {
35                     lei += stackw[top];
36                     ans = max(ans, lei * stackh[top]);
37                     -- top;
38                 }
39                 stackh[++top] = h;
40                 stackw[top] = lei;
41             }
42             stackh[++top] = h;
43             stackw[top] = w;
44         }
45         lei = 0;
46         while(top)
47         {
48             lei += stackw[top];
49             ans = max(ans, lei * stackh[top]);
50             -- top;
51         }
52         printf("%d\n", ans);
53     }
54     return 0;
55 }

POJ2082

时间: 2024-10-09 23:30:33

POJ2082 Terrible Sets的相关文章

Terrible Sets

Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3017   Accepted: 1561 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

poj 2082 Terrible Sets (数据结构 ——栈 STL)

 Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 2999   Accepted: 1549 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 elem

POJ 2082 Terrible Sets

Terrible Sets Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5325   Accepted: 2713 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 element

stack(单调栈) POJ 2082 Terrible Sets

题目传送门 题意:紧贴x轴有一些挨着的矩形,给出每个矩形的长宽,问能组成的最大矩形面积为多少 分析:用堆栈来维护高度递增的矩形,遇到高度小的,弹出顶部矩形直到符合递增,顺便计算矩形面积,且将弹出的宽度都累积到当前的矩形中,这样最后再扫描一遍,算面积很方便,这题应该算是 POJ 2559 的强化版了 收获:stack的应用,求矩形面积,矩阵相乘,表达式计算 代码: /************************************************ * Author :Running

Terrible Sets(求矩形的最大面积(递增栈))

题意:紧贴x轴有一些相互挨着的矩形,给定每个矩形的长宽,问他们可以形成的最大矩形是多少. Sample Input 3 1 2 3 4 1 2 3 3 4 1 2 3 4 -1 Sample Output 12 14 1 #include <iostream> 2 #include <stack> 3 #include <cstdio> 4 5 using namespace std; 6 7 struct rec{ 8 int w; 9 int h; 10 11 }d

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195

[转] POJ DP问题

列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2039, 2063, 20

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************