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来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...
随机推荐
- 学习笔记TF037:实现强化学习策略网络
强化学习(Reinforcement Learing),机器学习重要分支,解决连续决策问题.强化学习问题三概念,环境状态(Environment State).行动(Action).奖励(Reward ...
- go 调用windows dll 的方法
go 调用windows dll 的方法 ,代码如下: package main import ( "fmt" "syscall" "time&quo ...
- etcd和redis的比较和日常使用场景
转自https://blog.csdn.net/weixin_41571449/article/details/79429511 个人观点:etcd的红火来源于kurbernetes用etcd做服务发 ...
- js实现表单
<html> <head> <title>表单页面</title> <meta http-equiv="Content- ...
- zabbix 与 nginx (五)
zabbix监控nginx的大概流程为: 1:被监控端的nginx开启stub_status模块 2:通过脚本的方式获取nginx的状态值 3:修改被监控端的配置文件,Userparameter= ...
- win10+vs2015编译caffe的cpu debug版本、部署matcaffe
一.编译caffe 1.安装python-3.5.2-amd64.exe https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64.exe ...
- 2013-7-30 802.1X企业级加密
今天做了U9510的企业级加密标杆测试,写了企业级加密标杆设备的操作指南.最后做到server 2003却出了问题,peap能关联,但是TLS怎么都关联不上.用adb shell查看logcat日志, ...
- git 一个分支完全覆盖另一个分支
1,git push origin develop:master -f就可以把本地的develop分支强制(-f)推送到远程master 2,git checkout master // 切换到旧的分 ...
- docker入门 什么是docker? 为什么使用docker?
1.什么是docker? 轻量级操作系统虚拟化解决方案 2.为什么使用docker? 1.docker的启动是秒级的,比传统虚拟机快很多 2.资源利用率高,一台主机上可同时运行数千个docker容器 ...
- 查询 SQL_Server 所有表的记录数: for xml path
--我加了 top 10 用的时候可以去掉 declare @select_alltableCount varchar(max)='';with T as (select top 10 'SELECT ...