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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

为君倾此杯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值