Play Game

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

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
这题就是一个区间dp,因为,每一次取都,可以取两个队列的头和尾,所以,就是一个二维的区间dp,我们用dp[al][ar][bl][br]表示,从第一个数列从al 到ar,第二个数列从,bl到br,当前这个人具有第一选择权的最大分值,那么我们,可以取两个队列的头和尾,就有4个状态转移方程了!用一个记忆化搜索就可以了!
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
#define MAXN 26
int suma[MAXN],sumb[MAXN],pa[MAXN],pb[MAXN],dp[MAXN][MAXN][MAXN][MAXN];
int dfs(int al,int ar,int bl ,int br)
{
if(dp[al][ar][bl][br]!=-1)
return dp[al][ar][bl][br];
dp[al][ar][bl][br]=0;
if(al<=ar)
dp[al][ar][bl][br]=suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al+1,ar,bl,br);
if(al<=ar)
dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar-1,bl,br));
if(bl<=br)
dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl+1,br));
if(bl<=br)
dp[al][ar][bl][br]=max(dp[al][ar][bl][br],suma[ar]-suma[al-1]+sumb[br]-sumb[bl-1]-dfs(al,ar,bl,br-1));
return dp[al][ar][bl][br];
}
int main ()
{
int n,i,tcase;
scanf("%d",&tcase);
while(tcase--)
{
scanf("%d",&n);
suma[0]=sumb[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&pa[i]);
suma[i]=suma[i-1]+pa[i];
}
for(i=1;i<=n;i++)
{
scanf("%d",&pb[i]);
sumb[i]=sumb[i-1]+pb[i];
}
memset(dp,-1,sizeof(dp));
printf("%d\n",dfs(1,n,1,n));
}
return 0;
}

最新文章

  1. 【HOW】如何手工编辑InfoPath文件
  2. float、double的有效位数
  3. Junit很少出现的一个问题 No tests found matching ...
  4. 自然语言13_Stop words with NLTK
  5. rsync.conf详解
  6. PHP获取当前时间、时间戳的各种格式写法汇总[日期时间](转)
  7. ORACLE数据库创建用户名和表空间
  8. 《zw版Halcon与delphi系列原创教程》发布说明
  9. Map的三种遍历方式
  10. HTML5本地化应用开发-HTML5 Web存储详解
  11. 版本控制-cvs
  12. Drools文档(八) 规则语言参考
  13. Invalid bound statement (not found)解决方法
  14. 超级好用的前端开发测试Chrome插件-WEB前端助手(FeHelper)
  15. ●BZOJ 3676 [Apio2014]回文串
  16. FtpCopy数据定时自动备份软件(FTP定时备份)
  17. SpringBoot Mybatis 分页插件PageHelper
  18. 【html】关于锚点的一些事
  19. HDU 1166 - 敌兵布阵 - [线段树][树状数组]
  20. jmeter --上传文件

热门文章

  1. C#复习三(Day 22)
  2. JavaSE复习日记 : 循环语句(for/while/do while)
  3. mac的svn之cornerstone简易教程
  4. jquery选择器:nth-child()与空格:eq() 的区别;
  5. kafka集群配置与测试
  6. Python BeautifulSoup中文乱码问题的2种解决方法
  7. python --appium搭建环境过程 ---新手总结(大牛勿喷,新手互相交流)
  8. C语言实现约瑟夫环讨论
  9. jQuery 层级选择器 + keyCode
  10. android学习----overridePendingTransition