uva10891(sum游戏)区间dp

题目:
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][n]-a[n];
        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][n]-sum[n]);
    }

}

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][n]-sum[n];
         printf("%d\n",ans);
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值