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 ...
随机推荐
- JAVA课堂作业(2019.10.21)
1. 代码: package class20191021; class Grandparent { public Grandparent() { System.out.println("Gr ...
- JAVA课堂作业(2019.10.14)
一. (1)代码 package class20191014; import java.util.Scanner; public class ClassHomework { public static ...
- leetcode(三)——2020.05.31
(上周7道题完成) 本周leetcode题目(from leetcode hot100): 15 17 19 21 33 94 42 额外完成: 22(回溯), 8, 31,200
- Nginx 转发时的一个坑,运维居然让我背锅!!
最近遇到一个 Nginx 转发的坑,一个请求转发到 Tomcat 时发现有几个 http header 始终获取不到,导致线上出现 bug,运维说不是他的问题,这个锅我背了. 新增的几个 header ...
- 【题解】GRE Words(UVA1502)
稍微有点难度--不过没有孔姥爷毒瘤( 题意 给定一个单词表,每个单词有权值,取出一部分(不改变顺序)使得这部分的每一个字符串都是后一个的子串,问得到的最大权值. 思路 设 f[i] 表示选了第 i 个 ...
- 【题解】「UVA11626」Convex Hull
凸包模板题. 之前写过拿 Graham 算法求凸包的,为了不重复/多学点知识,那这次拿 Andrew 算法求凸包吧qaq *此文章所有图片均为作者手画. Andrew 算法 假设我们有这些点: 首先把 ...
- P6007 [USACO20JAN]Springboards G
本题解仅用与作者加深算法理解,也欢迎大家的阅读 做题背景 原本关于二维的点的 \(dp\) 问题一直都没有什么想法,昨天晚上再做一道 \(cdq\) 的题目的时候被同学询问了这道题,发现可以用二维偏序 ...
- 算法-数位dp
算法-数位dp 前置知识: \(\texttt{dp}\) \(\texttt{Dfs}\) 参考文献 https://www.cnblogs.com/y2823774827y/p/10301145. ...
- nginx优化-转载
(1)nginx运行工作进程个数,一般设置cpu的核心或者核心数x2 如果不了解cpu的核数,可以top命令之后按1看出来,也可以查看/proc/cpuinfo文件 grep ^processor / ...
- 【Django Python版本对应】
使用Python36 时应该使用Django版本1.11.4 pip install django==1.11.4 版本对应表: Django version Python versions 1.8 ...