HDU-1506 largest rectangle in a histogram Dynamic Programming

Source: Internet
Author: User

Attach the TleCodeFirst, discretization the height, and then solving the DP height. The complexity is too high.

The Code is as follows:

View code

# Include <cstdlib> # Include <Cstdio> # Include <Cstring> # Include <Map> # Include <Iostream> # Include <Algorithm> # Define Maxn 100005 Using   Namespace  STD; Int  N, CNT;  Int  SEQ [maxn], CSeq [maxn];  Long   Long  DP [maxn]; Map < Int , Int > MP, RMP;  Void Getint ( Int & C ){  Char  CHR; While (CHR = getchar (), CHR < '  0  ' | CHR> '  9  '  ); C = CHR- '  0  '  ;  While (CHR = getchar (), CHR> = '  0  ' & CHR <= '  9  '  ) {C = C * 10 + Chr- '  0  '  ;} Inline  Long   Long  DP (){  Long   Long Max = 0  ; For ( Int I = 0 ; I <n; ++ I ){  For ( Int J = 1 ; J <= MP [seq [I]; ++ J) {DP [J] + = RMP [J]; max = Max (max, DP [J]);}  For ( Int J = MP [seq [I] + 1 ; J <= CNT; ++ J) {DP [J] = 0  ;}}  Return  Max ;}  Int  Main (){  Int  Max;  While (Scanf ( "  % D  " ,& N), n) {CNT = 0 ; Memset (DP,  0 , Sizeof  (DP); MP. Clear (), RMP. Clear ();  For ( Int I = 0 ; I <n; ++ I) {getint (SEQ [I]); CSeq [I] = SEQ [I];} Sort (CSeq, CSeq + N );  For ( Int I =0 ; I <n; ++ I ){  If (MP. Count (CSeq [I]) = 0  ) {MP [CSeq [I] = ++ CNT; RMP [CNT] = CSeq [I] ;}} printf (  "  % I64d \ n  "  , Dp ());}  Return   0  ;} 

 

Later, the state is constructed based on a more sharp solution. L [I], R [I], and the two arrays are maintained for the purpose. It indicates the leftmost and rightmost aspects of the current height.

The Code is as follows:

# Include <cstdlib> # Include <Cstdio> # Include <Cstring> # Include <Iostream> # Define Maxn 100005 Using   Namespace  STD;  Int  N, seq [maxn], L [maxn], R [maxn]; inline  Long   Long Max (Long   Long X, Long   Long  Y ){  Return X> Y? X: Y ;}  Long   Long  DP (){  Long   Long Max = 0  ;  For ( Int I =2 ; I <= N; ++ I ){  While (SEQ [L [I]- 1 ]> = SEQ [I]) {L [I] = L [L [I]- 1  ];  If (L [I] <= 0 ) Break ; //  This sentence is not added before, TLE  }}  For (Int I = N- 1 ; I> = 1 ;-- I ){  While (SEQ [R [I] + 1 ]> = SEQ [I]) {R [I] = R [R [I] + 1  ];  If (R [I]> = N) Break  ;}}  For ( Int I = 1 ; I <= N; ++ I) {max = Max (max, (long) (1) * seq [I] * (R [I]-l [I] + 1  ));}  Return  Max ;}  Int  Main (){  While (Scanf ( "  % D  " ,& N), n ){  For (Int I = 1 ; I <= N; ++ I) {scanf (  "  % D  " ,& SEQ [I]); L [I] = R [I] = I;} printf (  "  % I64d \ n  "  , Dp ());}  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.