uva-12484-cards

//http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3928

//解题思路:一个dp题,有用到滚动数组来优化空间,时间复杂度n*n,状态转移方程如下:

//rang[i][j]= max(sum(i+1,j)-rang[i+1][j]+val[i], sum(i,j-1)-rang[i,j-1]+val[j]);


//ps:本菜dp很弱,这个状态转移方程是老师提供的;


//代码如下:


#include<stdio.h>
#include<string.h>
long long re[11000],ans[11000],sum[11000],v[11000];
long long Max(long long a,long long b){return a>b?a:b;}
int main()
{
	int n,i,k;
	while(scanf("%d",&n)!=EOF)
	{
		memset(sum,0,sizeof(sum));
		memset(re,0,sizeof(re));
		memset(ans,0,sizeof(ans));
		for(i=1;i<=n;i++)
			scanf("%lld",&v[i]);
		for(i=1;i<=n;i++)
		{
			re[i]=v[i];
			sum[i]=sum[i-1]+v[i];
		}
		for(k=2;k<=n;k++)
		{
			for(i=1;i<=n-k+1;i++)
				ans[i]=Max(sum[i+k-1]-sum[i]-re[i+1]+v[i],sum[i+k-2]-sum[i-1]-re[i]+v[i+k-1]);
			for(i=1;i<=n-k+1;i++)
				re[i]=ans[i];
		}
		printf("%lld\n",ans[1]);
	}
	return 0;
}

你可能感兴趣的:(优化)