POJ-1678 I Love this Game!【记忆化搜索】

题意:两个人取数字,每次要取比对方取的数字大[a,b],问player1对player2的最大分差是多少。

思路:用dp[i]表示当前的人选第i个数作为第一个数字,可以获得的最大分差。这是一个先后手不断交换的过程,每次后手也会选择最优策略,因此dp[i]=c[i]-op,op表示后手作为之后的先手去取能造成的最大分差。

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxv = 1e4 + 4;

int t, n, a, b, dp[maxv], c[maxv];//dp[i]表示当前的人第一个选第i个数可以获得的最大分差

int dfs(int x)
{
	if (dp[x] != -inf)
	{
		return dp[x];
	}
	int op = -inf;
	for (int j = x + 1; j <= n; j++)
	{
		if (c[j] - c[x] >= a && c[j] - c[x] <= b)
		{
			op = max(op, dfs(j));
		}
	}
	if (op == -inf)
	{
		dp[x] = c[x];
	}
	else
	{
		dp[x] = c[x] - op;
	}
	return dp[x];
}

int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%d %d %d", &n, &a, &b);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &c[i]);
			dp[i] = -inf;
		}
		sort(c + 1, c + 1 + n);
		int ans = -inf;
		for (int i = 1; i <= n; i++)
		{
			if (c[i] >= a && c[i] <= b)
				ans = max(ans, dfs(i));
		}
		printf("%d\n", ans == -inf ? 0 : ans);
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值