Uva-10891-Game of Sum

问题描述:

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?

输入

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.

输出

For each test case, print a number, which represents the maximum difference that the first player obtained after playing this game optimally.

样例输入

4
4 -1 -20 7
 
4
1 2 3 4

样例输出

7
10
 

用d(i,j) 表示原 序列 [i...j] 在双方都采取最优的策略的情况下,先手得分的最大值(先手是相对的,指在当前局面下的第一个取的人)。

对方肯定有一种可能是得0分,因为先手可以取完所有的。

d(i,j) = sum(i,j) – min{ d(i+1,j), d(i+2,j), … , d(j,j) , d(i,j-1), d(i,j-2), … , d(i,i) , 0 }

如何理解这个方程呢?

在某一个局面下,总和是一定的,所以一个人的得分越高,另一个人的得分越低。

那么我当前的最优决策是让对方在后面的所有步骤中取得的得分和最低,即min{ d(i+1,j), d(i+2,j), … , d(j,j) , d(i,j-1), d(i,j-2), … , d(i,i) , 0 }

注意min里面的d()先手其实换人了,如d(i+1,j)就是我从左边取一个,把i+1~j的序列让给对手。其他的一个意思.

那么我的最优得分即d(i,j)就是sum(i,j) – min{ d(i+1,j), d(i+2,j), … , d(j,j) , d(i,j-1), d(i,j-2), … , d(i,i) , 0 }

最后的结果是d(i,j)-(sum(1,n)-d(i,j))即2*d(i,j)-sum(1,n)

 

此时程序的复杂度为O(n^3),可以加一个优化,使复杂度降为O(n^2)

设f(i,j)为min(d[i][j],d[i+1][j],d[i+2][j]+......+d[j][j])

g(i,j)为min(d[i][j],d[i][j-1],d[i][j-2]+......+d[i][i])

则d[i][j]=sum(i,j)-min(0,f(i+1,j),g(i,j-1))

f和g可以O1推断出来:

f[i][j]=min(d[i][j],f[i+1][j])

g[i][j]=min(d[i][j],f[i][j-1])

因此程序降为n^2

 

优化后代码如下:

#include<iostream>
#define Size 1005
using namespace std;

int n;
int a[Size];
int s[Size];
int d[Size][Size],f[Size][Size],g[Size][Size];

inline int sum(int l,int r){ return s[r]-s[l-1]; }

int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    s[0]=0;
    for(int i=1;i<=n;i++){
        s[i]=s[i-1]+a[i];
    }
    for(int i=1;i<=n;i++){//边界 
        d[i][i]=f[i][i]=g[i][i]=a[i];
    }
    for(int L=1;L<=n;L++){
        for(int i=1;i+L<=n;i++){
            int j=i+L;
            
            int m=0;//m=min(0,f(i+1,j),g(i,j-1))
            m=min(m,f[i+1][j]);
            m=min(m,g[i][j-1]);
            
            d[i][j]=sum(i,j)-m;
            
            f[i][j]=min(d[i][j],f[i+1][j]);
            g[i][j]=min(d[i][j],g[i][j-1]);
        }
    } 
    cout<<2*d[1][n]-sum(1,n);
    
    return 0;
} 

 

转载于:https://www.cnblogs.com/FuTaimeng/p/5414438.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值