POJ2082 Terrible Sets

Posted 嘒彼小星

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 
【题解】
先看暴力做法
以每一个方块为起点,看他能向右拓展到什么地方,这块面积来更新答案,复杂度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

 

 

以上是关于POJ2082 Terrible Sets的主要内容,如果未能解决你的问题,请参考以下文章

poj2082 terrible sets 单调栈

poj2082 terrible sets 单调栈

Terrible Sets_单调栈

POJ-2082 terriblesets(堆栈题+阅读理解)

POJ 2082 题解

CodeForces - 742B Arpa’s obvious problem and Mehrdad’s terrible solution