Record the dynamic planning of DPL, DPR, distinguish the left side of the record I is larger than that of I, and then (DPR [I]-DPL [I] + 1) * H [I] To Get The Length
Dynamic transfer equation while (temp> 1 & H [temp-1]> = H [I]) temp = DPL [temp-1]
/*************************************** * *********************************> File Name: hdu1506.cpp> author: Yang> mail: [email protected]> created time: sunday, August 24, 2014, 23:41:16 ************************************* * *********************************/# include <iostream> # include <stdio. h> # include <memory. h> using namespace STD; # define N using 5int main () {int DPL [N], DPR [N]; long H [N]; int N; while (scanf ("% d", & N), n) {for (INT I = 1; I <= N; I ++) scanf ("% LLD ", & H [I]); DPL [1] = 1; int temp; For (INT I = 2; I <= N; I ++) {temp = I; while (temp> 1 & H [temp-1]> = H [I]) temp = DPL [temp-1]; DPL [I] = temp;} DPR [N] = N; for (INT I = n-1; I> = 1; I --) {temp = I; while (temp <n & H [I] <= H [temp + 1]) temp = DPR [temp + 1]; DPR [I] = temp;} long sum, ANS = 0; For (INT I = 1; I <= N; I ++) {// cout <DPL [I] <"" <DPR [I] <Endl; sum = (DPR [I]-DPL [I] + 1) * H [I]; // cout <"sum:" <sum <Endl; if (sum> ans) ans = sum;} cout <ans <Endl ;}}
HDU 1507 largest rectangle in a Histogram