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

因此,用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. Flask flask-migrate 数据库迁移

    简介 flask-migrate是flask的一个扩展模块,主要是扩展数据库表结构的. 官方文档:http://flask-migrate.readthedocs.io/en/latest/ 使用: ...

  2. jarvisoj flag在管理员手上

    jarvisoj flag在管理员手上 涉及知识点: (1)代码审计和cookie注入 (2)哈希长度拓展攻击 解析: 进入题目的界面.看到 那么就是想方设法的变成admin了.挂上御剑开始审计.发现 ...

  3. 软件工程团队作业-测试与发布(Alpha版本)

    Alpha版本测试报告 功能测试报告 概述 测试阶段:alpha验收测试 测试方法:手动功能测试 测试环境(测试矩阵) Windows操作系统(win10)附带"Windows讲述人&quo ...

  4. 熊猫全球站视点关注,韩国投入47亿韩元开发区块链等ICT技术

    韩国科学技术信息通信部评选出18个包括区块链项目在内的政府扶持项目,为信息通信(ICT)创新企业提供技术开发支持. 熊猫全球站获悉,韩国政府计划分期选拔第一阶段(前期策划阶段3个月)和第二阶段(技术开 ...

  5. AcWing 326. XOR和路径

    大型补档计划 题目链接 如果整体来做,发现既有加法,也有整体异或,这样不容易搞. 考虑异或,各个位置互不干扰,按位考虑一下. 枚举每一位 \(k\) 发现如果设 \(f[u]\) 为这一位的期望结果还 ...

  6. Array的简单使用(Boost和STL通用)

    目录 目录 介绍 使用 Boost和STL的区别 介绍 本来这一次是想简单介绍一下Boost里面的协程库的使用的,但是Boost.Coroutine已经被废弃了,而Boost.Coroutine2目前 ...

  7. js生成随机数、随机数列、数值转金融格式

    随机数的生成:min到max之间,包括两者自身 parseInt(Math.random()*(max-min+1)+min,10); Math.floor(Math.random()*(max-mi ...

  8. react第三单元(react组件的生命周期)

    第三单元(react组件的生命周期) #课程目标 灵活掌握react组件的生命周期以及组件的活动过程. 能够灵活使用react的生命周期 #知识点 react的类组件的生命周期分为三个阶段 实例期 存 ...

  9. linux里用户权限:~$,/$,~#,/#的区别与含义

    $表明是非root用户登录,#表示是root用户登录,它们是终端shell的命令提示符几种常用终端的命令提示符 BASH:  root账户: # ,非root账户: $KSH:  root账户: # ...

  10. QEMU网络模式(一)——bridge

    网络配置 QEMU支持的网络模式 qemu-kvm主要向客户机提供了4种不同模式的网络. 1)基于网桥(bridge)的虚拟网卡; 2)基于NAT的虚拟网络 3)QEMU内置的用户模式网络(user ...