描述

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. 搭建简单的FTP服务器

    客户端部分主要使用C#提供的webclient类 (https://msdn.microsoft.com/library/system.net.webclient.aspx) 通过WebClient. ...

  2. 【Jmeter】api性能测试总结

    1.前提概念 平时常用的性能测试:api性能测试+场景性能测试:今天就说一说api性能测试 2.如何进行性能测试? 需求:对某api进行性能测试,看看最大承受的并发数,分析下图表 分析: 错误思路:当 ...

  3. python set 集合复习--点滴

    一.set特性: set是一个无序不重复的元素集合. 集合对象是一组无序排列的可哈希的值,集合成员可以做字典中的键.集合支持用in和not in操作符检查成员,由len()内建函数得到集合的基数(大小 ...

  4. Java 初始 多态

    什么是多态 简单的来说就是具有多种形态的能力的特征 package ten; public interface Day1 { public void ring(); } package ten; pu ...

  5. qsort函数排序各种类型的数据。

    qsort函数是库函数中的一员,我们先来看看官方文档是怎么写的: 其中qsort的参数void* base是传入一个数组,size_t num 为数组整体大小,size_t size 为单个元素的大小 ...

  6. laravel框架容器管理

    来自http://www.cnblogs.com/chy1000/p/7072936.html 本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章 ...

  7. AttributeError: 'module' object has no attribute 'main'

    本机环境:ubuntu16.04,  ros-kinetic $ roscore 报错 Traceback (most recent call last): File , in <module& ...

  8. k8s学习笔记之四:资源清单定义入门

    第一章.k8s中的资源 1.什么叫资源? k8s中所有的内容都抽象为资源, 资源实例化之后,叫做对象 2.在k8s中有哪些资源? 工作负载型资源(workload): Pod ReplicaSet D ...

  9. xsy子矩形

    考虑一种解题方法,枚举上下边界L,R, 然后二分答案T,我们要判断的是否存在 \[ \frac{(sum_j - sum_i)}{2 * (R - L + 1 + j - i)} \ge T \] 也 ...

  10. profile default

    SAPDBHOST = 10.199.0.26 j2ee/dbtype = hdb j2ee/dbname = ISD j2ee/dbhost = 10.199.0.26 dbs/hdb/dbname ...