Stone Game
Description
There is a stone game.At the beginning of the game the player picks n piles of stones in a line.
The goal is to merge the stones in one pile observing the following rules:
- At each step of the game,the player can merge two adjacent piles to a new pile.
- The score is the number of stones in the new pile.
You are to determine the minimum of the total score.
Example
Example 1:
Input: [3, 4, 3]
Output: 17
Example 2:
Input: [4, 1, 1, 4]
Output: 18
Explanation:
1. Merge second and third piles => [4, 2, 4], score = 2
2. Merge the first two piles => [6, 4],score = 8
3. Merge the last two piles => [10], score = 18
思路:
区间动态规划.
设定状态: f[i][j] 表示合并原序列 [i, j] 的石子的最小分数
状态转移: f[i][j] = min{f[i][k] + f[k+1][j]} + sum[i][j], sum[i][j] 表示原序列[i, j]区间的重量和
边界: f[i][i] = 0, f[i][i+1] = sum[i][i+1]
答案: f[0][n-1]
public class Solution {
/**
* @param A an integer array
* @return an integer
*/
int search(int l, int r, int[][] f, int[][] visit, int[][] sum) {
if (visit[l][r] == 1)
return f[l][r];
if (l == r) {
visit[l][r] = 1;
return f[l][r];
}
f[l][r] = Integer.MAX_VALUE;
for (int k = l; k < r; k++) {
f[l][r] = Math.min(f[l][r], search(l, k, f, visit, sum) + search(k + 1, r, f, visit, sum) + sum[l][r]);
}
visit[l][r] = 1;
return f[l][r];
}
public int stoneGame(int[] A) {
if (A == null || A.length == 0) {
return 0;
}
int n = A.length;
// initialize
int[][] f = new int[n][n];
int[][] visit = new int[n][n];
for (int i = 0; i < n; i++) {
f[i][i] = 0;
}
// preparation
int[][] sum = new int[n][n];
for (int i = 0; i < n; i++) {
sum[i][i] = A[i];
for (int j = i + 1; j < n; j++) {
sum[i][j] = sum[i][j - 1] + A[j];
}
}
return search(0, n-1, f, visit, sum);
}
}
Stone Game的更多相关文章
- POJ1740A New Stone Game[组合游戏]
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5769 Accepted: 3158 ...
- timus 1180. Stone Game 解题报告
1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- POJ 1740 A New Stone Game
A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5453 Accepted: 2989 ...
- Light OJ 1296 - Again Stone Game (博弈sg函数递推)
F - Again Stone Game Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- poj 1115 Lifting the Stone 计算多边形的中心
Lifting the Stone Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- 【POJ】A New Stone Game(博弈论)
http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...
- HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)
A simple stone game ...
- POJ 1740 A New Stone Game(普通博弈)
A New Stone Game 题意: 对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆.最后谁无子可取即输 ...
- POJ 3922A Simple Stone Game
题目链接 A Sample Stone Game 题目大意:给定n,k,表示最初时有n个石头,两个人玩取石子游戏,第一个人第一次可以取1~n-1个石头,后面每个人最多可以拿走前面一个人拿走的个数的K倍 ...
随机推荐
- 升级nginx1.12为1.161版本
升级nginx1.12为1.161版本 一.添加源 到 cd /etc/yum.repos.d/ 目录下 新建nginx.repo 文件 vim nginx.repo 输入以下信息 [nginx-st ...
- ubuntu 使用新添加的用户登录只有$解决方法
在ubuntu中,使用useradd新建的用户,默认使用的shell是dash,导致界面不美观,操作也不舒服. 情况如下: 只有美元符,不显示用户,很多乱码,且文件没有颜色. 解决方法,将该用户使用的 ...
- MySQL直方图
MySQL8.0开始支持索引之外的数据分布统计信息可选项 我们知道,在DB中,优化器负责将SQL转换为很多个不同的执行计划,完了从中选择一个最优的来实际执行.但是有时候优化器选择的最终计划有可能随着D ...
- Mybatis @One注解使用
@One注解:一对一关联查询
- Jmeter+Ant+Jenkins构建接口自动化测试平台(Windows)
一.首先先介绍下我的环境: 1. win10系统 2. ant版本:apache-ant-1.10.1(作用:执行脚本,便于后期的持续集成,下载地址:http://ant.apache.org/bin ...
- 如何用navicat导入数据?
介绍了如何使用navicat导入数据到数据库 0背景介绍 这里用的软件版本号是11.2.7 1选择要导入的数据库,右击选择导入向导 2 选择导入数据文件的类型 根据要导入数据文件的类型,选择对应的导入 ...
- SASS系列之:!global VS !deafult
先脑补两组场景. 场景一: 同事们每天中午都会外出吃饭.通常情况下都会先问,去哪儿吃啊?不知道啊?下楼再说吧.到了楼下好不容易有个人站出来说,既然没人说我可就说了啊,咱们去吃香草香草吧.没人反对就去, ...
- 有关mysql的utf8和utf8mb4,以及Illegal mix of collations for operation 'like'
参考以下几个帖子: https://www.cnblogs.com/install/p/4417527.html https://blog.csdn.net/Yetmoon/article/detai ...
- JavaScript中匿名函数this指向问题
this对象是在运行时基于函数执行环境绑定的,在全局函数中,this=window,在函数被作为某个对象的方法调用时,this等于这个对象. 但是匿名函数的执行环境是全局性的,所以匿名函数的this指 ...
- [LeetCode] 448. 找到所有数组中消失的数字 ☆
描述 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您 ...