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 ;}
\