Monotone Queue Water Brush ads

Source: Internet
Author: User

"Problem description"
Recently, Afy decided to give Toj print ads, billboards are painted in the city's buildings, the city has a close to the N buildings.
Afy decided to place a billboard on top of a rectangle as large as possible. We assume that each building has a height,
From left to right give the height of each building h1,h2 ... HN, and 0

Requires the maximum area of the output billboard.


"Input File"
The first line in the input file ad.in is a number n (n<= 400,000)
The second line is the number of N, which indicates the height of each building h1,h2 ... HN, and 0"Output File"
A total of one row in the output file Ad.out, representing the maximum area of the billboard.
"Input Sample"
6
5 8 4 4 8 4
"Output Example"

24


#include <iostream> #include <cstdio> #include <algorithm> #include <cstring>using namespace STD; #define MAXN 400000 + 10int L[MAXN], r[maxn];int pos[maxn];int a[maxn];int n;void get_left () {int head = 1, tail =    0;        for (int i = 1; I <= N, i++) {while (Head <= tail && a[pos[tail]] >= a[i]) tail--;        L[i] = I-pos[tail]-1;    Pos[++tail] = i;    }}void get_right () {int head = 1, tail = 0;    Pos[tail] = n + 1;        for (int i = n, i >= 1; i--) {while (Head <= tail && a[pos[tail]] >= a[i]) tail--;        R[i] = Pos[tail]-i-1;    Pos[++tail] = i;        }}int Main () {while (~SCANF ("%d", &n) {memset (l, 0, sizeof L);        memset (r, 0, sizeof R);        for (int i = 1; I <= n; i++) scanf ("%d", A + i);        Get_left ();        Get_right ();        Long Long ans =-1; for (int i = 1; I <= n; i++) ans = max (ans, (long long) l[i] + r[i] + 1) * A[i]);    printf ("%i64d\n", ans); } return 0;} /*65 8 4 4 8 4*/


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Monotone Queue Water Brush ads

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.