可以知道整体石子的总和一定的,所以一个人的得分越高,另一个人的得分就越低。不管怎么取任意时刻游戏的状态都是原始序列的一段连续子序列(即被玩家取剩下的序列)。

因此,用d(i,j)表示玩家A在i到j部分的最大和,在双方都采取最优策略的情况下,先手得分最大值。

d[i][j] = sum[i][j] - min(dp(i + k, j), dp(i, j - k)); (1 <= k <= j - i + 1)

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
int S[105], A[105], d[105][105], vis[105][105], n;
int dp(int i, int j)
{
if (vis[i][j])
return d[i][j];
vis[i][j] = 1;
int m = 0;
for (int k=i+1;k<=j;k++)
{
m=min(m,dp(k,j));
}
for(int k=i;k<j;k++)
{
m=min(m,dp(i,k));
}
d[i][j]=S[j]-S[i-1]-m;
return d[i][j];
}
int main()
{
while (scanf("%d", &n), n)
{
memset(vis, 0, sizeof(vis));
S[0]=0;
for(int i=1;i<=n;i++)
{
scanf("%d", &A[i]);
S[i]=S[i-1]+A[i];
}
printf("%d\n",2*dp(1,n)-S[n]); //dp(1, n) - (S[n] - dp(1, n))
}
return 0;
}

  

Game of Sum的更多相关文章

  1. LeetCode - Two Sum

    Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...

  2. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  3. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  4. POJ 2739. Sum of Consecutive Prime Numbers

    Sum of Consecutive Prime Numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20050 ...

  5. BZOJ 3944 Sum

    题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...

  6. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  7. [LeetCode] Partition Equal Subset Sum 相同子集和分割

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. [LeetCode] Split Array Largest Sum 分割数组的最大值

    Given an array which consists of non-negative integers and an integer m, you can split the array int ...

  9. [LeetCode] Sum of Left Leaves 左子叶之和

    Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...

  10. [LeetCode] Combination Sum IV 组合之和之四

    Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...

随机推荐

  1. PyQt(Python+Qt)学习随笔:Designer中不能编辑信号和槽的问题

    新建了一个窗口部件,在窗口上添加了相关布局,再设置窗口窗口的布局为垂直布局,窗口设计好界面后如图所示: 可以看到窗口是QWidget类,窗口上从上到下有三个布局,窗口自身的布局为垂直布局,布局名为ve ...

  2. fedora版本如何升级

    自动升级 sudo dnf update --refresh # 更新系统包 sudo dnf install dnf-plugin-system-upgrade # 安装系统更新插件 sudo dn ...

  3. C#中SQL SERVER 2008字符数据类型使用心得

    一.尽可能使用Varchar,少使用或者不使用Char字符类型 因为char类型输入的数据长度达不到设计长度,会用空格补足,下面是数据表设计图: 下面是编辑前200行的图: 凡是输入的数据长度达不到设 ...

  4. Java 线程安全问题的本质

    原创声明:作者:Arnold.zhao 博客园地址:https://www.cnblogs.com/zh94 目录: 线程安全问题的本质 理解CPU JVM虚拟机类比于操作系统 重排序 汇总 一些解释 ...

  5. windows安装程序无法将windows配置为在此计算机上运行

    ----------------------------------------------- 解决办法: 当出现如上提示的时候,按下shift+f10 会打开命令窗口,进入到C:\windows\s ...

  6. Codeforces Edu Round 51 A-D

    A. Vasya And Password 模拟题,将所缺的种类依次填入"出现次数$ >1 $"的位置,替换掉Ta即可. #include <iostream> ...

  7. 题解-SDOI2013 淘金

    题面 SDOI2013 淘金 有一个 \(X\).\(Y\) 轴坐标范围为 \(1\sim n\) 的范围的方阵,每个点上有块黄金.一阵风来 \((x,y)\) 上的黄金到了 \((f(x),f(y) ...

  8. 微信公众测试号中的url和token

    我设置的前提条件: 已购买阿里云服务器,开通了80端口,且网站已备案. 1.在网站文件中新建index.php文件.该文件内容如下: 把 define("TOKEN", " ...

  9. 移动端 better-scroll基础

    一.什么是better-scroll better-scroll 是一款重点解决移动端(已支持 PC)各种滚动场景需求的插件 #滚动原理 1. 与浏览器滚动原理一致,父容器高度固定,子元素内容撑开,必 ...

  10. React中JSX的理解

    React中JSX的理解 JSX是快速生成react元素的一种语法,实际是React.createElement(component, props, ...children)的语法糖,同时JSX也是J ...