Hdu 1506 Largest Rectangle in a Histogram (find the Largest Rectangle)

Source: Internet
Author: User

Hdu 1506 Largest Rectangle in a Histogram (find the Largest Rectangle)

1. Be sure to define a [] as LL. I wa N times here.

2. dp is used to search for boundaries.

AC code:

# Include
 
  
# Include
  
   
# Define LL long longusing namespace std; const ll inf = 1 <60; LL a [100005]; // define it as LLint L [100005]; int R [100005]; int main () {int n; while (scanf ("% d", & n), n) {for (int I = 1; I <= n; I ++) {scanf ("% lld", & a [I]) ;}for (int I = 1; I <= n; I ++) // use the dp ideology, constantly expanding the boundary {L [I] = I; while (L [I]> 1 & a [L [I]-1]> = a [I]) L [I] = L [L [I]-1];} for (int I = n; I> = 1; I --) {R [I] = I; while (R [I]
   
    
= A [I]) R [I] = R [R [I] + 1];} LL ans =-INF; for (int I = 1; I <= n; I ++) {LL temp = a [I] * (R [I]-L [I] + 1); if (ans
    
     
Timeout code for finding boundaries without using dp:
     

 

 

# Include
      
       
# Include
       
        
# Define LL long longusing namespace std; const ll inf = 1 <60; LL a [100005]; int L [100005]; int R [100005]; int main () {int n; while (scanf ("% d", & n), n) {for (int I = 1; I <= n; I ++) {scanf ("% lld", & a [I]) ;}for (int I = 1; I <= n; I ++) // timeout, optimize with dp {int j = I; while (j> 1 & a [j]> = a [I]) j --; L [I] = ++ j;} for (int I = 1; I <= n; I ++) {int j = I; while (j
        
         
= A [I]) j ++; R [I] = -- j;} LL ans =-INF; for (int I = 1; I <= n; I ++) {LL temp = a [I] * (R [I]-L [I] + 1); if (ans
         
          

 

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.