HDU 1003 Max sum

Source: Internet
Author: User

Digress:Although it is a simple question, the more inconspicuous things are often something we ignore, but it has a non-general importance. There was nothing in the city competition, and nothing in the provincial competition. Let me return to 0. So we should start from 0 and prepare for the next year and the last year. Let's not talk about it.

Analysis: this is a question that can be used in many ways. Although I used it in the past, I used violence in my memory. Today I want to use DP, which is also the preparation for the next question.

If f (I) is set, it indicates the maximum subsequence sum between the 1st-bit and the I-bit in the sequence, sum is the sum (different from the largest subsequence and) from the beginning of a bit to the first I-1 bit, And a is the number of columns in the original sequence.

F (I) = max {f (I-1), A (I) + (sum <0? 0: Sum )}

CodeIt can be further optimized, but I didn't think so, because we can see the shadows of this question in the next question.

Specifically, the introduction of a negative infinity point in The 0th bits does not affect the result, so that the two for s can be changed to one for S.

 

Code
# Include < Iostream >
Using   Namespace STD;

Int DP [ 100001 ], [ 100001 ], Start [ 100001 ], End [ 100001 ];
Int Main ()
{
Int CAS;
CIN > CAS;
DP [ 0 ] = A [ 0 ] =- 10000 ;
For ( Int T = 1 ; T <= CAS; ++ T)
{
Int N, Sum = 0 , Temp = 1 ;
CIN > N;
For ( Int I = 1 ; I <= N; ++ I)
{
CIN > A [I];
If (DP [I - 1 ] > A [I] + Sum)
{
DP [I] = DP [I - 1 ];
Start [I] = Start [I - 1 ];
End [I] = End [I - 1 ];
}
Else
{
DP [I]=A [I]+SUM;
Start [I]=Temp;
End [I]=I;
}
Sum + = A [I];
If (Sum < 0 )
{
Sum= 0;
Temp=I+1;
}
}
Cout < " Case " < T < " : " < Endl;
Cout < DP [N] < "   " < Start [N] < "   " < End [N] < Endl;
If (T ! = CAS)
Cout < Endl;
}
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.