Coins in a Line III

本文探讨了一个经典的博弈论问题,通过动态规划解决硬币游戏中的最优策略问题。游戏规则为两名玩家轮流从一排硬币的两端取硬币,目标是获得比对手更多的钱。文章详细解析了如何利用动态规划确定先手玩家是否能赢得比赛,并提供了具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

There are n coins in a line. Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.

Could you please decide the first player will win or lose?

Given array A = [3,2,2], return true.

Given array A = [1,2,4], return true.

Given array A = [1,20,4], return false.

Coins in a Line三步曲的最后一步,搞懂了前面两题的思路这题其实也很简单。与前面不一样的是这次我们可以从左端或者右端取硬币,不确定性比较高。那么只考虑一端还剩多少硬币时不够的,而是考虑剩下i到j的硬币时,先手可以取得的最大价值。可以看到这题其实又是一道典型的区间类DP问题。

定义状态,f[i][j]为还剩i到j的硬币时,先手可以取得的最大价值。

状态转换方程是 f[i][j] = sum[i][j] - min(f[i+1][j],f[i][j-1]).也就是考虑f[i][j]时先手是从左端取还是右端取硬币,从左端取对应 f[i+1][j],从右端取对应f[i][j-1]。

在这两者中f[i][j]中的先手都是后手。所以取其中比较小的值可以获得最大值。其实也可以写成f[i][j] = max(sum[i+1][j]-f[i+1][j] + values[i],sum[i][j-1]-f[i][j-1] + values[j])。但是太繁琐。

这题初始化时,考虑是从两边往中间压缩,所以先初始化区间长度最短的,即f[i][i] = values[i][i]。也可以优化f[i][i+1]= max(values[i],values[i+1])。不先初始化而是留给转换方程求解也可以,但是比较容易爆栈。这题因为采用记忆化搜索的方式,且一次只可以取一个。栈的高度是O(n^2)级别的。时间复杂度为O(n^2).代码如下:

class Solution:
    # @param values: a list of integers
    # @return: a boolean which equals to True if the first player will win
    def firstWillWin(self, values):
        n = len(values)
        if n == 2:
            return True
        sums = [0]
        for i in xrange(n):
            sums.append(sums[-1] + values[i])
        dp = [[-1] * n for i in xrange(n)]
        for i in xrange(n):
            dp[i][i] = values[i]
        for i in xrange(n-1):
            dp[i][i+1] = max(values[i],values[i+1])
        return self.search(dp, 0, n-1, sums, values) * 2 > sums[n-1]
        
    def search(self, dp, start, end, sums, values):
        if dp[start][end] > 0:
            return dp[start][end]
        left =  self.search(dp,start+1, end, sums, values)
        right = self.search(dp, start, end-1, sums, values)
        dp[start][end] = sums[end + 1] - sums[start] - min(left, right)
        return dp[start][end]

 

 

转载于:https://www.cnblogs.com/sherylwang/p/5613506.html

Monocarp is going to make a purchase with cost of exactly m m burles. He has two types of coins, in the following quantities: coins worth 1 1 burle: a 1 a 1 ​ regular coins and infinitely many fancy coins; coins worth k k burles: a k a k ​ regular coins and infinitely many fancy coins. Monocarp wants to make his purchase in such a way that there's no change — the total worth of provided coins is exactly m m. He can use both regular and fancy coins. However, he wants to spend as little fancy coins as possible. What's the smallest total number of fancy coins he can use to make a purchase? Input The first line contains a single integer t t ( 1 ≤ t ≤ 3 ⋅ 10 4 1≤t≤3⋅10 4 ) — the number of testcases. The only line of each testcase contains four integers m , k , a 1 m,k,a 1 ​ and a k a k ​ ( 1 ≤ m ≤ 10 8 1≤m≤10 8 ; 2 ≤ k ≤ 10 8 2≤k≤10 8 ; 0 ≤ a 1 , a k ≤ 10 8 0≤a 1 ​ ,a k ​ ≤10 8 ) — the cost of the purchase, the worth of the second type of coin and the amounts of regular coins of both types, respectively. Output For each testcase, print a single integer — the smallest total number of fancy coins Monocarp can use to make a purchase. Examples Inputcopy Outputcopy 4 11 3 0 0 11 3 20 20 11 3 6 1 100000000 2 0 0 5 0 1 50000000 Note In the first testcase, there are no regular coins of either type. Monocarp can use 2 2 fancy coins worth 1 1 burle and 3 3 fancy coins worth 3 3 (since k = 3 k=3) burles to get 11 11 total burles with 5 5 total fancy coins. In the second testcase, Monocarp has a lot of regular coins of both types. He can use 11 11 regular coins worth 1 1 burle, for example. Notice that Monocarp doesn't have to minimize the total number of used coins. That way he uses 0 0 fancy coins. In the third testcase, Monocarp can use 5 5 regular coins worth 1 1 burle and 1 1 regular coin worth 3 3 burles. That will get him to 8 8 total burles when he needs 11 11. So, 1 1 fancy coin worth 3 3 burles is enough.
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值