TZOJ 5101 A Game(区间DP)
描述
Consider the following two-player game played with a sequence of N positive integers (2 <= N <= 100) laid onto a 1 x N game board. Player 1 starts the game. The players move alternately by selecting a number from either the left or the right end of the gameboar. That number is then deleted from the board, and its value is added to the score of the player who selected it. A player wins if his sum is greater than his opponents.
Write a program that implements the optimal strategy. The optimal strategy yields maximum points when playing against the "best possible" opponent. Your program must further implement an optimal strategy for player 2.
输入
Line 1: N, the size of the board
Line 2-etc: N integers in the range (1..200) that are the contents of the game board, from left to right
输出
Two space-separated integers on a line: the score of Player 1 followed by the score of Player 2.
样例输入
6
4 7 2 9
5 2
样例输出
18 11
题意
1*N的游戏盘,每个格子都有价值,玩家一次拿最左或最右,拿了删掉这个格子,问玩家1先拿,玩家2后拿,问俩人都最优可以拿多少分
题解
dp[i][j]=区间[i,j]先手-后手的差值
很容易推出dp[i][j]可由小区间得到
dp[i][j]=a[i]-dp[i+1][j];
dp[i][j]=a[j]-dp[i][j-1];
最终我们得到dp[1][n]为先手-后手的差值
设玩家1拿了V1,玩家2拿了V2
V1+V2=Σa;
V1-V2=dp[1][n];
求得
V1=(Σa+dp[1][n])/2;
V2=(Σa-dp[1][n])/2;
代码
#include<bits/stdc++.h>
using namespace std; int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int a[],dp[][]={},sum=;
for(int i=;i<=n;i++)
scanf("%d",&a[i]),sum+=a[i];
for(int len=;len<=n;len++)
for(int i=;i<=n-len+;i++)
{
int j=i+len-;
dp[i][j]=max(a[i]-dp[i+][j],a[j]-dp[i][j-]);
}
printf("%d %d\n",(sum+dp[][n])/,(sum-dp[][n])/);
}
return ;
}
TZOJ 5101 A Game(区间DP)的更多相关文章
- TZOJ 3295 括号序列(区间DP)
描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...
- 【BZOJ-4380】Myjnie 区间DP
4380: [POI2015]Myjnie Time Limit: 40 Sec Memory Limit: 256 MBSec Special JudgeSubmit: 162 Solved: ...
- 【POJ-1390】Blocks 区间DP
Blocks Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5252 Accepted: 2165 Descriptio ...
- 区间DP LightOJ 1422 Halloween Costumes
http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...
- BZOJ1055: [HAOI2008]玩具取名[区间DP]
1055: [HAOI2008]玩具取名 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1588 Solved: 925[Submit][Statu ...
- poj2955 Brackets (区间dp)
题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...
- HDU5900 QSC and Master(区间DP + 最小费用最大流)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...
- BZOJ 1260&UVa 4394 区间DP
题意: 给一段字符串成段染色,问染成目标串最少次数. SOL: 区间DP... DP[i][j]表示从i染到j最小代价 转移:dp[i][j]=min(dp[i][j],dp[i+1][k]+dp[k ...
- 区间dp总结篇
前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- CSS3 实现圆形、椭圆形、三角形等各种形状样式
CSS3 圆形 #css3-circle{ width: 150px; height: 150px; border-radius: 50%; background-color: #232323;} C ...
- 阻塞I/O、非阻塞I/O和I/O多路复用
一.阻塞I/O 首先,要从你常用的IO操作谈起,比如read和write,通常IO操作都是阻塞I/O的,也就是说当你调用read时,如果没有数据收到,那么线程或者进程就会被挂起,直到收到数据.阻塞的意 ...
- c#利用ApplicationContext类 同时启动双窗体的实现
Application类(位于System.Windows.Forms命名空间)公开了Run方法,可以调用该方法来调度应用程序进入消息循环.Run方法有三个重载 1.第一个重载版本不带任何参数,比较少 ...
- Logistic Loss的简单讨论
首先应该知道Logistic Loss和Crossing Entropy Loss本质上是一回事. 所以所谓的SoftMaxLoss就是一般二分类LogisitcLoss的推广.之所以在网络中采取这种 ...
- golang垃圾回收和SetFinalizer
golang自带内存回收机制--GC.GC通过独立的进程执行,它会搜索不再使用的变量,并释放.需要注意的是,进行GC会占用机器资源. GC是自动进行的.如果要手动进行GC,可以调用runtime.GC ...
- Linux打开文件设置
在某些情况下会要求增加Linux的文件打开数,以增加服务器到处理效率,在Linux下查看最多能打开的文件数量为: cat /proc/sys/fs/file-max 然后设置ulimit -n 文件数 ...
- Python 算术运算符
Python 算术运算符 运算结果为浮点数 除法:/ 整除: // 求余计算: % 求余运算可以用于固定时间的检测,比如说每10分钟进行一次什么样的操作,则:minute % 10 乘方运算:
- python开发购物车
1 业务需求 商品中心 显示库存的商品 商品能够加入到购物车 个人中心 购物车 修改购物车的商品 下单 完成的订单 订单详情 账户余额 2 代码实现 # 定义全局变量信息 # 商品编号信息 goods ...
- ES6中字符串模板的使用
反撇号(键盘上Tab键上面那个)基础知识 ES6引入了一种新型的字符串字面量语法,我们称之为模板字符串(template strings).除了使用反撇号字符代替普通字符串的引号 ‘ 或 ” 外,它们 ...
- Centos7 安装redis集群哨兵模式
https://blog.csdn.net/lihongtai/article/details/82826809