D. 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).

题意:
一个数字序列由n(n为整数且小于2e5)位组成,其中有整数个数位被污染,现在A和B可以轮流给被污染数位赋值(0-9),A先来,若最后序列前后两端数位和不等,A赢,否则B赢,两方都选最优策略。

Examples

input

4
0523

output

Bicarp

input

2
??

output

Bicarp

input

8
?054??0?

output

Bicarp

input

6
???00?

output

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.

分析:
A破坏,B还原。
将字符串从中间分开,A每增加一个数,则左或右两边的和增加0~9,而B则需要补差。
先手A要想赢肯定会选 0 或 9 两个极端数,而后手B必须把这个差值补回来,并且他们每人轮流一次,
所以到最后两人补充的数相差 9 的倍数,所以没有补充前左右的差一定是 9 的倍数。
所以可以根据左右的空来来判断。

CODE:

#include <iostream>
#define maxn 200005
using namespace std;
char a[maxn];
int main()
{
    int n,i;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
        {
            cin>>a[i];
        }
        int nl,nr,lp,rp;
        nl=nr=lp=rp=0;
        for(i=1;i<=n/2;i++)
        {
            if(a[i]!='?')
                nl+=a[i]-'0';
            else
                lp++;
        }
        for(i=n/2+1;i<=n;i++)
        {
            if(a[i]!='?')
                nr+=a[i]-'0';
            else
                rp++;
        }
        if(nl==nr)
        {
            if(lp==rp)
                cout<<"Bicarp"<<endl;
            else
                cout<<"Monocarp"<<endl;
        }
        int re,num;
        if(nl<nr)
        {
            re=nr-nl;
            num=(lp-rp)/2;
            if(num*9==re)
                cout<<"Bicarp"<<endl;
            else
                cout<<"Monocarp"<<endl;
        }
        if(nl>nr)
        {
            re=nl-nr;
            num=(rp-lp)/2;
            if(num*9==re)
                cout<<"Bicarp"<<endl;
            else
                cout<<"Monocarp"<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44350170/article/details/100942464