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:

  1. At each step of the game,the player can merge two adjacent piles to a new pile.
  2. 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的更多相关文章

  1. POJ1740A New Stone Game[组合游戏]

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5769   Accepted: 3158 ...

  2. timus 1180. Stone Game 解题报告

    1.题目: 1180. Stone Game Time limit: 1.0 secondMemory limit: 64 MB Two Nikifors play a funny game. The ...

  3. 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 ...

  4. POJ 1740 A New Stone Game

    A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5453   Accepted: 2989 ...

  5. Light OJ 1296 - Again Stone Game (博弈sg函数递推)

    F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu ...

  6. poj 1115 Lifting the Stone 计算多边形的中心

    Lifting the Stone Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  7. 【POJ】A New Stone Game(博弈论)

    http://poj.org/problem?id=1740 题目大意就是,对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分 ...

  8. HDUOJ--------A simple stone game(尼姆博弈扩展)(2008北京现场赛A题)

    A simple stone game                                                                                  ...

  9. POJ 1740 A New Stone Game(普通博弈)

    A New Stone Game 题意: 对于n堆石子,每堆若干个,两人轮流操作,每次操作分两步,第一步从某堆中去掉至少一个,第二步(可省略)把该堆剩余石子的一部分分给其它的某些堆.最后谁无子可取即输 ...

  10. POJ 3922A Simple Stone Game

    题目链接 A Sample Stone Game 题目大意:给定n,k,表示最初时有n个石头,两个人玩取石子游戏,第一个人第一次可以取1~n-1个石头,后面每个人最多可以拿走前面一个人拿走的个数的K倍 ...

随机推荐

  1. KAFA 监测| Kafka监测的方法和工具

    1.目标 在我们上一篇Kafka教程中,我们讨论了Kafka Tools.今天,我们将看到Kafka Monitoring.在此,我们将学习如何监控Apache Kafka的概念.此外,我们将涵盖在故 ...

  2. 文件和异常——python从编程入门到实践

    从文件中读取数据 1. 读取整个文件 要读取文件,首先来创建一个文件: 然后打开并读取这个文件,再将其内容显示到屏幕上: file_reader.py with open('pi_digits.txt ...

  3. python实战项目 — 爬取 妹子图网,保存图片到本地

    重点: 1. 用def函数 2. 使用 os.path.dirname("路径保存") , 实现每组图片保存在独立的文件夹中 方法1: import requests from l ...

  4. 通过Anaconda安装的jupyter notebook,打开时,未能启动默认浏览器

    问题:通过Anaconda安装的jupyter notebook,通过开始菜单的快捷方式打开时,未能启动网页,需要复制url,粘贴到浏览器中才会出现工作面板. 解决方法: 修改jupyter_note ...

  5. Magic Line(思维+计算几何问题)(2019牛客暑期多校训练营(第三场))

    示例: 输入: 140 1-1 01 00 -1 输出:-1 999000000 1 -999000001 题意:给定平面上一系列的点,求一条以(x1,y1),(x2,y2)两点表示的直线将平面分为包 ...

  6. dom元素新增后不会触发事件

    <!DOCTYPE HTML> <html> <head> <title>checkbox设置只能单选</title> <script ...

  7. Python2与Python3兼容

    Python2与Python3兼容 python3写的代码如何也能在pyhon2上跑?请无论如何加上这一句,python3没有啥影响 from __future__ import absolute_i ...

  8. 0.UML图入门——学习《大话设计模式》笔记

    <大话设计模式>中讲述了UML类图的基本用法,做此笔记加深理解. 注:上图来源于<大话设计模式> 上图中设计的关键术语为:继承.实现.聚合.组合.关联.依赖. 要想弄清楚UML ...

  9. 在notepad++中编辑时光标消失不见

    在notepad++进行编辑时,会不知道的情况下,鼠标光标由竖线变成了下划线,如图 解决方法很简单,是点击”insert“键或者”ins“键,即可改变光标形状.

  10. Nginx记录post body内容

    nginx在记录http的body内容时,会将中文转义为16进制 在nginx 1.11.8 以上版本中log_format 增加了escape=json 参数,可以不转义变量内容: log_form ...