Light OJ 1031---Easy Game(区间DP)
题目链接
http://lightoj.com/volume_showproblem.php?problem=1031
Description
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 |
题意:有n个数排成一行,现在A和B两人从两端取任意个数(每次至少取一个),直到取完所有的数,A先取,求A取得数的和比B大多少?
思路:区间DP,dp[i][j] 表示区间i~j A取得数的和比B大多少,那么可以这样分析:对于区间i~j A先取sum[k]-sum[i-1]或sum[j]-sum[k](只能从两端取),然后该B取了,即对于区间k+1~j和i~k DP转换为B比A大多少了,所以状态转移方程为 dp[i][j]=max(dp[i][j],max(sum[k]-sum[i-1]-dp[k+1][j],sum[j]-sum[k]-dp[i][k]));
代码如下:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int sum[];
int dp[][]; ///A比B大多少? int main()
{
int T,Case=;
int n;
cin>>T;
while(T--)
{
sum[]=;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&sum[i]);
sum[i]+=sum[i-];
}
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
dp[i][i]=sum[i]-sum[i-];
for(int len=;len<n;len++)
{
for(int i=;i<=n;i++)
{
if(i+len>n) break;
dp[i][i+len]=sum[i+len]-sum[i-];
for(int k=i;k<i+len;k++)
{
dp[i][i+len]=max(dp[i][i+len],max(sum[k]-sum[i-]-dp[k+][i+len],sum[i+len]-sum[k]-dp[i][k]));
}
}
}
printf("Case %d: %d\n",Case++,dp[][n]);
}
}
Light OJ 1031---Easy Game(区间DP)的更多相关文章
- Light OJ 1031 - Easy Game(区间dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1031 题目大意:两个选手,轮流可以从数组的任意一端取值, 每次可以去任意个但仅 ...
- Light OJ 1031 - Easy Game(区间DP)
题目大意: 给你一个n,代表n个数字,现在有两个选手,选手A,B轮流有有一次机会,每个选手一次可以得到一个或者多个数字,从左侧或者右侧,但是不能同时从两边取数字,当所有的数字被取完,那么游戏结束.然后 ...
- light oj 1422 Halloween Costumes (区间dp)
题目链接:http://vjudge.net/contest/141291#problem/D 题意:有n个地方,每个地方要穿一种衣服,衣服可以嵌套穿,一旦脱下的衣服不能再穿,除非穿同样的一件新的,问 ...
- LightOJ 1031 Easy Game (区间DP)
<题目链接> 题目大意: 给定一段序列,两人轮流取数,每人每次只能从序列的两端的任意一段取数,取的数字位置必须连续,个数不限,问你这两人取数的最大差值是多少. 解题分析: 每人取数时面对的 ...
- Light OJ 1033 - Generating Palindromes(区间DP)
题目大意: 给你一个字符串,问最少增加几个字符使得这个字符串变为回文串. ============================================================= ...
- Light OJ 1422 - Halloween Costumes(区间DP 最少穿几件)
http://www.cnblogs.com/kuangbin/archive/2013/04/29/3051392.html http://www.cnblogs.com/ziyi--caolu/a ...
- [Swust OJ 360]--加分二叉树(区间dp)
题目链接:http://acm.swust.edu.cn/problem/360/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- Light oj1031 Easy Game (区间dp)
题目链接:http://vjudge.net/contest/140891#problem/F A和B都足够聪明,只有我傻,想了好久才把代码和题意对应上[大哭] 代码: #include<ios ...
- Light OJ 1030 - Discovering Gold(概率dp)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...
- Light OJ 1364 Expected Cards (期望dp,好题)
题目自己看吧,不想赘述. 参考链接:http://www.cnblogs.com/jianglangcaijin/archive/2013/01/02/2842389.html #include &l ...
随机推荐
- WebApi系列~开放的CORS,跨域资源访问对所有人开放
回到目录 之前有客户问我,如何AJAX跨域post,这个问题挺有意思,在我们看来,我是不被允许的,因为它是不安全的,但随着web api的火热,这个东西也被人们一步步的接受了,确实,有时,我们的接口希 ...
- 用 flow.ci 让 Hexo 持续部署
编者按:感谢 @小小小杜 投稿,原文链接Juglans' Blog.如果你也想体验 flow.ci 的自动化持续部署,来 http://flow.ci 首页提交申请,邀请码随后会发送到邮箱:) flo ...
- css_04之显示、定位
1.显示方式:display:取值:none(隐藏,不占页面空间,脱离文档流)/block(元素变为块级)/inline(元素变为行内)/inline-block(元素变为行内块): 2.显示效果:v ...
- sql 循环处理表数据中当前行和上一行中某值相+/-
曾经,sql中循环处理当前行数据和上一行数据浪费了我不少时间,学会后才发现如此容易,其实学问就是如此,难者不会,会者不难. 以下事例,使用游标循环表#temptable中数据,然后让当前行和上一行中的 ...
- JavaScript 对象的基本知识
js对象和属性的基本定义 (function(){ $(document).ready(function(){ return "object define"; //创建对象实例 v ...
- 重构Mybatis与Spring集成的SqlSessionFactoryBean(1)
一般来说,修改框架的源代码是极其有风险的,除非万不得已,否则不要去修改.但是今天却小心翼翼的重构了Mybatis官方提供的与Spring集成的SqlSessionFactoryBean类,一来是抱着试 ...
- Codeforces Round #380 (Div. 2) 总结分享
B. Spotlights 题意 有n×m个格子的矩形舞台,每个格子里面可以安排一个演员或聚光灯,聚光灯仅可照射一个方向(俯视,上下左右).若聚光灯能照到演员,则称为"good positi ...
- TSQL 去除重复值
数据去重主要分为两种情况:第一种是保证Select的全部列无重复:第二种是select的部分字段无重复,而其他字段取特定值. 1,对select的全部字段去重,直接使用distinct 函数 2,对s ...
- 理解Docker容器的进程管理
摘要: Docker在进程管理上有一些特殊之处,如果不注意这些细节中的魔鬼就会带来一些隐患.另外Docker鼓励"一个容器一个进程(one process per container)&qu ...
- 机器学习&数据挖掘笔记_13(用htk完成简单的孤立词识别)
最近在看图模型中著名的HMM算法,对应的一些理论公式也能看懂个大概,就是不太明白怎样在一个具体的机器学习问题(比如分类,回归)中使用HMM,特别是一些有关状态变量.观察变量和实际问题中变量的对应关系, ...