Hdu 1506 (Dynamic Planning, great optimization)

Source: Internet
Author: User

This question is time-out based on the general idea and must be optimized to a certain extent.

For a [I], we need to record the length of the maximum continuous value before and after a than a [I]. If each comparison is performed one by one, the data size is 100000, and the time complexity is O (n ^ 2). Timeout is inevitable. But in fact for a [I], if you want to find the next continuous value, there is no need to compare one by one, directly look at a [I-1] value on the line.

For example, if we want to see how long the sequence 2, 3, 4, and 5 can be extended in the future, we don't need to compare them with 4 and 5 one by one. When we compute 4, we have calculated that 5 is bigger than 4, because 3 is smaller than 4, and 4 can extend the length in the future, 3 can certainly be achieved (the data in the length of 4 can be extended is greater than or equal to 4, of course, it is also larger than 3 ), we can directly compare the value after the end of the final length reached 4.

During the calculation of this question, pay special attention to the hexadecimal conversion. If temp is not forcibly converted to _ int64 bit, the submission will be wa.

The idea of optimizing this question is very clever and worth learning.


[Cpp]
# Include <stdio. h>
# Define N 100005
Int a [N], s [N], e [N];
Int main ()
{
Int n;
While (scanf ("% d", & n), n)
{
Int I;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
S [I] = e [I] = I;
}
For (I = 0; I <n; I ++)
{
While (s [I]> = 1 & a [s [I]-1]> = a [I])
S [I] = s [s [I]-1];
}
For (I = n-1; I> = 0; I --)
{
While (e [I] <n-1 & a [e [I] + 1]> = a [I])
E [I] = e [e [I] + 1];
}
_ Int64 ans;
Ans = 0;
For (I = 0; I <n; I ++)
{
_ Int64 temp;
Temp = (_ int64) (e [I]-s [I] + 1) * a [I]; // mandatory conversion is required here.
If (temp> ans)
Ans = temp;
}
Printf ("% I64d \ n", ans );
}
Return 0;
}

# Include <stdio. h>
# Define N 100005
Int a [N], s [N], e [N];
Int main ()
{
Int n;
While (scanf ("% d", & n), n)
{
Int I;
For (I = 0; I <n; I ++)
{
Scanf ("% d", & a [I]);
S [I] = e [I] = I;
}
For (I = 0; I <n; I ++)
{
While (s [I]> = 1 & a [s [I]-1]> = a [I])
S [I] = s [s [I]-1];
}
For (I = n-1; I> = 0; I --)
{
While (e [I] <n-1 & a [e [I] + 1]> = a [I])
E [I] = e [e [I] + 1];
}
_ Int64 ans;
Ans = 0;
For (I = 0; I <n; I ++)
{
_ Int64 temp;
Temp = (_ int64) (e [I]-s [I] + 1) * a [I]; // mandatory conversion is required here.
If (temp> ans)
Ans = temp;
}
Printf ("% I64d \ n", ans );
}
Return 0;
}


 

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.