CF1102D.Balanced Ternary String
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
给你一个长度恰好为 n 的字符串 s,其中每个字符均为 '0'、'1' 或 '2'。这类字符串称为三元字符串。
你的任务是:通过将字符串中最少数量的字符替换为其他字符,使其变为一个平衡三元字符串(即字符串中 '0' 的个数等于 '1' 的个数,且 '1' 的个数(显然 '0' 的个数也相同)等于 '2' 的个数)。
在所有可能的平衡三元字符串中,你必须得到字典序(字母序)最小的那个。
注意:你既不能从字符串中删除字符,也不能向字符串中添加字符。此外,你只能将原字符串中的字符替换为 '0'、'1' 或 '2'。
题目保证答案一定存在。
输入格式
The first line of the input contains one integer n (3≤n≤3⋅105, 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'.
输入的第一行包含一个整数 n(3≤n≤3⋅105,且 n 能被 3 整除)—— 表示字符串 s 的字符个数。
第二行包含一个长度恰好为 n 的字符串 s,其字符仅由 '0'、'1' 和 '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.
输出一个字符串——即通过最少次数的字符替换,从给定字符串所能得到的字典序(字母序)最小的平衡三进制字符串。
由于 n 能被 3 整除,因此答案一定存在;且显然该答案是唯一的。
输入输出样例
输入#1
3 121
输出#1
021
输入#2
6 000000
输出#2
001122
输入#3
6 211200
输出#3
211200
输入#4
6 120110
输出#4
120120
输入解题思路,AI测评打分。不知道怎么写?