POJ 1678 I Love this Game!(博弈DP)

34 篇文章 1 订阅
20 篇文章 0 订阅
I Love this Game!
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 2136 Accepted: 819

Description

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

Source


题目大意:

    有N个数,第一个人先选择一个[a, b]的数,然后再轮流选择,每次选择的数要比上一次大,并且差值要属于[a, b]。每个人的得分就是他选择的牌的分数。求先手的最大领先分数。


解题思路:

    第一次遇到博弈dp,由于每次选择的牌一定比上一次大,所以我们可以先把所有数从小到大排序。然后我们对所有先手能选择的值记忆化搜索,记忆化搜索时,对手肯定选择使你的分差最小的情况,这样状态转移就写出来了,在所有结果中取最大值即可。

AC代码

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define INF 0x3f3f3f3f

const int maxn=10000+3;
int num[maxn],N,a,b,dp[maxn];//dp[i]表示先手选i时的最大分差

int dfs(int p)
{
    if(dp[p]!=-INF)
        return dp[p];
    int ans=INF;
    for(int i=p+1;i<N;++i)//对手一定选择满足条件的使分差最小的方案
        if(num[i]-num[p]>=a&&num[i]-num[p]<=b)
            ans=min(ans,num[p]-dfs(i));
    if(ans==INF)//如果对手没有满足条件的选择,则比赛结束,分差等于这次选择的值
        ans=num[p];
    return dp[p]=ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d",&N,&a,&b);
        for(int i=0;i<N;++i)
            scanf("%d",&num[i]);
        sort(num,num+N);
        int ans=-INF;
        for(int i=0;i<N;++i)//初始化
            dp[i]=-INF;
        for(int i=0;i<N;++i)//找到所有满足初始条件的最大值
            if(num[i]>=a&&num[i]<=b)
                ans=max(ans,dfs(i));
        printf("%d\n",ans==-INF?0:ans);
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值