Max Sum (简单dp)

#include <stdio.h>
#define inf 999999999
int dp[100050],s[100050],pre[100050];
int Max,start,end,n;
void solve()
{
	int i,j;
	dp[1] = s[1];
	pre[1] = 0;
	Max = s[1];
	end = 1;
	for(i=2;i<=n;i++)
	{
		if(s[i] > dp[i-1] + s[i]) 
		{
			dp[i] = s[i];
			pre[i] = 0;
			if(Max < dp[i]) { Max = dp[i]; end = i;}
		}
		else{
			dp[i] = dp[i-1] + s[i];
			pre[i] = i-1;
			if(Max < dp[i]) { Max = dp[i]; end = i;}
		}		
	}	
}
int St(int x)
{
	if(pre[x] != 0)
		return St(pre[x]);
	return x;		
}
int main()
{
	int T,i,k;
	scanf("%d",&T);
	for(k=1;k<=T;k++){
		scanf("%d",&n);
		for(i=1;i<=n;i++)
			scanf("%d",&s[i]);
		solve();
		if(k != 1)
		printf("\n");
		printf("Case %d:\n%d %d %d\n",k,Max,St(end),end);	
		
	}
		
}

你可能感兴趣的:(Max Sum (简单dp))