You are given a string ss consisting of exactly nn 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.

Input

The first line of the input contains one integer nn (3≤n≤3⋅1053≤n≤3⋅105, nn is divisible by 33) — the number of characters in ss.

The second line contains the string ss consisting of exactly nn characters '0', '1' and '2'.

Output

Print one string — the lexicographically (alphabetically) smallest balanced ternary string which can be obtained from the given one with minimum number of replacements.

Because nn is divisible by 33 it is obvious that the answer exists. And it is obvious that there is only one possible answer.

Examples

Input
3
121
Output
021
Input
6
000000
Output
001122
Input
6
211200
Output
211200
Input
6
120110
Outpu1201
题目链接 :https://vjudge.net/problem/CodeForces-1102D
题意:给你一个长度为N个字符串,N是3的倍数,字符串中只包括'0','1','2'这三个字符,
题目让你修改最少数量的字符,使这个字符串的中的这三个字符的数量相等,
即如果N=6,需要含有两个‘1’,两个‘2’,两个‘0’,并且希望你输出满足条件并且字典序最小的那一个。 思路:
贪心构造。
先预处理一下每一个字符出现了多少次,
然后遍历一下字符串,当0字符出现了大于n/3的时候,
把'0'最后一个出现的位置尝试改成‘2’,如果‘2’的数量不小于n/3,那么就改成'1'。之所以这个顺序,就是因为要求字典序最小,这样构造出来的最小。
‘1’,’2’类推该怎么构造。
既然我们每一次可能用到某一个字符第一次出现的位置或者最后一个出现的位置,我们就要开一个双端队列,然后预处理的时候把这个信息就加入到对应的双端中,
注意:这里讲的最后一次出现的,是对于刚预处理完的,如果每一次把他的最后一个出现的给改成别的字符了,那么就要从双端中弹出,
次后的继位。 细节可以看代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
char s[maxn];
char ans[maxn];
int cnt[];
int num[];
deque<int> v[];
int main()
{
gbtb;
cin>>n;
cin>>s;
strcpy(ans,s);
repd(i,,n-)
{
if(s[i]=='')
{
cnt[]++;
v[].pb(i);
}else if(s[i]=='')
{
cnt[]++;
v[].pb(i);
}else
{
v[].pb(i);
cnt[]++;
}
}
int x=n/;
// for(int i=n-1;i>=0;i--)
// {
// if(s[i]=='0')
// {
// if(cnt[0]>x)
// {
// if(cnt[2]<x)
// {
// s[i]='2';
// cnt[2]++;
//
// }else
// {
// s[i]='1';
// cnt[1]++;
// }
// cnt[0]--;
// }
// }
// }
repd(i,,n-)
{
if(s[i]=='')
{
if(cnt[]>x)
{
int index=v[].back();
v[].pop_back();
// *(--v[0].end());
// v[0].erase(--v[0].end());
if(cnt[]<x)
{
ans[index]='';
cnt[]++; }else
{
ans[index]='';
cnt[]++;
}
cnt[]--;
}
}
else if(s[i]=='')
{
if(cnt[]>x)
{
if(cnt[]<x)
{
int index=v[].back();
v[].pop_back();
ans[index]='';
cnt[]++; }else
{
int index=v[].front();
v[].pop_front();
ans[index]='';
cnt[]++;
}
cnt[]--;
}
}else
{
if(cnt[]>x)
{
int index=v[].front();
v[].pop_front();
if(cnt[]<x)
{ ans[index]='';
cnt[]++; }else
{
ans[index]='';
cnt[]++;
}
cnt[]--;
}
}
}
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}


Balanced Ternary String CodeForces - 1102D (贪心+思维)的更多相关文章

  1. Balanced Ternary String(贪心+思维)

    题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...

  2. codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题

    http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...

  3. Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)

    题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\ ...

  4. D - Balanced Ternary String (贪心)

    题目链接:http://codeforces.com/contest/1102/problem/D 题目大意:给你一个字符串,这个字符串是由0,1,2构成的,然后让你替换字符,使得在替换的次数最少的前 ...

  5. Obtain The String CodeForces - 1295C binary_search+思维

    妈耶,,,被B题卡到哭,C题一发就过了... 字符串问题.首先用vector记录每个字符出现的位置,然后对字符串t的每个字符,用二分查找函数查找,注意用upper_bound查找,对于字符i,首先用变 ...

  6. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  7. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  8. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  9. CodeForces - 1009B Minimum Ternary String

    You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...

随机推荐

  1. ABAP CDS 替换对象(Replacement Objects)引起的数据错误

    最近遇到了一个诡异的问题:从CDS视图中取得的数据,和从透明表中取得的数据,会有不同的值.在这里记录下问题的表现和解决方案,以供参考. 系统版本:S/4HANA OP1610 涉及表:MCHB 本文链 ...

  2. SqlBulkCopy批量添加

    /// <summary> /// 添加数据 /// 注:DataTable列名必须和数据库列名一致 /// </summary> /// <returns>< ...

  3. matlab数字图像处理 入门基础

    本代码基于张铮主编的<数字图像处理与机器视觉>一书. 源图片:lena A = imread ('lena.jpg'); %读入图像lena.jpg,赋给变量A %imwrite(A,'l ...

  4. win7(64位)Sql server 用T-sql读取本地数据文件dbf的数据文件

    原文地址:https://www.cnblogs.com/cl1006/p/9924066.html 第一步启用Ad Hoc Distributed Queries  在SQLserver执行以下的语 ...

  5. python opencv show图片,debug技巧

    debug的时候可以直接把图片画出来debug. imshow函数就是python opencv的展示图片的函数,第一个是你要起的图片名,第二个是图片本身.waitKey函数是用来展示图片多久的,默认 ...

  6. squid调整

    Squid采用新方案部署的调整步骤一,隔离二,修改三,验证四,波及==============================[1] 把被引用到的待修改对像实例,从前端应用负载nginx的配置中摘出 ...

  7. Qt5中的lambda表达式和使用lambda来写connect

    c11新特性中加入了lambda表达式,所以Qt 也支持 需在.pro文件中加入 CONFIG += c++11 例子: QString program = "C:/Windows/Syst ...

  8. TP4212 FM9836C

    今天拆了两个充电宝,发现充电宝的电路是由一个集成芯片控制的.芯片型号:TP4212, FM9836C,

  9. Nginx-PHP优化设置 + lnmp调优的关键影响因素 + php-fpm + nginx返回码 + tcp调优 + 最大文件描述符

    https://www.cnblogs.com/zengkefu/p/5602473.html最大文件描述符 https://blog.csdn.net/fanren224/article/detai ...

  10. Java中没有引用传递只有值传递(在函数中)

    ◆传参的问题 引用类型(在函数调用中)的传参问题,是一个相当扯的问题.有些书上说是传值,有些书上说是传引用.搞得Java程序员都快成神经分裂了.所以,我们最后来谈一下“引用类型参数传递”的问题. 如下 ...