Maximum continuous sub-sequence DP problem of HDU 1231

Source: Internet
Author: User

Maximum continuous sub-sequence DP problem of HDU 1231

For a typical DP question, an additional requirement is added to output the starting and ending values of the subsequence.

Add a record method, nothing special.

When recording the final ans, both the start and end subscript are recorded;

When the current maximum value sum is updated, the Start Node is updated.

Const int MAX_N = 10001; long arr [MAX_N]; int N, sta, end; long getMaxSubs () {long sum = 0, ans = LLONG_MIN; int ts = 0; for (int I = 0; I <N; I ++) {sum + = arr [I]; if (ans <sum) {ans = sum; end = I; sta = ts;} if (sum <0) {sum = 0; ts = I + 1;} return ans;} int main () {while (~ Scanf ("% d", & N) {for (int I = 0; I <N; I ++) {scanf ("% I64d ", arr + I);} long ans = getMaxSubs (); if (ans <0ll) {printf ("% d % I64d % I64d \ n", 0, arr [0], arr [N-1]);} else {printf ("% I64d % I64d % I64d \ n", ans, arr [sta], arr [end]) ;}} 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.