POJ 2082 Terrible Sets(栈)

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 w 1h 1+w 2h 2+...+w nh n  < 10 9.

Output

Simply output Max(S) in a single line for each case.

 

题目大意:有n个并列的长方形,找出一个长方形,使其落下这n个长方形上,并且面积最大。

思路:不会讲……给个tips,有一些答案不可能成为最大矩形就不用算了。

 

代码(94MS):

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <iostream>

 4 #include <algorithm>

 5 #include <stack>

 6 using namespace std;

 7 

 8 const int MAXN = 50010;

 9 

10 struct Rectangle {

11     int w, h;

12     void read() {

13         scanf("%d%d", &w, &h);

14     }

15 };

16 

17 Rectangle a[MAXN], t;

18 int ans, n;

19 

20 void solve() {

21     ans = 0;

22     int last = 0;

23     stack<Rectangle> stk;

24     for(int i = 0; i < n; ++i) {

25         t.read();

26         if(last < t.h) stk.push(t);

27         else {

28             int total = 0;

29             while(!stk.empty() && stk.top().h > t.h) {

30                 total += stk.top().w;

31                 ans = max(ans, stk.top().h * total);

32                 stk.pop();

33             }

34             t.w += total;

35             stk.push(t);

36         }

37         last = t.h;

38     }

39     int total = 0;

40     while(!stk.empty()) {

41         total += stk.top().w;

42         ans = max(ans, stk.top().h * total);

43         stk.pop();

44     }

45 }

46 

47 int main() {

48     while(scanf("%d", &n) != EOF) {

49         if(n == -1) break;

50         solve();

51         printf("%d\n", ans);

52     }

53 }
View Code

 

 

你可能感兴趣的:(set)