poj2082 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.
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
刚做了poj2559,这题只是每个矩形有宽度了。
1 program rrr(input,output); 2 var 3 h,l,r,q,sum:array[0..50050]of longint; 4 n,i,t,ans,w:longint; 5 function max(a,b:longint):longint; 6 begin 7 if a>b then exit(a) else exit(b); 8 end; 9 begin 10 assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output); 11 while true do 12 begin 13 readln(n);if n=-1 then break; 14 sum[0]:=0; 15 for i:=1 to n do begin readln(w,h[i]);sum[i]:=sum[i-1]+w; end; 16 t:=0;q[0]:=0; 17 for i:=1 to n do 18 begin 19 while (t>0) and (h[i]<=h[q[t]]) do dec(t); 20 l[i]:=q[t]; 21 inc(t);q[t]:=i; 22 end; 23 t:=0;q[0]:=n+1; 24 for i:=n downto 1 do 25 begin 26 while (t>0) and (h[i]<=h[q[t]]) do dec(t); 27 r[i]:=q[t]; 28 inc(t);q[t]:=i; 29 end; 30 ans:=0; 31 for i:=1 to n do ans:=max(ans,h[i]*(sum[r[i]-1]-sum[l[i]])); 32 writeln(ans); 33 end; 34 close(input);close(output); 35 end.