#476 Stone Game
来源:互联网 发布:超级球星的体测数据 编辑:程序博客网 时间:2024/06/06 02:09
题目描述:
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.
For [4, 1, 1, 4]
, in the best solution, the total score is 18
:
1. Merge second and third piles => [4, 2, 4], score +22. Merge the first two piles => [6, 4],score +63. Merge the last two piles => [10], score +10
Other two examples:[1, 1, 1, 1]
return 8
[4, 4, 5, 9]
return 43
这题提示用dp解。可以想到,用dp[i][j]来表示合并stone i ~ j所得的最小score,那么求得dp[0][A.size() - 1]就为最后的答案了。那么怎么求dp[i][j]呢?题目是merge score,可以反向思考:每一个merge以后的stone,它是怎么被merge的?它一定是找了两个merge代价最小的stone来merge的。那么可以得到dp的方程为:dp[i][j] = dp[i][k] + dp[k + 1][j] + presum[i][j]。这里presum[i][j]是指在A中i~j所有元素的和;k从i到j - 1遍历,找出所有k中得到的最小dp[i][j],就是最终所求的dp[i][j]. 从dp方程中也可以看出,决定dp[i][j]的并不是stone本身的value(因为presum[i][j]是不变的),而是上一次左右两边的stone各自merge时产生的不同score。
这里为了降低time cost,引入了visited matrix,去记录已经算过的dp[i][j];否则会超时。
Mycode(AC = 218ms):
class Solution {public: /** * @param A an integer array * @return an integer */ int stoneGame(vector<int>& A) { // Write your code here if (A.size() == 0) return 0; // initialize vectors vector<vector<int>> dp(A.size(), vector<int>(A.size(), INT_MAX)); vector<vector<bool>> visited(A.size(), vector<bool>(A.size(), false)); vector<vector<int>> presum(A.size(), vector<int>(A.size(), 0)); // get the value of presum matrix for (int i = 0; i < A.size(); i++) { presum[i][i] = A[i]; for (int j = i + 1; j < A.size(); j++) { presum[i][j] = presum[i][j - 1] + A[j]; } } stoneGame(dp, presum, visited, A, 0, A.size() - 1); return dp[0][A.size() - 1]; } void stoneGame(vector<vector<int>>& dp, vector<vector<int>>& presum, vector<vector<bool>>& visited, vector<int>& A, int start, int end) { // use visited matrix to mark whether // dp[start][end] is already computed // to save cost if (visited[start][end] || start > end) { return; } visited[start][end] = true; if (start == end) { dp[start][end] = 0; } else { for (int i = start; i < end; i++) { stoneGame(dp, presum, visited, A, start, i); stoneGame(dp, presum, visited, A, i + 1, end); // the score of start ~ end stone is equal to // previous scores of two parts + stone values from start to end dp[start][end] = min(dp[start][end], presum[start][end] + dp[start][i] + dp[i + 1][end]); } } } };
- #476 Stone Game
- Stone Game
- Stone Game
- HDOJ 1729 Stone Game
- POJ_1694_An Old Stone Game
- 1180. Stone Game
- hdu1729 Stone Game sg
- hdu 1729 Stone Game
- Hdu 1729 Stone Game
- Ural 1180. Stone Game
- Ural 1180 Stone Game
- hdu 4387 Stone Game
- hdu1729 Stone Game
- A New Stone Game
- Sicily 1839. Stone Game
- HDU4387 Stone Game
- hdu 4387 Stone Game
- UVA1378A Funny Stone Game
- 求一个数组中连续子数组的最大和
- Arduino + Lcd1602 显示当前环境温度
- 在jquery 中创建新标签和属性的方法 手记
- 8.1 enum类型
- springmvc返回json数据406错误的解决办法
- #476 Stone Game
- HashMap详细介绍(源码解析)和使用示例
- 奇计淫巧______bitset优化
- 【学习Linux】结构化命令——for
- TextView 使用setMovementMethod滑动
- scala基础-------String的操作
- 怎样找回自己CSDN丢失博客?
- virtualbox 安装 debian
- CentOS使用NTFS-3G加载NTFS硬盘