Stone Game

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.

For [4, 1, 1, 4], in the best solution, the total score is 18:

1. Merge second and third piles => [4, 2, 4], score +2
2. Merge the first two piles => [6, 4],score +6
3. Merge the last two piles => [10], score +10

Other two examples:
[1, 1, 1, 1] return 8
[4, 4, 5, 9] return 43

这是一道比较典型的区间类DP题目.如果从小到大合并,则这题很难选择.一个比较好的方式是自顶向下,先考虑最后一次合并的费用.每一次合并的附加费用是合并的两堆石块的总数目,即新石堆的石块数目.

我们可以枚举合并的位置,看哪个位置的成本最小,这个过程可以一直进行下去,直到一个石块的时候,合并成本是0.这中间因为枚举分割位,有非常多的重复区间.所以需要判断是否已经处理过. f的DP矩阵一开始初始化为-1,如果某个位置不是-1,则说明已经处理过,不必另外维护一个visited矩阵,另外,每次我们都要求区间和,也是一个重复性非常高的过程,可以提前求出.一个是直接求各个区间点的值,是二维矩阵,另外一个是先求前缀和数组,利用这个数组求区间和.显然后者空间复杂度低很多.代码如下:

class Solution:
    # @param {int[]} A an integer array
    # @return {int} an integer
    def stoneGame(self, A):
        # memory search
        if not A:
            return 0
        f = [[-1]*len(A) for i in xrange(len(A))]
        for i in xrange(len(A)):
            f[i][i] = 0
        cost = [0]*(len(A)+1)
        for i in xrange(1, len(A)+1):
            cost[i] = cost[i-1] + A[i-1]
        return self.search(A, f, cost, 0, len(A) - 1 )

    def search(self, A, f, cost, start, end):
        if f[start][end] >= 0:
            return f[start][end]
        import sys
        f[start][end] = sys.maxint
        for k in xrange(start,end):
           left = self.search(A, f, cost, start, k)
           right = self.search(A, f, cost,  k+1, end)
           f[start][end] = min(f[start][end], left + right + cost[end+1] - cost[start])

        return f[start][end]

时间复杂度为O(n^3),枚举两边和切分位置.空间复杂度为O(n^2),DP矩阵

时间: 2024-08-24 22:55:04

Stone Game的相关文章

POJ 1740 A New Stone Game

A New Stone Game Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5453   Accepted: 2989 Description Alice and Bob decide to play a new stone game.At the beginning of the game they pick n(1<=n<=10) piles of stones in a line. Alice and Bob

Lifting the Stone(hdoj1115)

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6104    Accepted Submission(s): 2546 Problem Description There are many secret openings in the floor which are covered by a big

Light OJ 1296 - Again Stone Game

传送门 1296 - Again Stone Game    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they a

URAL 1180. Stone Game (博弈 + 规律)

1180. Stone Game Time limit: 1.0 second Memory limit: 64 MB Two Nikifors play a funny game. There is a heap of N stones in front of them. Both Nikifors in turns take some stones from the heap. One may take any number of stones with the only condition

Openjudge NOI题库 ch0111/10 河中跳房子|NOIP2015 day2 stone

这题同时也是NOIP2015 D2T1 跳石头 stone 原题. 总时间限制: 1000ms 内存限制: 65536kB 描述 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一个岩石跳到另一个岩石.这项激动人心的活动在一条长长的笔直河道中进行,在起点和离起点L远 (1 ≤ L≤ 1,000,000,000) 的终点处均有一个岩石.在起点和终点之间,有N (0 ≤ N ≤ 50,000) 个岩石,每个岩石与起点的距离分别为Di (0 < Di < L). 在比赛过程中,奶牛轮流从起点

ural 1005 Stone Pile

这是道01背包题   ,虽然背包不会  ,但是还是看出来了,递推公式写啊写没写出来,后来一同学说是dfs,这我就开始了A了, 题意是给你n个重量是Wn的石头  让你放到两个包里面,看他们两个差值最小,并且输出这个差值. dfs代码 #include <stdio.h> int sum; int h,T; int a[100]; void dfs (int x,int y) { if(x==T) { if(y>h) h=y ; return ; } if(h==sum/2) return

Again Stone Game

Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the game and they alternate moves. In each move, a player has to select any pile and should remove at least one and no more t

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

F - Again Stone Game Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Description Alice and Bob are playing a stone game. Initially there are n piles of stones and each pile contains some stone. Alice stars the

HDU 1115 Lifting the Stone

Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5440    Accepted Submission(s): 2278 Problem Description There are many secret openings in the floor which are covered by a big

杭电1279 Stone Game(经典博弈)

Stone Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 2539    Accepted Submission(s): 741 Problem Description This game is a two-player game and is played as follows: 1. There are n boxes;