Codeforces 1215D 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.

一张票有n位数,如果这张票的前一半数字的和等于后一半数字的和(n一定是偶数),就称这张票为快乐票。有些数被擦除了,标记为’?’(’?‘的个数也是偶数),现在Monocarp 和 Bicarp 进行一个游戏,两人轮流将’?'变换成0到9的任意一个数,Monocarp先手,如果最后票为快乐票则Bicarp赢,否则Monocarp赢。

输入格式

The first line contains one even integer \(n (2≤n≤2⋅10^5)\) — 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.

先输入数字长度

然后输入这个数字,可能包括问号

输出格式

If Monocarp wins, print "Monocarp" (without quotes). Otherwise print "Bicarp" (without quotes).

谁赢输出谁的名字

样例

输入1

4
0523

输出1

Bicarp

输入2

2
??

输出2

Bicarp

输入3

8
?054??0?

输出3

Bicarp

输入4

6
???00?

输出4

Monocarp

题解

三种情况

  1. 如果最开始左半部分数字之和等于右半部分数字之和,左右问号数量相等Bicarp赢;数量不等Monocarp赢.

应该很好理解,Monocarp先手,无论Monocarp填什么,Bicarp的最优策略都是在另一边填一样的,这样可以一直保证左右数字之和相等,因为Bicarp最后一个填,所以Bicarp赢.

当然,如果数量不等,Bicarp自然赢不了.


当两边数字和不等,且Monocarp可以在数字和大的部分填数字时,他一定会填9以拉开差距,那么Bicarp如果能在另半部分填,一定也会填9.

这就是两部分的问号不断抵消的情况,抵消到只有一边有问号为止,这时候就可以继续思考了,下面所有情况都是假定只有一边有问号

  1. 如果左半部分数字之和大于右半部分数字之和

若只有左边有问号,无论Bicarp如何决策也无法使左半部分减少,右半部分又无法增加,那么Monocarp赢

若只有右边有问号,那么Monocarp一定会填0拉开差距,而Bicarp一定会填9,那么看一下Bicarp能填的数量即可,若能填\(n\)个问号,\(n \times 9\)与左右之差相等,Bicarp赢,反之Monocarp赢.

注意写代码时,"只有左边有问号"的情况可以和"只有右边有问号"的情况合并,因为这时候看作右边的问号数量为负数即可,为负数肯定无法与正数的数量差相等,Monocarp赢一定会赢.

  1. 如果左半部分数字之和小于右半部分数字之和

可以看作为第2种情况的镜像,同理可以解决

代码

#include <bits/stdc++.h>
#define t(i) int(((double)(i) / (n)) + 0.5)
using namespace std;
char s[200005];
int main() {
int n, mark[2] = {0, 0}, sum[2] = {0, 0};
scanf("%d%s", &n, s);
for (int i = 0; i < n; i++)
if (s[i] == '?') mark[t(i)]++;
else sum[t(i)] += s[i] - '0';
puts((mark[0] == mark[1] && sum[0] == sum[1] ||
sum[0] > sum[1] && sum[0] - sum[1] == (mark[1] - mark[0]) / 2 * 9 ||
sum[1] > sum[0] && sum[1] - sum[0] == (mark[0] - mark[1]) / 2 * 9) ? "Bicarp" : "Monocarp");
return 0;
}

