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 ;}