CF1725H.Hot Black Hot White

普及+/提高

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

One day, you are accepted as being Dr. Chanek's assistant. The first task given by Dr. Chanek to you is to take care and store his magical stones.

Dr. Chanek has NN magical stones with NN being an even number. Those magical stones are numbered from 11 to NN. Magical stone ii has a strength of AiA_i. A magical stone can be painted with two colours, namely the colour black or the colour white. You are tasked to paint the magical stones with the colour black or white and store the magical stones into a magic box with a magic coefficient ZZ (0≤Z≤20 \leq Z \leq 2). The painting of the magical stones must be done in a way such that there are N2\frac{N}{2} black magical stones and N2\frac{N}{2} white magical stones.

Define concat(x,y)\text{concat}(x, y) for two integers xx and yy as the result of concatenating the digits of xx to the left of yy in their decimal representation without changing the order. As an example, concat(10,24)\text{concat}(10, 24) will result in 10241024.

For a magic box with a magic coefficient ZZ, magical stone ii will react with magical stone jj if the colours of both stones are different and concat(Ai,Aj)×concat(Aj,Ai)+Ai×Aj≡Zmod  3\text{concat}(A_i, A_j) \times \text{concat}(A_j, A_i) + A_i \times A_j \equiv Z \mod 3. A magical stone that is reacting will be very hot and dangerous. Because of that, you must colour the magical stones and determine the magic coefficient ZZ of the magic box in a way such that there is no magical stone that reacts, or report if it is impossible.

有一天,你被接纳为陈博士(Dr. Chanek)的助手。陈博士交给你的第一项任务是照看并储存他的魔法石。

陈博士有 NN 颗魔法石,其中 NN 为偶数。这些魔法石编号为 11 到 NN。第 ii 颗魔法石的强度为 AiA_i。每颗魔法石可被涂成两种颜色之一:黑色或白色。你的任务是将这些魔法石涂成黑色或白色,并将其存入一个具有魔法系数 ZZ(0≤Z≤20 \leq Z \leq 2)的魔法盒中。涂色必须满足:恰好有 N2\frac{N}{2} 颗魔法石为黑色,且恰好有 N2\frac{N}{2} 颗魔法石为白色。

对两个整数 xx 和 yy,定义 concat(x,y)\text{concat}(x, y) 为:将 xx 的十进制表示的各位数字按原顺序拼接在 yy 的十进制表示的左侧所得的整数。例如,concat(10,24)\text{concat}(10, 24) 的结果为 10241024。

对于一个魔法系数为 ZZ 的魔法盒,若魔法石 ii 与魔法石 jj 颜色不同,且满足

concat(Ai,Aj)×concat(Aj,Ai)+Ai×Aj≡Z(mod3),\text{concat}(A_i, A_j) \times \text{concat}(A_j, A_i) + A_i \times A_j \equiv Z \pmod{3},

则魔法石 ii 与魔法石 jj 将发生反应。发生反应的魔法石会变得极其炽热且危险。因此,你必须对魔法石进行涂色,并确定魔法盒的魔法系数 ZZ,使得没有任何一对魔法石发生反应;若不可能实现,则需报告该情况。

输入格式

The first line contains a single even integer NN (2≤N≤1052 \le N \le 10^5) — the number of magical stones Dr. Chanek has.

The second line contains NN integer A1,A2,…,ANA_1, A_2, \ldots, A_N (1≤Ai≤1091 \leq A_i \leq 10^9) — the strengths of all magical stones.

第一行包含一个偶数整数 NN(2≤N≤1052 \le N \le 10^5)—— 表示陈博士拥有的魔法石数量。

第二行包含 NN 个整数 A1,A2,…,ANA_1, A_2, \ldots, A_N(1≤Ai≤1091 \leq A_i \leq 10^9)—— 表示所有魔法石的强度。

输出格式

If it is not possible to satisfy the condition of the problem, output −1-1.

Otherwise, output two lines. The first line contains an integer ZZ denoting the magic coefficient of the magic box. The second line contains a string SS of length NN. SiS_i is 00 if magical stone ii is coloured black or 11 if magical stone ii is coloured white. If there are more than one possibilities of colouring and choosing the magic coefficient ZZ, output any of them.

如果无法满足题目的条件,则输出 −1-1。

否则,输出两行。第一行包含一个整数 ZZ,表示魔法盒的魔法系数。第二行包含一个长度为 NN 的字符串 SS;其中 SiS_i 为 00 表示第 ii 颗魔法石被染成黑色,为 11 表示第 ii 颗魔法石被染成白色。若存在多种染色方案及对应的魔法系数 ZZ,输出任意一种即可。

输入输出样例

  • 输入#1

    4
    4 10 9 14

    输出#1

    0
    1001

说明/提示

By giving the above colouring, it can be seen that:

  • i=1,j=2⟶concat(4,10)×concat(10,4)+10×4=410×104+40=42680≡2mod  3i=1, j=2 \longrightarrow \text{concat}(4, 10) \times \text{concat}(10, 4) + 10 \times 4 = 410 \times 104 + 40 = 42680 \equiv 2 \mod 3
  • i=1,j=3⟶concat(4,9)×concat(9,4)+4×9=49×94+36=4642≡1mod  3i=1, j=3 \longrightarrow \text{concat}(4, 9) \times \text{concat}(9, 4) + 4 \times 9 = 49 \times 94 + 36 = 4642 \equiv 1 \mod 3
  • i=4,j=2⟶concat(14,10)×concat(10,14)+10×14=1410×1014+140=1429880≡2mod  3i=4, j=2 \longrightarrow \text{concat}(14, 10) \times \text{concat}(10, 14) + 10 \times 14 = 1410 \times 1014 + 140 = 1429880 \equiv 2 \mod 3
  • i=4,j=3⟶concat(14,9)×concat(9,14)+14×9=149×914+126=136312≡1mod  3i=4, j=3 \longrightarrow \text{concat}(14, 9) \times \text{concat}(9, 14) + 14 \times 9 = 149 \times 914 + 126 = 136312 \equiv 1 \mod 3

Because of that, by choosing Z=0Z = 0, it can be guaranteed that there is no magical stone that reacts.

通过上述染色方案可以发现:

  • i=1,j=2⟶concat(4,10)×concat(10,4)+10×4=410×104+40=42680≡2mod  3i=1, j=2 \longrightarrow \text{concat}(4, 10) \times \text{concat}(10, 4) + 10 \times 4 = 410 \times 104 + 40 = 42680 \equiv 2 \mod 3
  • i=1,j=3⟶concat(4,9)×concat(9,4)+4×9=49×94+36=4642≡1mod  3i=1, j=3 \longrightarrow \text{concat}(4, 9) \times \text{concat}(9, 4) + 4 \times 9 = 49 \times 94 + 36 = 4642 \equiv 1 \mod 3
  • i=4,j=2⟶concat(14,10)×concat(10,14)+10×14=1410×1014+140=1429880≡2mod  3i=4, j=2 \longrightarrow \text{concat}(14, 10) \times \text{concat}(10, 14) + 10 \times 14 = 1410 \times 1014 + 140 = 1429880 \equiv 2 \mod 3
  • i=4,j=3⟶concat(14,9)×concat(9,14)+14×9=149×914+126=136312≡1mod  3i=4, j=3 \longrightarrow \text{concat}(14, 9) \times \text{concat}(9, 14) + 14 \times 9 = 149 \times 914 + 126 = 136312 \equiv 1 \mod 3

因此,选择 Z=0Z = 0 即可保证不存在发生反应的魔法石。

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

首页