Balanced Ternary String

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.

Input
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’.

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 n is divisible by 3 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
Output
120120
代码
应该是某个代码敲错了,没注意

#include<iostream>
using namespace std;
int arr[1000000];
int main()
{
    int n;
    cin >> n;
    int sum0 = 0;
    int sum1 = 0;
    int sum2 = 0;
    for(int i = 0; i < n; i++)
    {
    	char x;
    	cin >> x;
    	arr[i] = x - '0';
        //cin >> arr[i];
        if(arr[i] == 0) sum0++;
        if(arr[i] == 1) sum1++;
        if(arr[i] == 2) sum2++;
    }
    int m = n/3;
    if(sum0 > m)
    {
        if(sum1 >= m)
        {
            int x = sum0 - m;
            int y = sum1 - m;
            for(int i = n-1; i >=0; i--)
            {
                if(arr[i] == 0&&x>0)
                {
                    arr[i] = 2;
                    x--;
                }
                if(arr[i] ==1 && y > 0)
                {
                    arr[i] = 2;
                    y--;
                }
            }
        }
        else
        {
            if(sum2 >= m)
            {
                int x = sum0 - m;
                int y = sum2 - m;
                for(int i = n-1; i >=0; i--)
                {
                    if(arr[i] == 0&&x>0)
                    {
                        arr[i] = 1;
                        x--;
                    }
                }
                for(int i = 0; i < n; i++)
                {
                    if(arr[i] == 2&&y>0)
                    {
                        arr[i] = 1;
                        y--;
                    }
                }
            }
            else
            {
                int x = m - sum1;
                int y = m - sum2;
                for(int i = n-1; i >=0; i--)
                {
                    if(arr[i]==0&&y>0)
                    {
                        arr[i] = 2;
                        y--;
                    }
                }
                for(int i = n-1; i >= 0; i--)
                {
                    if(arr[i] == 0&& x >0)
                    {
                        arr[i] = 1;
                        x--;
                    }
                }
            }
        }
    }
    else if(sum0 == m)
    {
        if(sum1 >= m)
        {
            int x = sum1 - m;
            for(int i = n-1; i >=0; i--)
            {
                if(arr[i] == 1&&x>0)
                {
                    arr[i] =2;
                    x--;
                }
            }
        }
        else
        {
            int x = sum2 - m;
            for(int i = 0; i < n; i++)
            {
                if(arr[i] == 2&&x>0)
                {
                    arr[i] = 1;
                    x--;
                }
            }
        }
    }
    else
    {
        if(sum1>=m)
        {
            if(sum2>=m)
            {
                int x = sum1-m;
                int y = sum2-m;
                for(int i = 0; i < n; i++)
                {
                    if(arr[i]==1&&x>0)
                    {
                        arr[i] = 0;
                        x--;
                    }
                    if(arr[i]==2&&y>0)
                    {
                        arr[i] = 0;
                        y--;
                    }
                }
            }
            else
            {
                int x = m - sum0;
                int y = m - sum2;
                for(int i = 0; i < n; i++)
                {
                    if(arr[i]==1&&x>0)
                    {
                        arr[i] = 0;
                        x--;
                    }
                }
                for(int i = n-1; i >=0; i--)
                {
                    if(arr[i] == 1 && y >0)
                    {
                        arr[i] = 2;
                        y--;
                    }
                }
            }
        }
        else
        {
            int x = m - sum0;
            int y = m - sum1;
            for(int i = 0; i < n; i++)
            {
                if(arr[i] == 2 && x >0 )
                {
                    arr[i] = 0;
                    x--;
                }
            }
            for(int i = 0; i < n; i++)
            {
                if(arr[i] == 2 && y >0)
                {
                    arr[i] = 1;
                    y--;
                }
            }
        }
    }
    for(int i = 0; i < n; i++)
    {
        cout << arr[i];
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值