题干:

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 Nspace 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

2

4

4 -10 -20 7

4

1 2 3 4

Sample Output

Case 1: 7

Case 2: 10

解题报告:

做过的原题【UVA - 10891 Game of Sum 】【HRBUST - 1622】 Alice and Bob 不解释了、、

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
const int INF = 0x3f3f3f3f;
ll dp[205][205];
ll sum[205],a[205];
ll dfs(int l,int r) {if(dp[l][r] != -1) return dp[l][r];if(l == r) return dp[l][l]=a[l];ll maxx = -INF;for(int i = l+1; i<=r; i++) {maxx = max(maxx , sum[r] - sum[l-1] - dfs(i,r));}for(int i = r-1; i>=l; i--) {maxx = max(maxx , sum[r] - sum[l-1] - dfs(l,i));}maxx = max(maxx,sum[r] - sum[l-1]);return dp[l][r] = maxx;
}
int main()
{int t,n;cin>>t;int iCase = 0;while(t--) {scanf("%d",&n);memset(sum,0,sizeof sum);memset(dp,-1,sizeof dp);for(int i = 1; i<=n; i++) scanf("%lld",&a[i]),sum[i] = sum[i-1] + a[i];printf("Case %d: %lld\n",++iCase,2*dfs(1,n) - sum[n]);}return 0 ;
}

【LightOJ - 1031】Easy Game (区间dp,博弈)相关推荐

  1. [AtCoder Grand Contest 048] D - Pocky Game(区间dp + 博弈)

    problem AtCoder solution 注意:本题不是平等博弈,因为先手只能取最左边,后手只能取最右边. 设 f[l][r][k]:f[l][r][k]:f[l][r][k]: 只剩下区间 ...

  2. LightOJ 1422:Halloween Costumes(区间DP入门)

    http://lightoj.com/volume_showproblem.php?problem=1422 题意:去参加派对,有n场派对,每场派对要穿第wi种衣服,可以选择外面套一件,也可以选择脱掉 ...

  3. lightoj 1044 - Palindrome Partitioning(需要优化的区间dp)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1044 题意:求给出的字符串最少能分成多少串回文串. 一般会想到用区间dp暴力3个for ...

  4. LightOJ - 1422 (区间DP)

    题意:有t组数据,对于每组,有n个聚会需要参加,下面依次是参加各个聚会需要的衣服编号,要求所需要的衣服一定穿在外面,在操作的时候,可以选择穿上一件衣服或脱下一件衣服,脱下的衣服不能继续使用,问最少需要 ...

  5. CF - D. Letter Picking(博弈 + 区间dp)

    https://codeforces.com/problemset/problem/1728/D 题意 给定长度为 n 的字符串 S,n 为偶数.Alice 和 Bob 初始分别都有一个空串. 现在 ...

  6. hdu 4597 + uva 10891(一类区间dp)

    题目链接:http://vjudge.net/problem/viewProblem.action?id=19461 思路:一类经典的博弈类区间dp,我们令dp[l][r]表示玩家A从区间[l, r] ...

  7. 20171216区间dp(有错请各位大佬指正)

    菜鸡蜕变从今天开始.昨天成哥在群里发了道题,佳哥说用区间dp做.我先尝试暴力.=================  暴力时间太长 达到规定的10^9数量级辅助程序消耗30分钟,且输出文件不知名损坏(? ...

  8. Brackets POJ - 2955 (区间DP+最大括号匹配子序列)

    传送门 题目:给一个长度n(<=100)的只包含'[',']','(',)'的字符串,求最长的完全匹配的子序列.输出长度 题解:区间dp,dp[i][j]表示i~j的最长匹配数,一支dp[i][ ...

  9. POJ 2955 Brackets (区间DP)

    题目链接:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  10. 0x53. 动态规划 - 区间DP(习题详解 × 8)

    整理的算法模板合集: ACM模板 点我看算法全家桶系列!!! 实际上是一个全新的精炼模板整合计划 文章目录 0x53. 动态规划 - 区间DP Problem A. 最优矩阵链乘 Problem B. ...

最新文章

  1. 对用2遍dfs求有向图强连通分量的理解
  2. linux 查找文件夹_用python打造一个基于socket的文件(夹)传输系统
  3. 三款新iPhone电池通过认证:容量感人
  4. SQL自动检查神器,再也不用担心SQL出错了,自动补全、回滚等功能大全
  5. tomcat7.0设置多域名指向同IP不同项目
  6. 分享一个游戏源码、游戏服务端、下载网站
  7. Alluxio的Raft HA实现
  8. Win8右键反应慢的原因与解决办法
  9. Android实现App版本自动更新
  10. 阅读代码时,用excel做笔记。
  11. 海兰一体计算机配置,新一代办公神器!海兰一体机G40 plus为何受职场人士追捧?...
  12. ”35岁没500万存款就是失败?“,35岁职场人真实存款流出!
  13. Android -- 读取assets文件夹下的资源
  14. Kubernetes inotify watch 耗尽
  15. s4 android4.4.2,三星Galaxy S4(GT-I9500)开始Android 4.4.2升级
  16. navicat mysql 存储过程_navicat怎么创建存储过程
  17. DataFrame的行列转置
  18. spring security技术分享
  19. AARCH64 常用的指令和寄存器描述
  20. React--key值详解

热门文章

  1. Js——elementFromPoint方法
  2. ASP.NET 安全认证(二)——灵活运用deny与allow 及保护.htm等文件
  3. C语言的fgets 与 gets
  4. 1189C. Candies
  5. 7-1 模拟EXCEL排序 (25 分)
  6. java 正则表达式 替换字符串img标签的路径_正则表达式和有限自动机的应用
  7. ib网卡命令_infiniband交换机配置命令总结
  8. python调用mysql数据库sql语句过长有问题吗_python连接MYSQL数据库,调用update语句后无法更新数据,解决...
  9. margin background_margin:auto与布局展示
  10. 卷积核_漫画:卷积神经网络中的卷积核到底是什么?