Recently title suddenly found vj brush also has a few long-ago attempt problem is not resolved, so when the water problem to make up these questions ....
Meaning of the questions:
Seeking a maximum contiguous subsequence sequence analysis, while its output range (the maximum value if there is the same section, the output i, j minimum interval)
Ideas:
Seeking a maximum contiguous subsequence: Recurrence Formula dp [i] = max (arr [i], dp [i-1] + arr [i]), represents a maximum contiguous subsequence end and i. Finally, the maximum output again traversed dp value.
About the interval range we write a structure records what you can, be updated when recursive.
code:
#include<cstdio> #include<algorithm> #include<iostream> using namespace std; #define maxn 10005 const int inf = 0x3f3f3f3f; struct node{ int l,r; int val; }dp[maxn]; int arr[maxn]; int main() { int n,mmax,flag; while(cin>>n&&n) { mmax=-inf , flag=0; for(int i=1;i<=n;i++) { CIN >> ARR [I]; IF (ARR [I]> = 0 ) = In Flag . 1 ; } // if all elements K are negative, and defines the maximum is 0, the output of the entire sequence of elements inclusive IF (! In Flag) { COUT << 0 << " " << ARR [ . 1 ] << " " << ARR [n-] << endl; Continue ; } // maximum continuous sequence DP DP [ 0 ] .L = . 1 ; for ( int I = . 1 ; I <= n-; I ++){ if(arr[i]>dp[i-1].val+arr[i]){ dp[i].val = arr[i]; dp[i].l = i; dp[i].r = i; }else{ dp[i].val = dp[i-1].val + arr[i]; dp[i].l = dp[i-1].l; dp[i].r = i; } mmax=max(dp[i].val,mmax); } int l,r; for(int i=1;i<=n;i++){ if(dp[i].val==mmax){ cout<<mmax<<" "<<arr[dp[i].l]<<" "<<arr[dp[i].r]<<endl; break; } } } return 0; }