Test instructions: To find the maximum rectangular area.
Idea: Maintain a monotonically increasing rectangular height, and when a number that does not maintain monotonicity is added, the maintenance area is accumulated ans
Ans = max{ans, current rectangle height x current position to the right width};
Finally, add 0 and empty the stack.
PS: Kneel jzt seniors/. \
1#include <stdio.h>2#include <algorithm>3 using namespacestd;4 structnode{5 Long Longheight, width;6}stack[100010];7 Long LongN, height, top, tmp, ans;8 intMain () {9 while(SCANF ("%lld", &n)!=eof&&N) {Tentop=0; ans =0; height =0; One for(inti =1; I <= N; i++){ Ascanf"%lld", &height); TMP =0; - while(Top >0&& stack[top].height > Height) {ans = max (ans, stack[top].height * (tmp +stack[top].width)); TMP + = Stack[top]. width;top--;} -Stack[++top].height =height; theStack[top].width =1+tmp; - } -TMP =0; - while(Top >0) {ans = max (ans, stack[top].height * (tmp +stack[top].width)); tmp + = stack[top].width;top--;} +printf"%lld\n", ans); - } +}
poj2559 largest Rectangle in a histogram