Gym-101502J(取数博弈+dp)

题目:http://codeforces.com/gym/101502/problem/J

J. Boxes Game
time limit per test
3.0 s
memory limit per test
256 MB
input
standard input
output
standard output
Ibraheem and Husam are playing a game with group of boxes, lined next to each other on a straight line, such that each box contains a card with some value written on it. Ibraheem and Husam already know the values of all cards before the game starts.

Ibraheem and Husam take turns to play the game, Ibraheem starts first. In each turn, a player can take one box from either ends of the line, add the value of the card inside this box to his score, then remove this box from the line.

Knowing that both players play optimally, your task is to calculate the value x - y, such that x is Ibraheem’s score at the end of the game, and y is Husam’s score at the end of the game.

Input
The first line contains an integer T (1 ≤ T ≤ 500), where T is the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 103), where n is the number of boxes in the line.

The second line of each test case contains n integers a1, a2, …, an ( - 103 ≤ ai ≤ 103), giving the values of the cards inside the boxes.

Output
For each test case, print a single line containing its answer.

Example
Input
5
4
3 1 100 5
1
1000
4
-1 100 4 -5
1
-4
1
0
Output
97
1000
92
-4
0

题意:
只能从第一个或者最后一个数取并且在数组中删除这个数,删除的这个数便是他的得分,两个人分别取数, Ibraheem 和扎马洛特 ,Ibraheem 先开始 知道两个玩家都发挥最佳, 你的任务是计算值 x - y, x 是 Ibraheem 在游戏结束时的得分, 而 y 是扎马洛特的分数。游戏结束。

**思路:**dp(x, y)表示先手在区间[x, y]能取得的最大分数。当先手取完,就轮到后手去,后手一定会选择当前能令他得到最大分数的策略,其实当先手在[x, y]区间两端取走一个数,那么后手面临两个状态[x+1, y]和[x, y-1],先手想要取得最大值,一定会想让后手取这两种状态中的较小值,设[x, y]区间的数字和为sum,转移方程就是dp(x, y) = max{sum - dp(x+1, y),sum- dp(x, y-1)}。边界就是只有一个数的时候,即x==y.

关于博弈思想:当先手取后,后手取的时候,后手其实也是看做一个先手处理,只是面临的状态不同而已,这样就非常容易理解了。

代码:

#include <cstdio>  
#include <cmath>  
#include <cctype>  
#include <algorithm>  
#include <cstring>  
#include <utility>  
#include <string>  
#include <iostream>  
#include <map>  
#include <set>  
#include <vector>  
#include <queue>  
#include <stack>  
using namespace std;  
#pragma comment(linker, "/STACK:1024000000,1024000000")   
#define eps 1e-10  
#define inf 0x3f3f3f3f  
#define PI pair<int, int>   
typedef long long LL;  
const int maxn = 100+5;  
int a[maxn], dp[maxn][maxn], sum[maxn];  
int dfs(int x, int y) {  
    if(dp[x][y] != -1) return dp[x][y];  
    if(x == y) return dp[x][y] = a[x];  
    int left = dfs(x+1, y);  
    int right = dfs(x, y-1);  
    int tol = sum[y] - sum[x-1]; //[x, y]的和  
    dp[x][y] = max(tol - left, tol - right);  
    return dp[x][y];  
}  
int main() {  
    int n;  
    while(scanf("%d", &n) == 1) {  
        sum[0] = 0;  
        for(int i = 1; i <= n; ++i) {  
            scanf("%d", &a[i]);  
            sum[i] = sum[i-1] + a[i];  
        }  
        memset(dp, -1, sizeof(dp));  
        int ans = dfs(1, n);  
        printf("%d %d\n", ans, sum[n] - ans); //最终答案为2*ans-sum[n] 
    }  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值