Codeforces1215D 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 n2 digits of this ticket is equal to the sum of the last n2 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
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),Monocarp先来,若最后序列前后两端数位和不等,Monocarp赢,否则Bicarp赢,两方都选最优策略。

问题分析
先分别获取两部分非擦除数的和sum1、sum2,以及两半数中’?‘的个数num1、num2。并记差值为x,那么问题统一为:前一半数的和为x+num1个’?’(值可不一,下同),后一半数的和为num2个’?’。
①sum1==sum2:
当num1不等于num2时,先手一定赢。反之则后者赢。
②假如sum1>sum2
(1)num1>=num2时,先手可以在一边一直放9,后手弥补不了差距。先手必胜。
(2)num1<num2时,前2n次同上,剩下的次数中,先手放sum1时,后手只能是放sum2来使得sum1+sum2=9;所以当9|sum1-sum2时后手胜。不然的话后手是必输的。

#include<bits/stdc++.h>
#define ll long long
const ll mod=1e9+7;
const int maxn=2e5+100;
using namespace std;
int n,m,k,t;
char c;
int num1,num2;
int sum1,sum2;
int main(){
    cin>>n;
    for(int i=1;i<=n/2;i++){
        cin>>c;
        if(c=='?')num1++;
        else sum1+=(c-'0');
    }
    for(int i=n/2+1;i<=n;i++){
        cin>>c;
        if(c=='?')num2++;
        else sum2+=(c-'0');
    }
    sum1+=num1/2*9;
    sum2+=num2/2*9;
    if(sum1==sum2){printf("Bicarp\n");}
    else{printf("Monocarp\n");}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CodeForces - 616D是一个关于找到一个序列中最长的第k好子段的起始位置和结束位置的问题。给定一个长度为n的序列和一个整数k,需要找到一个子段,该子段中不超过k个不同的数字。题目要求输出这个序列最长的第k好子段的起始位置和终止位置。 解决这个问题的方法有两种。第一种方法是使用尺取算法,通过维护一个滑动窗口来记录\[l,r\]中不同数的个数。每次如果这个数小于k,就将r向右移动一位;如果已经大于k,则将l向右移动一位,直到个数不大于k。每次更新完r之后,判断r-l+1是否比已有答案更优来更新答案。这种方法的时间复杂度为O(n)。 第二种方法是使用枚举r和双指针的方法。通过维护一个最小的l,满足\[l,r\]最多只有k种数。使用一个map来判断数的种类。遍历序列,如果当前数字在map中不存在,则将种类数sum加一;如果sum大于k,则将l向右移动一位,直到sum不大于k。每次更新完r之后,判断i-l+1是否大于等于y-x+1来更新答案。这种方法的时间复杂度为O(n)。 以上是两种解决CodeForces - 616D问题的方法。具体的代码实现可以参考引用\[1\]和引用\[2\]中的代码。 #### 引用[.reference_title] - *1* [CodeForces 616 D. Longest k-Good Segment(尺取)](https://blog.csdn.net/V5ZSQ/article/details/50750827)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces616 D. Longest k-Good Segment(双指针+map)](https://blog.csdn.net/weixin_44178736/article/details/114328999)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值