• X Ticket Game 博弈论


    题目

    Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even number). During the evening walk Monocarp and Bicarp found a ticket where some of the digits have been erased. The number of digits that have been erased is even.

    Monocarp and Bicarp have decided to play a game with this ticket. Monocarp hates happy tickets, while Bicarp collects them. A ticket is considered happy if the sum of the first n/2 digits of this ticket is equal to the sum of the last n/2 digits.

    Monocarp and Bicarp take turns (and Monocarp performs the first of them). During each turn, the current player must replace any erased digit with any digit from 0 to 9. The game ends when there are no erased digits in the ticket.

    If the ticket is happy after all erased digits are replaced with decimal digits, then Bicarp wins. Otherwise, Monocarp wins. You have to determine who will win if both players play optimally.

    Input

    The first line contains one even integer n (2≤n≤2⋅105) — the number of digits in the ticket.

    The second line contains a string of n digits and "?" characters — the ticket which Monocarp and Bicarp have found. If the i-th character is "?", then the i-th digit is erased. Note that there may be leading zeroes. The number of "?" characters is even.

    Output

    If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).

    Examples

    Input 1

    4
    0523
    

    Output 1

    Bicarp
    

    Input 2

    2
    ??
    

    Output 2

    Bicarp
    

    Input 3

    8
    ?054??0?
    

    Output 3

    Bicarp
    

    Input 4

    6
    ???00?
    

    Output 4

    Monocarp
    

    Note

    Since there is no question mark in the ticket in the first example, the winner is determined before the game even starts, and it is Bicarp.

    In the second example, Bicarp also wins. After Monocarp chooses an erased digit and replaces it with a new one, Bicap can choose another position with an erased digit and replace it with the same digit, so the ticket is happy.

    题解

    题目大意

    给一串长为n的数列,n为偶数,其中有偶数个“?”,
    M与B在“?”上填数,填满后前一半数字和等于后一半则B获胜,否则M获胜
    M先手,问谁会获胜

    解题思路

    (sum)是数字和,(cnt)是“?”的个数
    B获胜条件:(sum_{left} - sum_{right} = (cnt_{right} - cnt_{left}) * 9 / 2)
    我们可以这样想:
    将左右两边看作两堆石子,(sum)是石子个数,(cnt)是可操作次数,每次可以放0-9任意个数的石子

    M先手,为了使两边不同,他一定会扩大差值,在大的那边填9;
    B后手,为了使两边相同,他一定要缩小差值,在小的那边补9;
    所以两堆石子的可操作次数可以消去

    两堆石子分别消去相同的个数和可操作次数,有下面几种情况:

    1. 个数和可操作次数都消去了,那一定是B赢

    2. 如果剩下的石子和可操作次数都在一边,那M和B无论怎么放,两堆石子都不可能相等,M赢

    3. 一边有石子,一边可操作,我们来仔细分析一下这种情况
      一边又石子s个,另一边可进行操作a次,
      由于(a_1+a_2)是偶数,所以(|a_1-a_2|)也为偶数
      我们可以知道,只有当两个和为9的时候,无论一个取0-9中的哪个数,都能从0-9中找到一个数,使两数和为9
      所以,当(s/(a/2)=9)时B能赢
      将两个操作看成一组,其中(a/2)是能有几组操作

    综上,我们可以得出判断式

    (sum_{left} - sum_{right} = (cnt_{right} - cnt_{left}) * 9 / 2)

    代码

    #include <cstdio>
    using namespace std;
    int n, s1, s2, a1, a2, a, s;
    char c;
    int main() {
        scanf("%d", &n);
        for(int i = 1; i <= n / 2; i++) 
            scanf(" %c", &c), c == '?' ? a1++ : s1 += c - '0';
        for(int i = n / 2 + 1; i <= n; i++) 
            scanf(" %c", &c), c == '?' ? a2++ : s2 += c - '0';
        a = a1 - a2; s = s2 - s1;
        puts(s == a * 9 / 2 ? "Bicarp" : "Monocarp");
        return 0;
    }
    
  • 相关阅读:
    Android Service 服务(一)—— Service
    ostringstream的使用方法
    【Android动画】之Tween动画 (渐变、缩放、位移、旋转)
    #pragma comment
    添加PDF文件对照度的粗浅原理,及方法
    Jenkins(二)
    “简密”App Store处女作开发总结
    Windows下FFmpeg高速入门
    Storm-0.9.2-incubating源代码编译打包
    几种开源分词工具的比較
  • 原文地址:https://www.cnblogs.com/shawk/p/12760034.html
Copyright © 2020-2023  润新知