# UVA 10891 Game of Sum

f[l][r] = min( dp[l+1][r] , dp[l+2][r] , dp[l+3][r]...dp[r][r]  )

g[l][r] = min( dp[l][l] , dp[l][l+1] , dp[l][l+2] , dp[l][l+3]....dp[l][r-1] )

dp[l][r] = sum - min( 0 , min(f[l][r] , g[l][r]) )

f和g存在递推式

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007

const int maxn = 109;
int n;
LL sum[maxn];
int a[maxn];
LL dp[maxn][maxn];
bool flag[maxn][maxn];

void init()
{
sum[0] = 0;
rep(i,1,n)
{
scanf("%d",&a[i]);
sum[i] = sum[i-1] + a[i];
}
Clean(flag,false);
}

LL dfs( int l , int r )
{
if ( flag[l][r] ) return dp[l][r];
if ( l == r ) return a[l];
if ( l > r ) return 0;
LL ans = sum[r] - sum[l-1];
rep(i,l,r-1)
{
ans = max( ans , sum[i]-sum[l-1] - dfs(i+1,r) );
ans = max( ans , sum[r] - sum[i] - dfs(l,i) );
}
flag[l][r] = true;
return dp[l][r] = ans;
}

int main()
{
while( cin>>n )
{
if ( !n ) break;
init();
cout<<dfs(1,n)<<endl;
}
return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007

#define mp make_pair
#define fi first
#define se second

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid int m=(l+r)>>1
const int maxn = 109;
LL a[maxn];
LL sum[maxn];
LL dp[maxn][maxn]; //先手取最大值
LL f[maxn][maxn];
LL g[maxn][maxn];
int n;
void init()
{
sum[0] = 0;
rep(i,1,n)
{
cin>>a[i];
sum[i] = sum[i-1] + a[i];
dp[i][i] = a[i];
f[i][i] = g[i][i] = inf;
}
rep(L,2,n)
{
rep(i,1,n-L+1)
{
int j = i + L - 1;
f[i][j] = min( f[i+1][j] , dp[i+1][j] );
g[i][j] = min( g[i][j-1] , dp[i][j-1] );
dp[i][j] = sum[j] - sum[i-1] - min( 0LL , min( f[i][j] , g[i][j] ) );
}
}
cout<<2*dp[1][n] - sum[n]<<endl;
}

int main()
{
while(cin>>n)
{
if ( !n ) break;
init();
}
return 0;
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：UVA 10891 Game of Sum 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)