Game of Sum
可以知道整体石子的总和一定的,所以一个人的得分越高,另一个人的得分就越低。不管怎么取任意时刻游戏的状态都是原始序列的一段连续子序列(即被玩家取剩下的序列)。
因此,用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的更多相关文章
- LeetCode - Two Sum
Two Sum 題目連結 官網題目說明: 解法: 從給定的一組值內找出第一組兩數相加剛好等於給定的目標值,暴力解很簡單(只會這樣= =),兩個迴圈,只要找到相加的值就跳出. /// <summa ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- BZOJ 3944 Sum
题目链接:Sum 嗯--不要在意--我发这篇博客只是为了保存一下杜教筛的板子的-- 你说你不会杜教筛?有一篇博客写的很好,看完应该就会了-- 这道题就是杜教筛板子题,也没什么好讲的-- 下面贴代码(不 ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Partition Equal Subset Sum 相同子集和分割
Given a non-empty array containing only positive integers, find if the array can be partitioned into ...
- [LeetCode] Split Array Largest Sum 分割数组的最大值
Given an array which consists of non-negative integers and an integer m, you can split the array int ...
- [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 ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
随机推荐
- 第11.20节 Python 中正则表达式的扩展功能:后视断言、后视取反
一. 引言 在<第11.19节 Python 中正则表达式的扩展功能:前视断言和前视取反>中老猿介绍了前视断言和前视取反,与二者对应的还有后视断言和后视取反. 二. (?<=-)后视 ...
- PyQt学习随笔:Qt Designer的Edit Buddies功能
在Qt Designer的Edit菜单下有个Edit Buddies(编辑伙伴关系)子菜单,该菜单的作用是将一个Label与其他控件关联起来,当Label控件设置了快捷键时,通过Label的快捷键就可 ...
- 初识python-名片管理系统v1.0
一.项目说明 本项目分享一个简单的名片管理系统,主要是通过实现简单的功能,来学习python基础. 具体功能有:新建名片.显示全部名片.查询.修改.删除名片 通过对名片的增删改查,来快速看懂pytho ...
- 软工团队作业--Scrum冲刺集合贴
软工团队作业--Scrum冲刺集合贴 团队 团队名称:广东靓仔六强选手 团队成员: 黄清山 黄梓浩 钟俊豪 周立 邓富荣 郑焕 博客链接 Scrum 冲刺 第一篇 Scrum 冲刺 第二篇 Scrum ...
- Python(二) 安装PIL
1. 在使用PIL之前我们需先安装PIL. 在cmd中使用 pip 指令,竟报错,没有这个指令 2. 我就给环境变量加上这个指令,找到本机上安装python的位置,找到scrips文件夹, 看到里面的 ...
- "利用python进行数据分析"学习记录01
"利用python进行数据分析"学习记录 --day01 08/02 与书相关的资料在 http://github.com/wesm/pydata-book pandas 的2名字 ...
- XJOI contest 1590
首先 热烈庆祝"CSP-S 2020全国开放赛前冲刺模拟训练题1"圆满结束!!! 感谢大毒瘤周指导的题目.题目还是很不错的,部分分设置的也比较合理,各种神仙随便 \(\text{A ...
- 题解-SHOI2005 树的双中心
SHOI2005 树的双中心 给树 \(T=(V,E)(|V|=n)\),树高为 \(h\),\(w_u(u\in V)\).求 \(x\in V,y\in V:\left(\sum_{u\in V} ...
- Kubernetes Python Client 初体验之安装授权
最近想做一个基于flask的云平台管理服务器,利用python调用kubenetes提供的API来实现云平台的操作.笔者使用的是Windows,kubernetes集群安装在Ubuntu和Respbi ...
- JApiDocs(自动生成接口文档神器)
JApiDocs教程 前言 作为一名优秀的程序员来说,由于涉及到要与前端进行对接,所以避免不了的就是写接口文档.写完接口文档,一旦代码返回结果,参数等出现变动,接口文档还得随之改动,十分麻烦,违背了我 ...