思路:

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. msf爆破

    SSH服务口令猜解: msf > use auxiliary/scanner/ssh/ssh_loginmsf auxiliary(ssh_login) > show optionsmsf ...

  2. 066_调整虚拟机内存参数的 shell 脚本

    #!/bin/bash#脚本通过调用 virsh 命令实现对虚拟机的管理,如果没有该命令,需要安装 libvirt-client 软件包 cat << EOF1.调整虚拟机最大内存数值2. ...

  3. WallpaperEngine 导入 mp4 文件出现 failed opening video

    WallpaperEngine 导入 mp4 文件出现 failed opening video 如图 下载这个插件 链接:点击打开链接 密码:dkf8

  4. LOJ#2977. 「THUSCH 2017」巧克力(斯坦纳树+随机化)

    题目 题目 做法 考虑部分数据(颜色较少)的: 二分中位数\(mid\),将\(v[i]=1000+(v[i]>mid)\) 具体二分操作:然后求出包含\(K\)种颜色的联通快最小的权值和,判断 ...

  5. NOIP2018普及组初赛解题报告

    本蒟蒻参加了今年的NOIP2018普及组的初赛 感觉要凉 总而言之,今年的题要说完全没有难度倒也不至于,还有不少拼RP的题,比如第一次问题求解考逻辑推理,第一次完善程序考双链表等 下面我就和大家一起看 ...

  6. 构造器Constructor

    构造器Constructor是否可被override构造器Constructor不能被继承,因此不能重写Overriding,但可以被重载Overloading 首先,构造器是不能被继承的,因为每个类 ...

  7. Fluent 时间步长【转载】

    转载自:http://blog.sina.com.cn/s/blog_4ada3be301011rjp.html 用FLUENT计算非稳态问题,是不是在计算时必须保证在每个时间步timestep里都要 ...

  8. 十一月百度杯pwnme 详细wp

    目录 程序基本信息 程序溢出点 整体思路 exp脚本 成功获得flag 参考 程序基本信息 可以看到开启了栈不可执行和got表不可写保护. 程序溢出点 在函数sub_400AF7中,v8可以读入0x1 ...

  9. gacutil.exe的位置

    如果我们需要用gacutil去注册dll ,就需要使用Visual Studio的Command Prompt,前提是需要安装Visual Studio,但是客户端上一般是没有安装VS的,所以你就需要 ...

  10. [Ubuntu] A start job is running for...interfaces

    CPU:RK3288 系统:Linux 移植 Ubuntu 16.04 到嵌入式平台,如果以太网有问题,在这里会耗时大约5分钟 开机后可以修改 Ubuntu 配置来缩短时间 打开下面的文件,可以看到最 ...