Terrible Sets
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
题目类型:单调队列
算法分析:一道经典的使用单调队列思想解的题。坐标从小到大的分析,如果存在一个高度不超过先前的高度,则应该将先前高度的矩形块的面积出栈并计算,更新最大值。否则就一直入栈。最后将还在栈中的矩形块的面积计算出来并更新最大值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 10000007; const int maxn = 100000 + 66; struct Node { long long h, w; Node (long long hh, long long ww) : h (hh), w (ww) {} Node () {h = 0, w = 0;} }; int main() { // freopen ("aaa.txt", "r", stdin); int n; while (scanf ("%d", &n) != EOF) { if (n == -1) break; deque <Node> deq; long long maxval = -INF, cnt; int i, W, H; for (i = 0; i < n; i++) { scanf ("%d%d", &W, &H); cnt = 0; while (!deq.empty() && deq.back().h >= H) { Node temp = deq.back (); deq.pop_back(); if (maxval < temp.h * (temp.w + cnt)) maxval = temp.h * (temp.w + cnt); cnt += temp.w; } deq.push_back (Node (H, W + cnt)); } cnt = 0; while (!deq.empty ()) { Node temp = deq.back (); deq.pop_back (); if (maxval < temp.h * (temp.w + cnt)) maxval = temp.h * (temp.w + cnt); cnt += temp.w; } printf ("%lld\n", maxval); } return 0; } |
- « 上一篇:poj2080
- poj2105:下一篇 »