【LightOJ - 1031】Easy Game (区间dp,博弈)

题干:

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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441086.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java web svn_如何搭建svnadmin,一个简单的svnWEB页面

Svn Admin是一个Java开发的管理Svn服务器的项目用户的web应用。安装好Svn服务器端好&#xff0c;把Svn Admin部署好&#xff0c;就可以通过web浏览器管理Svn的项目&#xff0c;管理项目的用户&#xff0c;管理项目的权限。使得管理配置Svn简便&#xff0c;再也不需要每次都到服…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐下象棋(记忆化搜索dp,dfs)

题干&#xff1a; 小乐乐一天天就知道玩&#xff0c;这一天又想玩象棋。 我们都知道马走日。 现在给定一个棋盘&#xff0c;大小是n*m,把棋盘放在第一象限&#xff0c;棋盘的左下角是(0,0),右上角是(n - 1, m - 1); 小乐乐想知道&#xff0c;一个马从左下角(0, 0)开始&#…

java 递归 时间复杂度_递归到底是怎么实现的?它的时间复杂度怎么算?

递归到底是个啥&#xff1f;常听见的一句话就是&#xff1a;自己调用自己。按照这个说法&#xff0c;写个简单的递归自己推导一下的确可以&#xff0c;但是总是有点绕&#xff0c;推着推着自己把自己陷进去了。递归函数运行时&#xff0c;实际上会进行一个压栈(思考栈的特点&am…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级)】小乐乐搭积木(状压dp)

题干&#xff1a; 小乐乐想要给自己搭建一个积木城堡。 积木城堡我们假设为n*m的平面矩形。 小乐乐现在手里有1*2&#xff0c;2*1两种地砖。 小乐乐想知道自己有多少种组合方案。 输入描述: 第一行输入整数n,m。(1<n,m<10) 输出描述: 输出组合方案数。 示例1 输…

Java 重定向 无法写入_java IO 文件读入,写入,重定向

Java代码 packagestar20110526;importjava.io.BufferedInputStream;importjava.io.BufferedOutputStream;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io…

【HDU - 1078】FatMouse and Cheese (记忆化搜索dp)

题干&#xff1a; FatMouse has stored some cheese in a city. The city can be considered as a square grid of dimension n: each grid location is labelled (p,q) where 0 < p < n and 0 < q < n. At each grid location Fatmouse has hid between 0 and 10…

获取excel名称java_使用Apache POI获取大型Excel文件的Excel工作表名称

小编典典为了显示Gagravarr的评论可能意味着什么&#xff1a;该XSSFReader包含方法XSSFReader.getSheetsData其中“返回一个迭代器&#xff0c;这将让你在把所有的不同的表&#xff0c;每个表的InputStream中只有打开时开始迭代器牵强。这是给你的时候&#xff0c;每个做关闭In…

【CodeForces - 298C】Parity Game (思维,有坑)

题干&#xff1a; You are fishing with polar bears Alice and Bob. While waiting for the fish to bite, the polar bears get bored. They come up with a game. First Alice and Bob each writes a 01-string (strings that only contain character "0" and &q…

java求小数高精度_浅谈Java中的高精度整数和高精度小数

在实际编码中&#xff0c;会遇到很多高精度的事例&#xff0c;比如&#xff0c;在计算金钱的时候就需要保留高精度小数&#xff0c;这样计算才不会有太大误差&#xff1a;在下面的代码中&#xff0c;我们验证了&#xff0c;当两个float型的数字相加&#xff0c;得到的结果和我们…

【CodeForces - 298B 】Sail (模拟,题意)

题干&#xff1a; The polar bears are going fishing. They plan to sail from (sx, sy) to (ex, ey). However, the boat can only sail by wind. At each second, the wind blows in one of these directions: east, south, west or north. Assume the boat is currently…

java中能构成循环的语句_《编程导论(Java)#183;3.2.4 循环语句》

本文全然复制《编程导论(Java)3.2.4 循环语句》的内容。除【】中的说明文字。请阅读和比較其它编程教材。我知道。假设我是一个刚開始学习的人&#xff0c;《编程导论(Java)》非常不适合自学。建议同学们阅读时&#xff0c;一定选择一本其它的书同一时候看&#xff0c;或上网。…

【CodeForces - 340B 】Maximal Area Quadrilateral (计算几何,枚举,有坑)

题干&#xff1a; Iahub has drawn a set of n points in the cartesian plane which he calls "special points". A quadrilateral is a simple polygon without self-intersections with four sides (also called edges) and four vertices (also called corners)…

php网站的编辑器,5款适合PHP使用的HTML编辑器推荐

概述Web开发中&#xff0c;很多地方都会用到HTML编辑器(富文本编辑器)&#xff0c;我也用过几种&#xff0c;如UEditor、CkEditor等。这几天看了几篇文章&#xff0c;都是关于HTML编辑器的&#xff0c;写个文章记录下。推荐的编辑器simditor这个编辑器是前几天刚看到的&#xf…

【牛客 - 301哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级 )】小乐乐和25(模拟,技巧)

题干&#xff1a; 小乐乐特别喜欢25这个数字&#xff0c;他想把所有的数字都变成25的倍数。 现在小乐乐得到一个数字&#xff0c;想问问你最少用几次操作才可以把这个数字改造成25的倍数。 对于一次操作我们可以把相邻的两位做交换&#xff0c;比如123经过一次操作之后就可以…

怎么表示一个PHP语句块,php switch语句多个值匹配同一代码块应用示例

先说说switch()语句的格式switch(表达式){case 匹配1&#xff1a;当匹配1和表达式匹配成功执行的代码;break;case 匹配2&#xff1a;当匹配2和表达式匹配成功执行的代码;break;default&#xff1a;如果case语句没有与表达式成功所执行的代码;}理解 switch 是怎样执行的非常重要…

php查找顶级分类,php 无限级分类 获取顶级分类ID,php顶级_PHP教程

php 无限级分类 获取顶级分类ID&#xff0c;php顶级有这样一个表&#xff0c;id是分类的ID&#xff0c;name是分类名称&#xff0c;pid是上级分类的ID。现在有个分类ID&#xff0c;程序要找到它上级的上级的上级……分类的ID&#xff0c;简单说就是找出顶级分类的ID。比如“新鲜…

【POJ - 2392】Space Elevator (dp,优秀的背包问题)

题干&#xff1a; The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 < K < 400) different types of blocks with which to build the tower. Each block of type i has heigh…

php三种web开发技术,三种WEB开发主流技术ASP-PHP-JSP的评价

三种Web开发主流技术ASP-PHP-JSP的评价[摘要]本文从程序开发者的角度&#xff0c;对现今社会盛行的WEB开发技术进行了分析&#xff0c;旨在让WEB 开发人员、使用者、准备学习的人对WEB开发技术有更加深入的了解&#xff0c;并为WEB应用程序开发提供指导。[关键词]WEB开发技术 A…

*【CodeForces - 1088 ABC】套题比赛,A水题B模拟C构造D交互

A. Input The only line contains the integer xx (1≤x≤100)(1≤x≤100). Output You should output two integers aa and bb, satisfying the given conditions, separated by a space. If no pair of integers satisfy the conditions above, print "-1" (wit…

php 计算前几天,php计算几分钟前、几小时前、几天前的几个函数、类分享

搜索热词一、函数实现实例1&#xff1a;代码如下:function time_tran($the_time){$now_time date("Y-m-d H:i:s",time()8*60*60);$now_time strtotime($now_time);$show_time strtotime($the_time);$dur $now_time - $show_time;if($dur < 0){return $the_tim…