Link: poj 2559 largest rectangle in a Histogram
 
Link: HDU 1506 largest rectangle in a Histogram
 
A series is given to indicate the height of the corresponding rectangle, and the largest rectangle area in the entire graph is obtained.
 
2, 1, 4, 5, 1, 3, 3
 
:
 
 
 
Idea: the maximum length that can be expanded to the left to the right of each rectangle multiplied by its height. The maximum value is the answer.
 
The maintenance sequence is decreasing with a monotonous queue, and the element of the output queue is the "Extreme Value" point.
 
Note: int64.
 
 
AC code:
 
 
 
# Include <stdio. h> # include <string. h> # define ll _ int64struct node {ll l, R; ll sum ;}; struct node que [100010]; ll H [100010]; ll pre [100010], next [100010]; // pre records the farthest position of hi to the left, and the farthest position of hi to the right in the next record. Int main () {ll N, I; ll head, tail; while (scanf ("% i64d", & N )! = EOF, n) {memset (que, 0, sizeof que); for (I = 1; I <= N; I ++) {scanf ("% i64d ", & H [I]); Pre [I] = N; next [I] = 1;} head = 1, tail = 0; for (I = 1; I <= N; I ++) {While (Head <= tail & H [I] <que [tail]. sum) // maintain the monotonic decreasing queue {pre [que [tail]. l] = I-1; // the element of the output queue is the farthest position on the right of his expansion tail --; // the pop-up element} que [+ tail]. sum = H [I]; // Input Queue que [tail]. L = I; // record location .} Head = 1, tail = 0; for (I = N; I> = 1; I --) {While (Head <= tail & H [I] <que [tail]. sum) {next [que [tail]. r] = I + 1; // the element of the output queue is the farthest position on the left of the queue to expand. --;} que [++ tail]. sum = H [I]; que [tail]. R = I;} ll ans = 0, temp; for (I = 1; I <= N; I ++) {temp = (pre [I]-next [I] + 1) * H [I]; If (temp> ans) ans = temp ;} printf ("% i64d \ n", ANS);} return 0 ;} 
 
 
 
HDU 1506 & poj 2559 largest rectangle in a histogram (monotonous Queue)