Max Sum (hdu-1003)
A very interesting dp is that it is interesting because its principle is actually very simple and the final answer can be obtained after a scan.
If the sum before the current number is less than 0, the largest sum starts from the current number. Otherwise, the largest sum is the sum before the current number.
So why is it true? We assume that the current number is continuous and less than 0, then his contribution to the following is negative, so no matter how large the number is, the maximum continuity and affirmation are not as good as the previous negative contribution.
Then, the maximum value of dp [I] is updated every time, which is equivalent to the end point of the continuous sum of enumeration.
#include
#include
#include
#includeusing namespace std;int T,n,a[100005],dp[100005];int main(){ scanf("%d",&T); int kase = 0; while(T--){ scanf("%d",&n); memset(dp,0,sizeof(dp)); for(int i=1;i<=n;i++) { scanf("%d",&a[i]); } printf("Case %d:\n",++kase); int ans = -1000000000,x1,x2,l,r; dp[1] = a[1]; x1 = 1; ans = dp[1]; l=1; r=1; for(int i=2;i<=n;i++){ if(dp[i-1]<0) { x1 = i; dp[i] = a[i]; } else dp[i] = dp[i-1] + a[i]; if(ans