描述

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)的更多相关文章

  1. TZOJ 3295 括号序列(区间DP)

    描述 给定一串字符串,只由 “[”.“]” .“(”.“)”四个字符构成.现在让你尽量少的添加括号,得到一个规则的序列. 例如:“()”.“[]”.“(())”.“([])”.“()[]”.“()[( ...

  2. 【BZOJ-4380】Myjnie 区间DP

    4380: [POI2015]Myjnie Time Limit: 40 Sec  Memory Limit: 256 MBSec  Special JudgeSubmit: 162  Solved: ...

  3. 【POJ-1390】Blocks 区间DP

    Blocks Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5252   Accepted: 2165 Descriptio ...

  4. 区间DP LightOJ 1422 Halloween Costumes

    http://lightoj.com/volume_showproblem.php?problem=1422 做的第一道区间DP的题目,试水. 参考解题报告: http://www.cnblogs.c ...

  5. BZOJ1055: [HAOI2008]玩具取名[区间DP]

    1055: [HAOI2008]玩具取名 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1588  Solved: 925[Submit][Statu ...

  6. poj2955 Brackets (区间dp)

    题目链接:http://poj.org/problem?id=2955 题意:给定字符串 求括号匹配最多时的子串长度. 区间dp,状态转移方程: dp[i][j]=max ( dp[i][j] , 2 ...

  7. HDU5900 QSC and Master(区间DP + 最小费用最大流)

    题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5900 Description Every school has some legends, ...

  8. 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 ...

  9. 区间dp总结篇

    前言:这两天没有写什么题目,把前两周做的有些意思的背包题和最长递增.公共子序列写了个总结.反过去写总结,总能让自己有一番收获......就区间dp来说,一开始我完全不明白它是怎么应用的,甚至于看解题报 ...

随机推荐

  1. #学习笔记#JSP数据交互

    #学习笔记#JSP数据交互 数据库的使用方式:   当用户在第一个页面的查询框输入查询语句点提交的时候我们是用什么样的方式完成这个查询的? 答:我们通过在第一个页面提交表单的形式,真正的数据库查询时在 ...

  2. python------SocketServer (0809)

    socket(一对一) 与socketserver 一.socketserver 1. 正式定义:The socketserver module simplifies the task of writ ...

  3. 【转】python两个 list 获取交集,并集,差集的方法

    1. 获取两个list 的交集: #方法一: a=[2,3,4,5] b=[2,5,8] tmp = [val for val in a if val in b] print tmp #[2, 5] ...

  4. MQTT研究之mosquitto:【环境搭建】

    环境信息: 1. Linux Centos7.2 环境,CPU 2核,内存8G. 2. mosquitto版本:mosquitto-1.5.4 官网:http://mosquitto.org/down ...

  5. Excel函数之vlookup的用法

    Vlookup函数用法: 实例: 要将编号对照表中的图书名称根据两表中的图书编码字段引入 订单明细中. Vlookup函数 参数一:键入一个需要搜索的字段,这里需要通过订单明细中的图书编号在编号对照离 ...

  6. Windows启动配置数据(BCD)存储文件包含一些无效信息

    Windows启动配置数据(BCD)存储文件包含一些无效信息-照牛排 http://www.zhaoniupai.com/archives/223.html 1)近来封装Windows 7,遇到挫折. ...

  7. Unity Shader Graph(二)Dissolve Effect

    此篇文章记录Dissolve Effect(溶解特效)的制作过程 软件环境 Unity 2018.1.2f1 Packages: Lightweight Render Pipeline 1.1.11 ...

  8. VIVADO 入门之仿真与逻辑分析仪使用

    多路分频器设计 在第七节的学习中,笔者带大家通过一个入门必学的流水灯实验实现,快速掌握了VIVADO基于FPGA开发板的基本流程.考虑到很多初学者并没有掌握好Vivado 下FPGA的开发流程,本章开 ...

  9. wampserver_x86_3.0.6 允许外网访问配置教程

    1.打开wamp目录下的apache配置文件中的httpd.conf 用可以看行数的编辑器打开 大概244行: 改为 <Directory /> AllowOverride none Re ...

  10. WPF dev 获取gridControl筛选后的数据

    GridControl.DataController.GetAllFilteredAndSortedRows();