[POJ2559] Largest Rectangle in a histogram

Source: Internet
Author: User

Title Link: http://poj.org/problem?id=2559

The rectangle with the largest area within the given histogram is obtained.

Using a thing called a monotone stack, the idea is much like DP, that is, each bar as a structure, the height of the current bar and its previous (left) can reach the maximum horizontal distance, and only meet the conditions when the stack.

1#include <algorithm>2#include <iostream>3#include <iomanip>4#include <cstring>5#include <climits>6#include <complex>7#include <fstream>8#include <cassert>9#include <cstdio>Ten#include <bitset> One#include <vector> A#include <deque> -#include <queue> -#include <stack> the#include <ctime> -#include <Set> -#include <map> -#include <cmath> +  - using namespacestd; +  A typedef __int64 LL; at Const intMAXN =100010; -typedefstructNode { - LL H; -     intp; - Node (ll _h, LL _p): H (_h), p (_p) {} - }; in  - inline ll Max (ll A, ll b) { to     returna > B?a:b; + } -  the intN; * LL H[MAXN]; $ LL ans;Panax NotoginsengStack<node>St; -  the intMain () { +     //freopen ("in", "R", stdin); A      while(~SCANF ("%d", &n) &&N) { theH[n] =0; +          while(!st.empty ()) St.pop (); -Ans =0; $Node TMP (-1,0); $ St.push (TMP); -          for(inti =0; I < n; i++) { -scanf"%i64d", &h[i]); the         } -LL cur =0;Wuyi          for(inti =0; I <= N; i++) { theTmp.h = H[i], TMP.P = i +1; -              while(St.top (). h >tmp.h) { WuTMP =st.top (); - St.pop (); Aboutans = max (ans, (i+1-TMP.P) *tmp.h); $             } - St.push (Node (H[i], TMP.P)); -         } -printf"%i64d\n", ans); A     } +     return 0; the}

[POJ2559] Largest Rectangle in a histogram

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.