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来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- robot framework中的timeout的关键词
1.默认robotframework中的含有等待的关键词(如:Wait Until Element Is Enabled),未手动设置时默认该参数为5sec 2.关键词:sleep A)一般在调试的时 ...
- USG防火墙DHCP设置保留IP地址
在使用disp cur查看当前配置 然后使用undo修改 这样10.0.1.1网段到10.0.1.100之间的IP地址不会分出去
- Linux platform平台总线、平台设备、平台驱动
平台总线(platform_bus)的需求来源? 随着soc的升级,S3C2440->S3C6410->S5PV210->4412,以前的程序就得重新写一遍,做着大量的重复工作, 人 ...
- Mysql索引会失效的几种情况
1.如果条件中有or,即使其中有条件带索引也不会使用(这也是为什么尽量少用or的原因): 2.对于多列索引,不是使用的第一部分,则不会使用索引: 3.like查询是以%开头: 4.如果列类型是字符串, ...
- BVLC CaffeNet可视化及类别预测
一.介绍 bvlc_reference_caffenet网络模型是由AlexNet的网络模型改写的,输入图片尺寸大小为227x227x3,输出的为该图片对应1000个分类的概率值. 介绍参考:caff ...
- Flask--(项目准备)--添加日志
日志:记录程序运行的状态,在manage.py同级目录下创建logs文件夹 定义日志文件: import logging from logging.handlers import RotatingFi ...
- 20165312 2017-2018-2《Java程序设计》课程总结
20165312 2017-2018-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:我期望的师生关系 预备作业2:C语言基础调查和java学习展望 预备作业3:Linux安 ...
- Mike Piehl
some books were writen by him: new about this man website: http://paperstreetenterprises.com
- 如何解决python 图表中文显示乱码问题(matlplotlib 包)
目前搜到的是,下载一个字体到程序路径,设置成默认字体. https://blog.csdn.net/irene_loong/article/details/68955485 #图表显示中文设置 im ...
- 如何使用 Visual C# .NET 处理 Excel 事件
事件处理概述 Visual C# .NET 使用委派处理来自组件对象模型 (COM) 服务器的事件.委派是 Microsoft Visual Studio .NET 中的一个新概念.对于 COM 事件 ...