POJ2082 --- Terrible Sets (monotonous stack)

Source: Internet
Author: User

POJ2082 --- Terrible Sets (monotonous stack)

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, sigma 0 <= j <= i-1wj <= x <= Sigma 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

A water question is difficult to understand...
Is to give you a row of rectangles, find the maximum rectangle area that can be formed
Monotonous Stack

/*************************************** * *********************************> File Name: POJ2082.cpp> Author: ALex> Mail: zchao1995@gmail.com> Created Time: thursday, May 07, 2015 ******************************** **************************************** /# include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Include
        
          # Include
         
           # Include
          
            # Include
           # Include
            
              # Include
             
               # Include
              
                Using namespace std; const double pi = acos (-1.0); const int inf = 0x3f3f3f; const double eps = 1e-15; typedef long LL; typedef pair
               
                 PLL; static const int N = 50010; int w [N], h [N]; int r [N], l [N]; int sum [N]; stack
                
                  St; int main () {int n; while (~ Scanf ("% d", & n) & n! =-1) {for (int I = 1; I <= n; ++ I) {scanf ("% d", & w [I], & h [I]); l [I] = r [I] = I;} while (! St. empty () {st. pop () ;}sum [0] = 0; for (int I = 1; I <= n; ++ I) {sum [I] = sum [I-1] + w [I] ;}for (int I = n; I> = 1; -- I) {if (st. empty () {st. push (make_pair (h [I], I);} else {while (! St. empty () {PLL u = st. top (); if (u. first <= h [I]) {break;} st. pop (); l [u. second] = I + 1;} st. push (make_pair (h [I], I) ;}} while (! St. empty () {PLL u = st. top (); st. pop (); l [u. second] = 1 ;}for (int I = 1; I <= n; ++ I) {if (st. empty () {st. push (make_pair (h [I], I);} else {while (! St. empty () {PLL u = st. top (); if (u. first <= h [I]) {break;} st. pop (); r [u. second] = I-1;} st. push (make_pair (h [I], I) ;}} while (! St. empty () {PLL u = st. top (); st. pop (); r [u. second] = n;} int ans = 0; for (int I = 1; I <= n; ++ I) {int L = l [I]; int R = r [I]; ans = max (ans, h [I] * (sum [R]-sum [L-1]);} printf ("% d \ n", ans);} return 0 ;}
                
               
              
             
            
          
         
        
       
      
     
    
   
  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.