1031 - Easy Game(博弈dp)
2016-03-07 21:59
465 查看
You are playing a two player game. Initially there are n integer numbers in an array and player A and B get chance to take them alternatively. Each player can take one or more numbers from the left or right end of the array but cannot take from both ends at a time. He can take as many consecutive numbers as he wants during his time. The game ends when all numbers are taken from the array by the players. The point of each player is calculated by the summation of the numbers, which he has taken. Each player tries to achieve more points from other. If both players play optimally and player A starts the game then how much more point can player A get than player B?
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains N space separated integers. You may assume that no number will contain more than 4 digits.
Output
For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.
Sample Input
Output for Sample Input
2
4
4 -10 -20 7
4
1 2 3 4
Case 1: 7
Case 2: 10
两个煞笔玩游戏,每次只能从一边取连续的数,至少取一个,可以取完,问两人绝顶聪明下,他们得到的分数差最大多少。
每个人都想拿最多的分数,那就是子状态中对手获得最大值最小的那个状态,直接记忆化搜索就ok。
dp[l][r]表示这个人在l,r区间内先手能拿到的最大分数。
具体看代码,可能理解一些。
Input
Input starts with an integer T (≤ 100), denoting the number of test cases.
Each case contains a blank line and an integer N (1 ≤ N ≤ 100) denoting the size of the array. The next line contains N space separated integers. You may assume that no number will contain more than 4 digits.
Output
For each test case, print the case number and the maximum difference that the first player obtained after playing this game optimally.
Sample Input
Output for Sample Input
2
4
4 -10 -20 7
4
1 2 3 4
Case 1: 7
Case 2: 10
两个煞笔玩游戏,每次只能从一边取连续的数,至少取一个,可以取完,问两人绝顶聪明下,他们得到的分数差最大多少。
每个人都想拿最多的分数,那就是子状态中对手获得最大值最小的那个状态,直接记忆化搜索就ok。
dp[l][r]表示这个人在l,r区间内先手能拿到的最大分数。
具体看代码,可能理解一些。
#include<cstdio> #include<cstring> #include<iostream> #include<queue> #include<vector> #include<algorithm> #include<string> #include<cmath> #include<set> #include<map> #include<vector> #include<stack> #include<utility> #include<sstream> using namespace std; typedef long long ll; const int inf = 0x3f3f3f3f; const int maxn = 1005; int t,n; int dp[105][105],sum[105],a[105]; int dfs(int l,int r) { if(dp[l][r] != -inf)return dp[l][r]; if(l > r) return dp[l][r] = 0; if(l == r) return dp[l][r] = a[l]; int ans = -inf; for(int i = l + 1;i <= r + 1;i++) ans = max(ans,sum[r] - sum[l - 1] - dfs(i,r)); for(int i = r - 1;i >= l - 1;i--) ans = max(ans,sum[r] - sum[l - 1] - dfs(l,i)); return dp[l][r] = ans; } int main() { #ifdef LOCAL freopen("C:\\Users\\ΡΡ\\Desktop\\in.txt","r",stdin); //freopen("C:\\Users\\ΡΡ\\Desktop\\out.txt","w",stdout); #endif // LOCAL int kase = 1; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i = 1;i <= n;i++) scanf("%d",a + i); sum[0] = 0; for(int i = 0;i <= n + 2;i++) for(int j = 0;j <= n + 2;j++) dp[i][j] = -inf; for(int i = 1;i <= n;i++) sum[i] = sum[i - 1] + a[i]; printf("Case %d: %d\n",kase++,2*dfs(1,n) - sum ); } return 0; }
相关文章推荐
- java 并发编程注意事项
- Java的参数传递
- 网站的简单的母版页
- 快速排序法的总结
- 【浅墨Unity3D Shader编程】之二 雪山飞狐篇:Unity的基本Shader框架写法&颜色、光照与材质
- 20135202闫佳歆--week3 构造一个简单的Linux系统MenuOs--学习笔记
- java异步计算Future的使用(转)
- 我的第一篇博客
- ReversedDemo
- 课堂作业1
- Android照片墙应用实现,再多的图片也不怕崩溃
- 283. Move Zeroes
- 优先队列
- 我为什么要再给lua写一个json模块
- ios的一些经验记录2
- 在java静态方法中获取当前类名
- 选择排序之简单选择排序(java实现)
- 20160307 Single Number
- BestCoder Round #74 HDU 5638 Topsort
- 设计模式 - 观察者模式(Observer)