#395 Coins in a Line II

本文探讨了一种博弈论游戏算法,玩家轮流从一排不同价值的硬币中取走一枚或两枚,目标是获得最高总价值。文章通过示例说明了如何决定首位玩家是否能赢,并提供了一个实现该算法的C++代码示例。

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

题目描述:

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

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

Example

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

Given A = [1,2,4], return false.

题目思路:

这题的思路和#394类似:自己有两种选择:拿1或者拿2.但是对手的选择(1或者2)都是必须让自己拿的最少的。自己拿1的时候:对手的选择是dp[i - 3]和dp[i-2]里取最小;自己拿2的时候,对手在dp[i - 4]和 dp[i - 3]取最小。但是自己可以在拿1和拿2中选个最大值。

这题比较坑爹的是要注意初始化啊初始化。必须要从array的尾巴开始初始化和计算,因为从头看是有uncertainty的,我们是不知道到底应该怎么取的。

Mycode(AC = 11ms):

class Solution {
public:
    /**
     * @param values: a vector of integers
     * @return: a boolean which equals to true if the first player will win
     */
    bool firstWillWin(vector<int> &values) {
        // write your code here
        if (values.size() == 0) {
            return false;
        }
        else if (values.size() <= 2) {
            return true;
        }
        else if (values.size() == 3) {
            return values[0] + values[1] > values[2];
        }
        else {
            int n = values.size();
            vector<int> dp(n + 1, 0);
            
            // must initial from tail, because
            // how to pick from head is uncertain
            dp[1] = values[n - 1];
            dp[2] = values[n - 1] + values[n - 2];
            dp[3] = values[n - 3] + values[n - 2];
            
            int total = values[0] + values[1] + values[2];
            
            for (int i = 4; i <= values.size(); i++) {
                total += values[i - 1];
                dp[i] = max(min(dp[i - 4], dp[i - 3]) + values[n - i + 1] + values[n - i], // if player1 pick 2 coins
                            min(dp[i - 3], dp[i - 2]) + values[n - i]); // if player 1 pick 1 coin
            }
            
            return dp[n] > total - 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、付费专栏及课程。

余额充值