您的位置:首页 > 其它

uva10891(sum游戏)区间dp

2016-03-08 19:15 253 查看
题目:

This is a two player game. Initially there are n integer numbers in an array and players A and B get

chance to take them alternatively. Each player can take one or more numbers from the left or right end

of the array but cannot take from both ends at a time. He can take as many consecutive numbers as

he wants during his time. The game ends when all numbers are taken from the array by the players.

The point of each player is calculated by the summation of the numbers, which he has taken. Each

player tries to achieve more points from other. If both players play optimally and player A starts the

game then how much more point can player A get than player B?

Input

The input consists of a number of cases. Each case starts with a line specifying the integer n (0 <

n ≤ 100), the number of elements in the array. After that, n numbers are given for the game. Input is

terminated by a line where n = 0.

Output

For each test case, print a number, which represents the maximum difference that the first player

obtained after playing this game optimally.

Sample Input

4

4 -10 -20 7

4

1 2 3 4

0

Sample Output

7

10

题意:

给出n(n<=100)个数,有A,B两个人,两个人都可以从这些数的左右两个边界取任意多个数,A先取,如果两个都足够聪明,都是按照最优策略取数,那么最后A的分数减去B的分数是多少.

分析:

定义d(i,j),表示原始数列第i~j个元素组成的子序列,在双方都采取最优策略的情况下,先手得分的最大值。

状态转移时,我们需要枚举从左边取还是从右边取以及取多少个,这等于枚举给对方剩下怎样的子序列,

是(k,j) (i< k<=j,用0表示全部取完)还是(i,k)(i<=k< j,用0表示全部取完);

因此状态转移方程是:

sum[i][j]表示从i到j的和。

d(i,j)=sum[i][j]-min(d(i,k),d(k+1,j),…….,0)

ac代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int a[105],dp[105][105];

int main()
{
//freopen("test.txt","r",stdin);
int n;
while(~scanf("%d",&n)&&n)
{
a[0]=0;
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
int num;
scanf("%d",&num);
a[i]=a[i-1]+num;
}
for(int k=1;k<=n;k++)
{
for(int i=1;i<=n-k+1;i++)
{
int mmin=0;
for(int j=i+1;j<=i+k-1;j++)
mmin=min(dp[j][i+k-1],mmin);
for(int j=i;j<i+k-1;j++)
mmin=min(dp[i][j],mmin);
dp[i][i+k-1]=a[i+k-1]-a[i-1]-mmin;
}
}
int ans=2*dp[1]
-a
;
printf("%d\n",ans);
}
return 0;
}


ac代码2:

#include<iostream>
#include<cstdio>
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f;
int a[maxn],d[maxn][maxn],sum[maxn];
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
d[i][i]=a[i];
d[i][i-1]=0;
sum[i]=sum[i-1]+a[i];
}
for(int i=n-1;i>0;i--)
{
for(int j=i+1;j<=n;j++)
{
int mmin=inf;
for(int k=i+1;k<=j+1;k++)
mmin=min(mmin,d[k][j]);
for(int k=j-1;k>=i-1;k--)
mmin=min(mmin,d[i][k]);
d[i][j]=sum[j]-sum[i-1]-mmin;
}
}
printf("%d\n",2*d[1]
-sum
);
}

}


ac2还可以直接这样写

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=105;
const int inf=0x3f3f3f3f;
int d[maxn][maxn],a[maxn],sum[maxn];
int main()
{
//freopen("in.txt","r",stdin);
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
}
memset(d,0,sizeof(d));
for(int i=1;i<=n;i++)
d[i][i]=a[i];
for(int i=n-1;i>0;i--)
for(int j=i+1;j<=n;j++)
{
int mmin=inf;
for(int k=i+1;k<=j;k++)
mmin=min(mmin,d[k][j]);
for(int k=i;k<j;k++)
mmin=min(mmin,d[i][k]);
mmin=min(mmin,0);             //全部取完
d[i][j]=sum[j]-sum[i-1]-mmin;
}
int ans=2*d[1]
-sum
;
printf("%d\n",ans);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: