AT_arc227_a.Fermat Point of Binary Strings

普及-

通过率:0%

时间限制:2.00s

内存限制:1024MB

AC君温馨提醒

该题目为【atcoder】题库的题目,您提交的代码将被提交至atcoder进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

A string of length 2N2N that contains NN occurrences of each of 0 and 1 is called a good string.

For good strings SS and TT, let dist⁡(S,T)\operatorname{dist}(S, T) denote the minimum number of swaps required to make SS equal to TT by swapping two adjacent characters of SS zero or more times.

You are given good strings AA, BB, and CC. Among all good strings XX, find one that minimizes the value of

\[ \operatorname{dist}(A, X)+\operatorname{dist}(B, X)+\operatorname{dist}(C, X), \]

and output it together with the minimum value.

长度为 2N2N 且恰好包含 NN 个字符 0 和 NN 个字符 1 的字符串称为好字符串。

对于两个好字符串 SS 和 TT,记 dist⁡(S,T)\operatorname{dist}(S, T) 为:通过对 SS 中相邻字符进行零次或多次交换,使其变为 TT 所需的最少交换次数。

给定三个好字符串 AA、BB 和 CC。在所有好字符串 XX 中,找出一个使下式取最小值的 XX:

[
\operatorname{dist}(A, X)+\operatorname{dist}(B, X)+\operatorname{dist}(C, X),
]

并输出该 XX 及对应的最小值。

输入格式

The input is given from Standard Input in the following format:

NN
AA
BB
CC

输入从标准输入中按以下格式给出:

NN
AA
BB
CC

输出格式

Let KK be the minimum value to be found, and let XX be a good string that achieves it. Output KK and XX in the following format:

KK
XX

If multiple outputs satisfy the conditions, you may output any of them.

设 KK 为待求的最小值,XX 为达到该最小值的一个好字符串。请按以下格式输出 KK 和 XX:

KK
XX

若存在多个满足条件的输出,可任选其一输出。

输入输出样例

  • 输入#1

    2
    1100
    1010
    0011

    输出#1

    4
    1010
  • 输入#2

    3
    101010
    101010
    101010

    输出#2

    0
    101010

说明/提示

Sample 1 Explanation:
If we choose 1010 as the good string XX, then dist⁡(A,X)\operatorname{dist}(A, X), dist⁡(B,X)\operatorname{dist}(B, X), and dist⁡(C,X)\operatorname{dist}(C, X) are 11, 00, 33, respectively, and their sum is 44. The sum of the distances cannot be made smaller than 44.

Sample 2 Explanation:
Since the three input strings are all equal, choosing the same string as XX makes the sum of the distances 00.

Constraints

  • 1≤N≤2×1051 \le N \le 2 \times 10^5
  • Each of AA, BB, and CC is a good string of length 2N2N.
  • NN is an integer.

样例 1 解释:
若选择 1010 作为好字符串 XX,则 dist⁡(A,X)\operatorname{dist}(A, X)、dist⁡(B,X)\operatorname{dist}(B, X) 和 dist⁡(C,X)\operatorname{dist}(C, X) 分别为 11、00、33,其和为 44。该距离之和无法小于 44。

样例 2 解释:
由于三个输入字符串完全相同,选择该字符串作为 XX 可使距离之和为 00。

约束条件

  • 1≤N≤2×1051 \le N \le 2 \times 10^5
  • AA、BB 和 CC 均为长度为 2N2N 的好字符串。
  • NN 为整数。

输入解题思路,AI测评打分。不知道怎么写?

首页