HDU 1506 & poj 2559 largest rectangle in a histogram (monotonous Queue)

Source: Internet
Author: User

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)

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.