1050 Loop Array Maximum sub-segment and base time limit: 1 second space limit: 131072 KB Score: 10 Difficulty: 2-level algorithm topic collection concerns the cyclic sequence consisting of n integers a[1],a[2],a[3],..., a[n], the sequence as a[i]+a[i+1]+...+a[j] Continuous sub-segments and the maximum value (the loop sequence is the number of n is enclosed in a circle, so you need to consider a sequence such as a[n-1],a[n],a[1],a[2]). When the given integer is a negative number and is 0. For example: -2,11,-4,13,-5,-2, and the largest subkey: 11,-4,13. and for 20. Input
Line 1th: Length of the integer sequence N (2 <= N <= 50000) line 2-n+1: n integers ( -10^9 <= s[i] <= 10^9)
Output
The maximum number of sub-segments and of the output loop array.
Input example
6-211-413-5-2
Output example
20
"Code":
#include <cstdio>#include<cstring>#include<queue>#include<iostream>#include<stack>#defineMAXN 1005#defineMAXM 50005#defineINF 0x3f3f3f3f#definell Long Longusing namespacestd;intN;ll A[MAXM];//One is that the maximum is not bounded, so that is the maximum contiguous sub-segment andll Max (intN) {ll DP[MAXM];DP [0]=0; ll ans=0; for(intI=1; i<=n;i++){ if(dp[i-1]>0) dp[i]=dp[i-1]+A[i]; Elsedp[i]=A[i]; Ans=Max (ans,dp[i]); } returnans;}//The other is to cross the boundary, at this time we can change the idea, ask the largest contiguous sub-segment and---> We can not find the minimum contiguous sub-segments of the bounds, so that the remaining number and not only the largest and is over-bounded continuous. //sum-bounded minimum contiguous sub-segment and 5-9 -18-7 6ll Min (intN) {ll DP[MAXM];DP [0]=0; ll ans=0; for(intI=1; i<=n;i++){ if(dp[i-1]<0) dp[i]=dp[i-1]+A[i]; Elsedp[i]=A[i]; Ans=min (ans,dp[i]); } returnans;}intMain () {scanf ("%d",&N); ll Sum=0; for(intI=1; i<=n;i++) {scanf ("%lld",&A[i]); Sum+=A[i]; } printf ("%lld\n", Max (n), sum-Min (n))); return 0;}
51nod 1050 Cyclic Array maximum sub-segments and "Annular dp/maximum sub-segments and/n-hard"