博客
关于我
HDU - 4597 Play Game (博弈 + 区间dp)
阅读量:284 次
发布时间:2019-03-01

本文共 2083 字,大约阅读时间需要 6 分钟。

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 ai(1≤ai≤10000). The third line contains N integer bi (1≤bi≤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

题意:Alice和Bob轮流在两个数组中取数字,每次只能取走一个数组的第一个或最后一个数字,问Alice取走数字的最大和

思路:dp[i][j][p][q] 表示第一个数组剩余 [i, j] ,第二个数组剩余 [p, q] 时,执行下一步的人可以获得的最大价值,它可以由上一步对方的最大值转移过来,4种转移方式:数组一的第一个、数组一的最后一个、数组二的第一个、数组二的最后一个。

for循环着实不好写,还是记忆化搜索8

#include 
using namespace std;typedef long long ll;const int inf = 0x3f3f3f3f;const int N = 25;int a[N], b[N], dp[N][N][N][N], n;int dfs(int i, int j, int p, int q, int sum) { if(i > j && p > q) return 0; if(dp[i][j][p][q]) return dp[i][j][p][q]; if(i <= j) { dp[i][j][p][q] = max(dp[i][j][p][q], sum - dfs(i + 1, j, p, q, sum - a[i])); dp[i][j][p][q] = max(dp[i][j][p][q], sum - dfs(i, j - 1, p, q, sum - a[j])); } if(p <= q) { dp[i][j][p][q] = max(dp[i][j][p][q], sum - dfs(i, j, p + 1, q, sum - b[p])); dp[i][j][p][q] = max(dp[i][j][p][q], sum - dfs(i, j, p, q - 1, sum - b[q])); } return dp[i][j][p][q];}int main(){// freopen("in.txt", "r", stdin); int t; scanf("%d", &t); while(t--) { scanf("%d", &n); int sum = 0; for(int i = 1; i <= n; ++i) { scanf("%d", &a[i]); sum += a[i]; } for(int i = 1; i <= n; ++i) { scanf("%d", &b[i]); sum += b[i]; } memset(dp, 0, sizeof(dp)); printf("%d\n", dfs(1, n, 1, n, sum)); } return 0;}

 

转载地址:http://kcio.baihongyu.com/

你可能感兴趣的文章
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>
mysql 添加索引
查看>>
MySQL 添加索引,删除索引及其用法
查看>>
mysql 状态检查,备份,修复
查看>>
MySQL 用 limit 为什么会影响性能?
查看>>
MySQL 用 limit 为什么会影响性能?有什么优化方案?
查看>>
MySQL 用户权限管理:授权、撤销、密码更新和用户删除(图文解析)
查看>>
mysql 用户管理和权限设置
查看>>
MySQL 的 varchar 水真的太深了!
查看>>
mysql 的GROUP_CONCAT函数的使用(group_by 如何显示分组之前的数据)
查看>>