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

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<vector>
#include<map>
#define Inf 0x3f3f3f3f
const int maxn=1e5+;
typedef long long ll;
using namespace std; int dp[][];
int sum[];
int n;
int dfs(int l,int r)
{
if(dp[l][r]!=-Inf)
{
return dp[l][r];
}
if(l>r)
{
return ;
}
int ans=-Inf;
for(int t=;t<=n;t++)
{
if(l+t<=r+)
ans=max(ans,sum[l+t-]-sum[l-]-dfs(l+t,r));
}
for(int t=;t<=n;t++)
{
if(r-t>=l-)
ans=max(ans,sum[r]-sum[r-t]-dfs(l,r-t));
}
// cout<<l<<" "<<r<<" "<<ans<<endl;
return dp[l][r]=ans;
}
int main()
{
int T;
cin>>T;
int cnt=;
while(T--)
{
scanf("%d",&n);
for(int t=;t<=n;t++)
{
for(int j=;j<=n;j++)
{
dp[t][j]=-Inf;
}
}
memset(sum,,sizeof(sum));
int x;
for(int t=;t<=n;t++)
{
scanf("%d",&x);
dp[t][t]=x;
sum[t]=sum[t-]+x;
}
dfs(,n);
printf("Case %d: %d\n",cnt++,dp[][n]);
}
return ;
}

Easy Game(记忆化搜索)的更多相关文章

  1. [hihocoder 1033]交错和 数位dp/记忆化搜索

    #1033 : 交错和 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描写叙述 给定一个数 x,设它十进制展从高位到低位上的数位依次是 a0, a1, ..., an - 1 ...

  2. UVA 825 Walking on the Safe Side(记忆化搜索)

      Walking on the Safe Side  Square City is a very easy place for people to walk around. The two-way ...

  3. UVA - 10118Free Candies(记忆化搜索)

    题目:UVA - 10118Free Candies(记忆化搜索) 题目大意:给你四堆糖果,每一个糖果都有颜色.每次你都仅仅能拿随意一堆最上面的糖果,放到自己的篮子里.假设有两个糖果颜色同样的话,就行 ...

  4. HDU 1331 Function Run Fun(记忆化搜索)

    Problem Description We all love recursion! Don't we? Consider a three-parameter recursive function w ...

  5. ural 1698. Square Country 5(记忆化搜索)

    1698. Square Country 5 Time limit: 2.0 secondMemory limit: 64 MB The first arithmetical operation ta ...

  6. 记忆化搜索 dp学习~2

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1331 Function Run Fun Time Limit: 2000/1000 MS (Java/ ...

  7. HDU 2089 不要62(数位DP&#183;记忆化搜索)

    题意  中文 最基础的数位DP  这题好像也能够直接暴力来做   令dp[i][j]表示以 j 开头的 i 位数有多少个满足条件 那么非常easy有状态转移方程 dp[i][j] = sum{ dp[ ...

  8. HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...

  9. poj 1579(动态规划初探之记忆化搜索)

    Function Run Fun Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17843   Accepted: 9112 ...

随机推荐

  1. 8月份Python招聘情况怎么样?Python爬取招聘数据,并进行分析

    前言 拉勾招聘是专业的互联网求职招聘平台.致力于提供真实可靠的互联网招聘求职找工作信息.今天我们一起使用 python 采集拉钩的 python 招聘信息,分析一下找到高薪工作需要掌握哪些技术 开发环 ...

  2. NTFS 文件系统结构

    背景 NTFS 作为一个新的文件系统,因其安全性高而受到越来越多的重视,越来越多的应用采用了NTFS 文件系统.作为一个新的文件系统,NTFS 有着许多区别于FAT32 的优点,如磁盘配额.文件系统加 ...

  3. Android Studio--家庭记账本(五)

    想到每次删除之后将表单自动初始化,却发现会导致除去已经删除的不在出现在表单上,数据库中剩余的数据还会再次出现在表单中.导致表单上的数据越来越多.后来又想到,当我实现删除之后,把表单采用全部清除,再重新 ...

  4. SQL性能优化-查询条件与字段分开执行,union代替in与or,存储过程代替union

    PS:概要.背景.结语都是日常“装X”,可以跳过直接看优化历程 环境:SQL Server 2008 R2.阿里云RDS:辅助工具:SQL 审计 概要 一个订单列表分页查询功能,单从SQL性能来讲,从 ...

  5. 前端自适应样式(reset.css)

    @charset "utf-8"; /* CSS Document */ html, body, ul, li, ol, dl, dd, dt, p, h1, h2, h3, h4 ...

  6. Schema约束, dom4j解析

    Schema是新的XML文档约束:Schema要比DTD强大很多,是DTD替代者;Schema本身也是XML文档,但Schema文档的扩展名为xsd,而不是xml .Schema功能更强大,数据类型更 ...

  7. Java—包装类/System类/Math类/Arrays类/大数据运算/Collection接口/Iterator迭代器

    基本类型包装类 8种基本类型对应的包装类如: 将字符串转成基本类型: 将基本数值转成字符串有3种方式: 基本类型直接与””相连接即可:34+" " 调用String的valueOf ...

  8. 2020-07-28:已知sqrt (2)约等于 1.414,要求不用数学库,求sqrt (2)精确到小数点后 10 位。

    福哥答案2020-07-28: 1.二分法.2.手算法.3.牛顿迭代法.基础是泰勒级数展开法.4.泰勒级数法.5.平方根倒数速算法,卡马克反转.基础是牛顿迭代法. golang代码如下: packag ...

  9. mosquitto基于SSL/TLS安全认证测试MQTT

    一.环境搭建 1.mosquitto介绍 mosquitto是一个实现了MQTT3.1协议的代理服务器,由MQTT协议创始人之一的Andy Stanford-Clark开发,它为我们提供了非常棒的轻量 ...

  10. 使用vs2008的一个问题 2008-01-08 13:13

    昨天在写控制台版的一个管理系统.语言是:c++ ,开发工具是:vs 2008 .功能简单的描述一下,它是就是简单的查询,插入,删除等操作,操作的对象是一个xml文件. 这时我要需要一个命令行参数,根据 ...