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

因此,用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)学习随笔:QTreeView树形视图的wordWrap属性

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 QTreeView树形视图的wordWrap属性用于控制视图展示数据项文本的单词换行原则,如果该值为 ...

  2. PyQt(Python+Qt)学习随笔:信号签名中的万能Python类型PyQt_PyObject

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 信号在定义时参数如果使用字符串'PyQt_PyObject'表示参数可以是任何Python类型,这通 ...

  3. Syclover 第十次极客大挑战web题题解

    这次有空的时候报名参加了一下三叶草的招新比赛,比赛时间是一个月,题目都挺基础挺好玩的,在这里记一下自己的题解同时把自己没有做的题目也跟着writeup做一遍 第一题:cl4y:打比赛前先撸一只猫!: ...

  4. 对巡风vulscan的理解

    # coding:utf-8 # 漏洞检测引擎 import urllib2 import thread import time import pymongo import sys import da ...

  5. vulnstack靶机实战01

    前言 vulnstack是红日安全的一个实战环境,地址:http://vulnstack.qiyuanxuetang.net/vuln/detail/2/最近在学习内网渗透方面的相关知识,通过对靶机的 ...

  6. LeetCode初级算法之数组:189 旋转数组

    旋转数组 题目地址:https://leetcode-cn.com/problems/rotate-array/ 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数. 示例 1: 输 ...

  7. 彻底搞懂js __proto__ prototype constructor

    在开始之前,必须要知道的是:对象具有__proto__.constructor(函数也是对象固也具有以上)属性,而函数独有prototype 在博客园看到一张图分析到位很彻底,这里共享: 刚开始看这图 ...

  8. CF1439C Greedy Shopping

    题解 尝试做一下,感觉是每次取一段前缀和,这样就相当于让我们证明在 \(a_i\le 10^{12}\) 时,不可能构造出隔一个取一个的情况(\(n=10^5\)). a[i]: 1, 2, 3, 5 ...

  9. 世界上最快的排序算法——Timsort

    前言 经过60多年的发展,科学家和工程师们发明了很多排序算法,有基本的插入算法,也有相对高效的归并排序算法等,他们各有各的特点,比如归并排序性能稳定.堆排序空间消耗小等等.但是这些算法也有自己的局限性 ...

  10. sqli-labs 54-65(CHALLANGES)

    challenges less-54 less-55 less-56 less-57 less-58 less-59 less-60 less-61 less-62 less-63 less-64 l ...