思路:

dp,用记忆化搜索比较好实现。

实现:

 class Solution
{
public:
int dfs(vector<int>& sum, int cur, int M, vector<vector<int>>& dp)
{
int n = sum.size();
if (n - cur <= * M) return sum[n - ] - sum[cur];
if (dp[cur][M] != -) return dp[cur][M];
int ans = INT_MIN;
for (int i = ; i <= min( * M, n - cur); i++)
{
int tmp = dfs(sum, cur + i, max(M, i), dp);
ans = max(ans, sum[cur + i - ] - sum[cur] + sum[n - ] - sum[cur + i - ] - tmp);
}
return dp[cur][M] = ans;
}
int stoneGameII(vector<int>& piles)
{
int n = piles.size();
vector<int> sum(n + , );
vector<vector<int>> dp(n + , vector<int>(n + , -));
for (int i = ; i <= n; i++) sum[i] = sum[i - ] + piles[i - ];
int res = dfs(sum, , , dp);
return res;
}
}

leetcode1140 Stone Game II的更多相关文章

  1. HDU4388:Stone Game II(博弈+思维)

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  2. hdu 4388 Stone Game II sg函数 博弈

    Stone Game II comes. It needs two players to play this game. There are some piles of stones on the d ...

  3. hdu 4388 Stone Game II

    Stone Game II HDU - 4388 题目大意: 给出n堆物品,每堆物品都有若干件,现在A和B进行游戏,每人每轮操作一次,按照如下规则: 1. 任意选择一个堆,假设该堆有x个物品,从中选择 ...

  4. HDU 4388 Stone Game II {博弈||找规律}

    Stone Game II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. Leetcode--Last Stone Weight II

    Last Stone Weight II 欢迎关注H寻梦人公众号 You are given an array of integers stones where stones[i] is the we ...

  6. LeetCode 1049. Last Stone Weight II

    原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each ...

  7. LeetCode 1140. Stone Game II

    原题链接在这里:https://leetcode.com/problems/stone-game-ii/ 题目: Alex and Lee continue their games with pile ...

  8. Stone Game II

    Description There is a stone game.At the beginning of the game the player picks n piles of stones in ...

  9. [hdu4388]Stone Game II

    不管是否使用技能,发现操作前后所有堆二进制中1的个数之和不变.那么对于一个堆其实可以等价转换为一个k个石子的堆(k为该数二进制的个数),然后就是个nim游戏. 1 #include<bits/s ...

随机推荐

  1. [Dart] Capture and Handle Data Sequences with Streams in Dart

    Streams represent a sequence of asynchronous events. Each event is either a data event, also called ...

  2. php MySQL 删除数据表

    MySQL 删除数据表 MySQL中删除数据表是非常容易操作的, 但是你再进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失. 语法 以下为删除MySQL数据表的通用语法: DROP TA ...

  3. sql server 计算属性,计算字段的用法与解析

    SQL学习之计算字段的用法与解析   一.计算字段 1.存储在数据库表中的数据一般不是应用程序所需要的格式.大多数情况下,数据表中的数据都需要进行二次处理.下面举几个例子. (1).我们需要一个字段同 ...

  4. 案例(拖拽对话框、高清放大镜、自制滚动条、元素的隐藏方式、表格隔行变色、tab切换效果、字符串拼接、刷新评论)

    一.拖拽对话框 <style> .of{ width: 500px; } #link,#close{ text-decoration: none; margin: 0 10px; font ...

  5. bzoj 3629

    给出数 $n$记 $f(x)$ 表示 $x$ 的因子和求出所有 $x$ 使得 $f(x) = n$考虑 $x = p_1 ^{a_1} * p_2 ^ {a_2} * \cdots * p_k ^ { ...

  6. spring boot 对某个接口进行次数限制,防刷。简易版。demo。

    一般的项目 如果没有做防刷 容易被人爆接口 或者就是说没有做token防刷过滤. 容易被人用正常的token刷接口.有些token非一次性. 用户登录之后生成token会有一个过期时间,但一般没有做频 ...

  7. BIOS 中断大全

    BIOS中断: 1.显示服务(Video Service--INT 10H) 00H -设置显示器模式0CH -写图形象素  01H -设置光标形状0DH -读图形象素  02H -设置光标位置0EH ...

  8. [bzoj 4872][六省联考2017]分手是祝愿

    传送门 Description N个灯按照1~N标号,按下一个开关i,所有标号是i的约数的开关都改变状态,目标是关掉所有的灯,如果当前最优策略≤k就直接按照最优策略走.否则随机按下一个开关.给出每个灯 ...

  9. Codeforces Round #563 (Div. 2)/CF1174

    Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...

  10. [Codeforces1137F]Matches Are Not a Child's Play——LCT+树状数组

    题目链接: [Codeforces1137F]Matches Are Not a Child's Play 题目大意: 我们定义一棵树的删除序列为:每一次将树中编号最小的叶子删掉,将该节点编号加入到当 ...