305871: CF1102D. Balanced Ternary String

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Balanced Ternary String

题意翻译

给出一个长为n的只由'1','2','0'组成的字符串,要求改动最少的位置,使'1','2','0'的个数相同(保证n能被3整除),并使改动后的字符串字典序最小。 n不大于$3*10^5$ 输出改动后的字符串(答案唯一)

题目描述

You are given a string $ s $ consisting of exactly $ n $ characters, and each character is either '0', '1' or '2'. Such strings are called ternary strings. Your task is to replace minimum number of characters in this string with other characters to obtain a balanced ternary string (balanced ternary string is a ternary string such that the number of characters '0' in this string is equal to the number of characters '1', and the number of characters '1' (and '0' obviously) is equal to the number of characters '2'). Among all possible balanced ternary strings you have to obtain the lexicographically (alphabetically) smallest. Note that you can neither remove characters from the string nor add characters to the string. Also note that you can replace the given characters only with characters '0', '1' and '2'. It is guaranteed that the answer exists.

输入输出格式

输入格式


The first line of the input contains one integer $ n $ ( $ 3 \le n \le 3 \cdot 10^5 $ , $ n $ is divisible by $ 3 $ ) — the number of characters in $ s $ . The second line contains the string $ s $ consisting of exactly $ n $ characters '0', '1' and '2'.

输出格式


Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements. Because $ n $ is divisible by $ 3 $ it is obvious that the answer exists. And it is obvious that there is only one possible answer.

输入输出样例

输入样例 #1

3
121

输出样例 #1

021

输入样例 #2

6
000000

输出样例 #2

001122

输入样例 #3

6
211200

输出样例 #3

211200

输入样例 #4

6
120110

输出样例 #4

120120

Input

题意翻译

给出一个长为n的只由'1','2','0'组成的字符串,要求改动最少的位置,使'1','2','0'的个数相同(保证n能被3整除),并使改动后的字符串字典序最小。 n不大于$3*10^5$ 输出改动后的字符串(答案唯一)

加入题单

上一题 下一题 算法标签: