I Love this Game! POJ - 1678(博弈dp )

A traditional game is played between two players on a pool of n numbers (not necessarily distinguishing ones).
The first player will choose from the pool a number x1 lying in [a, b] (0 < a < b), which means a <= x1 <= b. Next the second player should choose a number y1 such that y1 - x1 lies in [a, b] (Attention! This implies y1 > x1 since a > 0). Then the first player should choose a number x2 such that x2 - y1 lies in [a, b]… The game ends when one of them cannot make a choice. Note that a player MUST NOT skip his turn.
A player’s score is determined by the numbers he has chose, by the way:
player1score = x1 + x2 + …
player2score = y1 + y2 + …
If you are player1, what is the maximum score difference (player1score - player2score) you can get? It is assumed that player2 plays perfectly.
Input
The first line contains a single integer t (1 <= t <= 20) indicating the number of test cases. Then follow the t cases. Each case contains exactly two lines. The first line contains three integers, n, a, b (2 <= n <= 10000, 0 < a < b <= 100); the second line contains n integers, the numbers in the pool, any of which lies in [-9999, 9999].
Output
For each case, print the maximum score difference player1 can get. Note that it can be a negative, which means player1 cannot win if player2 plays perfectly.
Sample Input
3
6 1 2
1 3 -2 5 -3 6
2 1 2
-2 -1
2 1 2
1 0

Sample Output
-3
0
1
题目链接
参考题解
给n个数,并给一个区间[a,b],第一次选的数要在这个区间内,然后每次选n个数中的某个数,使得这个数减去对手所选的数得到的差值在[a,b]区间内。要求第一个选手所选的数的和减去第二个人所选的数的和的最大差值。
设dp[i]表示从第i个物品开始,并且先手选第i个物品的最大值,有dp[i]=pri[i]-max{dp[j]}。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = 1e9, MAX = 1e4 + 5;
int num[MAX], dp[MAX];  //dp[i]表示从第i个物品开始,并且先手选第i个物品的最大值;
int T, a, b, n;

int Dfs(int x)
{
    if(dp[x] != -1) return dp[x];   //如果已经记录过了,那么就直接返回
    int ans = -inf;
    for(int i = x + 1; i <= n; i++)
    {
        if(num[i] - num[x] >= a && num[i] - num[x] <= b)
            ans = max(ans, Dfs(i));
    }
    return ans == -inf ? dp[x] = num[x] : dp[x] = num[x] - ans;
}

int Solve()
{
    int ans = -inf;
    for(int i = 1; i <= n; i++)
        if(num[i] >= a && num[i] <= b)
            ans = max(ans, Dfs(i));
    return ans == -inf ? 0 : ans;
}

int main()
{
    cin >> T;
    while (T--)
    {
        scanf("%d%d%d", &n, &a, &b);
        for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
        sort(num + 1, num + 1 + n);
        memset(dp, -1, sizeof(dp));
        printf("%d\n", Solve());
    }
    return 0;;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值