HDU 1506 largest Rectangle in a histogram monotone queue

Source: Internet
Author: User

Largest Rectangle in a histogramTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 12554 Accepted Submission (s): 3509


Problem Descriptiona Histogram is a polygon composed of a sequence for rectangles aligned at a common base line. The rectangles has equal widths but could have different heights. For example, the figure on the left shows the histogram that consists of rectangles with the Heights 2, 1, 4, 5, 1, 3, 3, Measured in units where 1 is the width of the rectangles:

Usually, histograms is used to represent discrete distributions, e.g., the frequencies of characters in texts. Note the order of the rectangles, i.e, their heights, is important. Calculate the area of the largest rectangle in a histogram that's aligned at the common base line, too. The shows the largest aligned rectangle for the depicted histogram.
Inputthe input contains several test cases. Each test case describes a histogram and starts with an integer n, denoting the number of rectangles it is composed of. Assume that 1 <= n <= 100000. Then follow n integers h1, ..., HN, where 0 <= hi <= 1000000000. These numbers denote the heights of the rectangles in histogram order. The width of each rectangle is 1. A Zero follows the input for the last test case.
Outputfor each test case output on a single line the area of the largest rectangle in the specified histogram. Remember that this rectangle must is aligned at the common base line.
Sample Input
7 2 1 4 5 1 3 34 1000 1000 1000 10000

Sample Output
84000



Test instructions: Enter n columns and discharge. Ask the largest rectangular area.
To maintain two monotone queues, LfT and RIT. LFT [i] represents the I point, the left row is higher than I point column, the leftmost column number recorded in LfT [i]. RIT in the same vein, record the number of pillars of the right-most continuous I-high column.
First case: ID 1 2 3 4 5 6 7h 2 1 4 5 1 3 3lft 1 1 3 4 1 6 6
RIT 1 7 4 4 7 7 7

With LfT and RIT, you can enumerate each column to calculate the maximum width of the column that extends two times. Multiply the height of the column to include the largest area of the column.



#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include < malloc.h> #include <ctype.h> #include <math.h> #include <string> #include <iostream># Include <algorithm>using namespace std, #include <stack> #include <queue> #include <vector># Include <deque> #include <set> #include <map> long long h[100010],lft[100010],rit[100010];int main () { Long Long N,m,t;while (cin>>n,n) {for (Long long i=1;i<=n;i++) {cin>>h[i]; lft[i]=rit[i]=i;} H[0]=h[n+1]=-1;for (Long long i=1;i<=n;i++) {while (h[i]<=h[lft[i]-1]) lft[i]=lft[lft[i]-1];} For (long long i=n;i>=1;i--) {while (h[i]<=h[rit[i]+1]) rit[i]=rit[rit[i]+1];} Long Long maxx=0;for (long long i=1;i<=n;i++) Maxx=max (maxx,h[i]* (rit[i]-lft[i]+1));p rintf ("%i64d\n", Maxx); for ( int i=1;i<=n;i++) {printf ("%d", Lft[i]);} Puts (""); for (int i=1;i<=n;i++) {printf ("%d", Rit[i]);} Puts ("");} return 0;} /*7 2 1 4 5 1 3 34 1000 1000 1000 10000*/ 




HDU 1506 largest Rectangle in a histogram monotone 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.