Ticket Game CodeForces - 1215D(博弈)

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
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.
挺好的一道博弈的题目,虽然我不会做。。
题意:一个数字序列由n(n为整数且小于2e5)位组成,其中有整数个数位被污染,现在A和B可以轮流给被污染数位赋值(0-9),A先来,若最后序列前后两端数位和不等,A赢,否则B赢,两方都选最优策略。
思路:我看网上有什么很高深的证明啥的,但是俺也不会。。
如果没有问号的话,我们就判断左右两半的和是否相等。
如果有问号,而且某一边的和大于另一边,而且问号数量也大于另一边。那么M就可以往这一边放大的,M就能赢了。
接下来的情况就是在剩下的一方里(假设此时还剩n个污染部位),还是A先手,两方每次选择一个0~9之间的数,如果最后选择数的和等于初始左右差值,B赢,否则A赢

这就转换为一个经典博弈问题了,只要n/2*9==差值,B赢,否则A赢。

想想为什么,因为后手的B只可以控制他和A选择的数的总和是9,故如果轮数乘9和差值相等就赢,否则就输。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

string s;
int n;

int main()
{
	cin>>n>>s;
	int sum1=0,sum2=0,lsum=0,rsum=0;
	int m=n%2?n/2+1:n/2;
	for(int i=0;i<n/2;i++)
	{
		if(s[i]=='?') sum1++;
		else lsum+=s[i]-'0';
	}
	for(int i=m;i<n;i++)
	{
		if(s[i]=='?') sum2++;
		else rsum+=s[i]-'0';
	}
	if(sum1==0&&sum2==0)
	{
		if(lsum==rsum) puts("Bicarp");
		else puts("Monocarp");
		return 0;
	}
	if((sum1>sum2&&lsum>rsum)||(sum2>sum1&&rsum>lsum)) puts("Monocarp");
	else
	{
		int cnt=(9*abs(sum1-sum2)/2);
		if(cnt!=abs(lsum-rsum)) puts("Monocarp");
		else puts("Bicarp");
	}
}

多思考一下吧,博弈真的感觉有心无力。。
努力加油a啊,(o)/~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

starlet_kiss

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

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

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

打赏作者

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

抵扣说明:

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

余额充值