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. 用js实现随机选取10–100之间的10个数字,存入一个数组,并排序

    var iArray = []; function getRandom(istart, iend) { var iChoice = iend - istart + 1; //加1是为了取到100 va ...

  2. work flow 工作流程

    常用git 命令 v-1 # 切换分支 git checkout site-v1.7.8 # 提交代码前,先拉取分支 git pull # 拉取失败说明有冲突,解决冲突... # 保存在stash之中 ...

  3. 网页中的meta标签的作用

    偶尔看到一篇博客详细介绍了meta的作用:http://www.cnblogs.com/nianshi/archive/2009/01/14/1375639.html

  4. 紧急整理了 20 道 Spring Boot 面试题,我经常拿来面试别人!

    面试了一些人,简历上都说自己熟悉 Spring Boot, 或者说正在学习 Spring Boot,一问他们时,都只停留在简单的使用阶段,很多东西都不清楚,也让我对面试者大失所望. 下面,我给大家总结 ...

  5. Django REST framework之版本,解释器,序列化

    1 版本 2 解释器 3.序列化 1 版本 通过?后面传版本号有两种方法: 方法一 from django.shortcuts import render from rest_framework.vi ...

  6. UVa 11846 - Finding Seats Again

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. secp256k1如何使用

    https://npm.taobao.org/package/secp256k1 这个即椭圆曲线加密算法算法,随机生成一个私钥然后通过椭圆曲线加密算法算法(ECC)得到一个公钥,且无法反向 然后再使用 ...

  8. 搭建LNMP环境(CentOS 6)

    本文档介绍如何使用一台普通配置的云服务器ECS实例搭建LNMP平台的web环境. Linux:自由和开放源码的类UNIX操作系统. Nginx:轻量级网页服务器.反向代理服务器. MySQL:关系型数 ...

  9. Rman将数据文件恢复到不同的路径

    RMAN> startup nomount connected to target database (not started)Oracle instance started Total Sys ...

  10. 深入浅出的webpack构建工具---AutoDllPlugin插件(八)

    深入浅出的webpack构建工具---AutoDllPlugin插件(八) DllPlugin插件能够快速打包,能把第三方依赖的文件能提前进行预编译打包到一个文件里面去.提高了构建速度.因为很多第三方 ...