HDOJ 题目 4597 Play Game(动态规划,记忆化搜索)

Play Game

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 741    Accepted Submission(s): 436


Problem Description
Alice and Bob are playing a game. There are two piles of cards. There are N cards in each pile, and each card has a score. They take turns to pick up the top or bottom card from either pile, and the score of the card will be added to his total score. Alice and Bob are both clever enough, and will pick up cards to get as many scores as possible. Do you know how many scores can Alice get if he picks up first?
 

Input
The first line contains an integer T (T≤100), indicating the number of cases. 
Each case contains 3 lines. The first line is the N (N≤20). The second line contains N integer a i (1≤a i≤10000). The third line contains N integer b i (1≤b i≤10000).
 

Output
For each case, output an integer, indicating the most score Alice can get.
 

Sample Input
  
  
2 1 23 53 3 10 100 20 2 4 3
 

Sample Output
  
  
53 105
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5085  5084  5083  5082  5081 

题意

   Alice和Bob玩一个游戏,有两个长度为N的正整数数字序列,每次他们两个
   只能从其中一个序列,选择两端中的一个拿走。他们都希望可以拿到尽量大
   的数字之和,并且他们都足够聪明,每次都选择最优策略。Alice先选择,问
   最终Alice拿到的数字总和是多少?


思路

   这题应该算是区间dp吧,可以看一下这题的原型:
   其他规则都一样,但是只有一个数字序列,也是每次只能拿左右两端的一个数字,问最终Alice拿多少? (这个可以去做uva-10891)
   只有一行数字序列可以用f(i, j)表示数字序列还剩下区间[i,j]段时开始拿,最多可以拿多少数字

   而这题只是变成了两行数字序列, 那么可以在上面的基础上,再增加两维
   变成f(i, j, k, l), 表示第一个序列剩下区间[i,j],第二个序列剩下区间[k,l]的情况下开始拿,最多可以拿多少?
   当面临状态f(i, j, k, l) 时,你有四种选择:
   1. 选择第一行的最左边数字
   2. 选择第一行的最右边数字
   3. 选择第二行的最左边数字
   4. 选择第二行的最右边数字
   所以, f(i, j, k, l)可以由:
   f(i+1, j, k, l)
   f(i, j-1, k, l)
   f(i, j, k+1, l)
   f(i, j, k, l-1)
   这四种状态转移而来, 
   假设当前状态是Alice要选择,那么上一个状态就是Bob选择的最大值,
   为了要让Alice的最终和最大,那么就要选择上面四种状态最小的一个转,
   设sum(i, j, k, l) 表示地一个序列[i,j]段之和与第二个序列的[k,l]段之和的和。

   sum(i, j, k, l)  - 上一次Bob拿的值就等于Alice能拿到的值   


   f(i, j, k, l) = sum(i, j, k, l) -
       min{
           f(i+1, j, k, l)
           f(i, j-1, k, l)
           f(i, j, k+1, l)
           f(i, j, k, l-1)
         }

 
ac代码
#include<stdio.h>
#include<string.h>
int n;
int sum1[25],sum2[25],dp[25][25][25][25],a[25],b[25];
int max(int a,int b)
{
	if(a>b)
		return a;
	else
		return b;
}
int min(int a,int b)
{
	if(a>b)
		return b;
	else
		return a;
}
int dfs(int a1,int a2,int b1,int b2)
{
	int now;
	int ans=dp[a1][a2][b1][b2];
	if(a1>a2)
	{
		now=sum2[b2]-sum2[b1-1];
		if(b2==b1)
			ans=now;
	}
	else
		if(b1>b2)
		{
			now=sum1[a2]-sum1[a1-1];
			if(a2==a1)
				ans=now;
		}
		else
		{
			now=sum1[a2]-sum1[a1-1]+sum2[b2]-sum2[b1-1];
		}
		if(ans!=-1)
			return dp[a1][a2][b1][b2]=ans;
	ans=0;
	if(a1<=a2)
	{
		ans=max(ans,now-min(dfs(a1+1,a2,b1,b2),dfs(a1,a2-1,b1,b2)));
	}
	if(b1<=b2)
	{
		ans=max(ans,now-min(dfs(a1,a2,b1+1,b2),dfs(a1,a2,b1,b2-1)));
	}
	return dp[a1][a2][b1][b2]=ans;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int i;
		scanf("%d",&n);
		memset(sum1,0,sizeof(sum1));
		memset(sum2,0,sizeof(sum2));
		memset(dp,-1,sizeof(dp));
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			sum1[i]=sum1[i-1]+a[i];
		}
		for(i=1;i<=n;i++)
		{
			scanf("%d",&b[i]);
			sum2[i]=sum2[i-1]+b[i];
		}
		//ans=-1;
		printf("%d\n",dfs(1,n,1,n));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值