CF1215D Ticket Game 博弈

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_40758751/article/details/101098929

D. Ticket Game

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Monocarp and Bicarp live in Berland, where every bus ticket consists of nn digits (nn 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 n2n2 digits of this ticket is equal to the sum of the last n2n2 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 00 to 99. 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 nn (2≤n≤2⋅105)(2≤n≤2⋅105) — the number of digits in the ticket.

The second line contains a string of nn digits and "?" characters — the ticket which Monocarp and Bicarp have found. If the ii-th character is "?", then the ii-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

Copy

4
0523

output

Copy

Bicarp

input

Copy

2
??

output

Copy

Bicarp

input

Copy

8
?054??0?

output

Copy

Bicarp

input

Copy

6
???00?

output

Copy

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.

思路:

首先两边可以配对的'?'是没有用的,因为先手采取最优策略是扩大两边差的绝对值,而另一边有'?'的话后手可以放和先手相同的牌来抵消这个影响,所以先算出两边'?'的差值。

此时,有一边的数字已经固定了,只剩下一遍可以调整,此时先手的策略有两种,要么尽量拿较大的值使可以调整的这边的值大于已经固定的那边的值(此时后手怎么做都是输)。要么尽量拿较小的值使可以调整的这边的值永远不可能赶上已经固定的那边的值(此时后手怎么做都是输)。

在两种先手的策略下,后手只能被动的尽可能抵消影响。所以每一轮(放两次数字)可以调整的这一边的值总会增加9,只要判断其能否大于或小于另一边的值即可。

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
typedef long long ll;
const int maxn = 1e6+50;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9+7;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
struct Half{
    int ques = 0;
    int score = 0;
};
int main()
{
    int n;
    Half l, r;
    char chr;
    cin >> n;
    rep(i, 1, n){
        cin >> chr;
        if(i <= n/2){
            if(chr == '?'){
                l.ques++;
            }else{
                l.score += chr - '0';
            }
        }else{
            if(chr == '?'){
                r.ques++;
            }else{
                r.score += chr - '0';
            }
        }
    }
    int pir = min(l.ques, r.ques);
    l.ques -= pir;
    r.ques -= pir;
    if(l.ques){
        if(l.score + l.ques/2*9 > r.score || l.score + l.ques/2*9 < r.score)
            puts("Monocarp");
        else
            puts("Bicarp");
    }
    if(r.ques){
        if(r.score + r.ques/2*9 > l.score || r.score + r.ques/2*9 < l.score)
            puts("Monocarp");
        else
            puts("Bicarp");
    }
    if(l.ques == 0 && r.ques == 0){
        if(l.score == r.score)
            puts("Bicarp");
        else
            puts("Monocarp");
    }
    return 0;
}
/*

*/

猜你喜欢

转载自blog.csdn.net/qq_40758751/article/details/101098929
今日推荐