Codeforces 1215D Ticket Game 题解的更多相关文章

  1. Codeforces 1215D. Ticket Game

    传送门 博弈,发现情况有点多,分析一下把有用的状态提取出来 显然各个位置的数字是没用的,我们只要知道两边的数字和分别是多少 并且状态显然和左右两边的 "?" 数量有关 因为最终我们 ...

  2. Codeforces Round #543 Div1题解(并不全)

    Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...

  3. Codeforces Round #545 Div1 题解

    Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...

  4. Codeforces Round #539 Div1 题解

    Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...

  5. [Codeforces Round #461 (Div2)] 题解

    [比赛链接] http://codeforces.com/contest/922 [题解] Problem A. Cloning Toys          [算法] 当y = 0 ,   不可以 当 ...

  6. Codeforces 7E - Defining Macros 题解

    目录 Codeforces 7E - Defining Macros 题解 前言 做法 程序 结尾 Codeforces 7E - Defining Macros 题解 前言 开始使用博客园了,很想写 ...

  7. Educational Codeforces Round 64 部分题解

    Educational Codeforces Round 64 部分题解 不更了不更了 CF1156D 0-1-Tree 有一棵树,边权都是0或1.定义点对\(x,y(x\neq y)\)合法当且仅当 ...

  8. Educational Codeforces Round 64部分题解

    Educational Codeforces Round 64部分题解 A 题目大意:给定三角形(高等于低的等腰),正方形,圆,在满足其高,边长,半径最大(保证在上一个图形的内部)的前提下. 判断交点 ...

  9. Ticket Game CodeForces - 1215D 博弈题

    题目描述 Monocarp and Bicarp live in Berland, where every bus ticket consists of n digits (n is an even ...

随机推荐

  1. cuda基础

    一:cuda编程模型 1:主机与设备 主机---CPU 设备/处理器---GPU CUDA编程模型如下: GPU多层存储空间结构如图: 2:Kernel函数的定义与调用 A:运行在GPU上,必须通过_ ...

  2. 详解 Seata Golang 客户端 AT 模式及其使用

    源码seata-golang 概述   我们知道 Seata Java Client 的 AT 模式,通过代理数据源,实现了对业务代码无侵入的分布式事务协调机制,将与 Transaction Coor ...

  3. 无监督LDA、PCA、k-means三种方法之间的的联系及推导

       \(LDA\)是一种比较常见的有监督分类方法,常用于降维和分类任务中:而\(PCA\)是一种无监督降维技术:\(k\)-means则是一种在聚类任务中应用非常广泛的数据预处理方法.    本文的 ...

  4. 不需重新编译php,安装postgresql扩展(pgsql和pdo_pgsql)

    为了不重新编译php,使用phpize工具进行追加. 1.下载phpX安装包 访问php官方下载页,找到自己对应的php版本:https://secure.php.net/downloads.php ...

  5. [TopCoder]Seatfriends

    题目   点这里看题目. 分析   可以想到用 DP 解决.   由于把空位放到状态里面太麻烦了,因此我们单独将 " 组 " 提出来进行 DP .   \(f(i,j)\):前\( ...

  6. flex弹性模型

    flex模型是w3c最新提出的一种盒子模型,很好的解决了普通模型的一些弊端. 一.比较两种盒子模型: demo: 给div添加边框,观察他们的区别 <body> <div class ...

  7. TensorFlow从0到1之TensorFlow损失函数(12)

    正如前面所讨论的,在回归中定义了损失函数或目标函数,其目的是找到使损失最小化的系数.本节将介绍如何在 TensorFlow 中定义损失函数,并根据问题选择合适的损失函数. 声明一个损失函数需要将系数定 ...

  8. 【JMeter_01】JMeter介绍与环境搭建

    JMeter介绍 Apache JMeter™应用开源软件,100%纯Java应用程序,设计之初是用于负载功能测试和性能测试.但因它在实现对各种接口的调用方面比较成熟,因此,常被用做接口功能测试. J ...

  9. 效率思维模式与Zombie Scrum

    Scrum是由Ken Schwaber和Jeff Sutherland在20世纪90年代提出的概念,并在1995年首次正式确定.起初Scrum是为了解决产品和软件开发固有的复杂性,然而现在Scrum被 ...

  10. vue-drag-resize 可拖拽可缩放的标签,如何管理多个拖拽元素之间的zIndex?操作上需要保持当前激活的组件是最上层,但是在总体上,又要确保其图层管理的顺序。

    麻烦总是不断出现,还好办法总比困难多, 1.公司开发一款可视化编辑html网页的多媒体编辑平台,牵扯到标签元素的拖拽,缩放,我找了找方法发现原生技术实现起来代码太多,麻烦,还好找到了一个vue组件,可 ...