Hdu oj 1506 Largest Rectangle in a Histogram and NYOJ 258 max length square

Source: Internet
Author: User

Train of Thought: monotonous queue, opening two Arrays: stack [] and len [] stack [] storage input long len [] storage width. In the stack, you can add and store data by order. If you encounter data smaller than the stack [top], you need to delete the top element until it can be stored at the top after deletion, before deletion, check whether the ans (l + len [top]) * stack [top] and current ans are updated to obtain the maximum value to ans ), write down the value of len [top] And give l (l + = len [top]). The new len value is l + 1. For details, refer to the code.
AC code:
[Cpp]
/* This code is nyoj ac code. If you want to change long to _ int64 in hdu oj (change the output format accordingly), hdu oj does not support long */
# Include <stdio. h>
# Include <string. h>
# Define max (a, B) a> B? A: B
Int stack [2, 100010], len [2, 100010];
Int main ()
{
Int a, B, n, h;
While (scanf ("% d", & n)
{
Long top =-1, ans = 0;
For (a = 0; a <= n; a ++)
{
If (a <n)
Scanf ("% d", & h );
Else
H =-1;
If (top <0 | stack [top] {
Stack [++ top] = h;
Len [top] = 1;
}
Else
{
Int l = 0;
While (stack [top]> = h & top> = 0)
{
Ans = max (ans, (long) (len [top] + l) * stack [top]);
L + = len [top --];
}
If (h> 0)
{
Stack [++ top] = h;
Len [top] = 1 + l;
}
}
}
Printf ("% lld \ n", ans );
}
}
Author: PIAOYI0208

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.