#394 Coins in a Line

本文探讨了一种博弈类DP问题,即两个玩家轮流从一排硬币中取走一枚或两枚,直到没有硬币为止,最后取走硬币的人获胜。通过分析不同情况下的胜利条件,提供了一个O(n)时间复杂度和O(1)空间复杂度的解决方案。

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

题目描述:

There are n coins in a line. Two players take turns to take one or two coins from right side until there are no more coins left. The player who take the last coin wins.

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

Example

n = 1, return true.

n = 2, return true.

n = 3, return false.

n = 4, return true.

n = 5, return true.

Challenge 

O(n) time and O(1) memory

题目思路:

这题属于博弈类dp题,win的精髓在于不能把宝压在对手上,也就是说,无论对手采取什么选择,都会win。用dp[i]表示有i个coin时,自己会不会win。那么想到,走到i有两种可能:自己拿了2个coins或者拿了1个coin走到的i。那我们要保证自己拿了2个coin必win,或者自己拿了1个coin必win(因为自己拿多少是可以选的)。也就是说,自己拿2个coin时,必须保证dp[i-4](对手拿2), dp[i-3](对手拿1)都是true的;自己拿一个coin时,必须保证dp[i-2](对手拿1), dp[i-3](对手拿2)都是true的。

搞清了逻辑之后,code倒是 

Mycode(AC = 12ms):

class Solution {
public:
    /**
     * @param n: an integer
     * @return: a boolean which equals to true if the first player will win
     */
     bool firstWillWin(int n) {
        // write your code here
        if (n == 0 || n == 3) {
            return false;
        }
        if (n == 1 || n == 2) {
            return true;
        }
        else {
            vector<bool> dp(n + 1, false);
            dp[1] = true;
            dp[2] = true;
            dp[3] = false;
            
            // ensure whether player 2 pick
            // 1 or 2 coins, player 1 will win
            for (int i = 4; i <= n; i++) {
                dp[i] = (dp[i - 3] && dp[i - 2]) || // player1 pick 1 coin
                        (dp[i - 4] && dp[i - 3]); // player1 pick 2 coins
            }
            
            return dp[n];
        }
    }
};


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&#39;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&#39;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&#39;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、付费专栏及课程。

余额充值