[CF1102D] Balanced Ternary String (贪心)

传送门:CF1102D

题目描述
给定一个由 0 , 1 , 2 0,1, 2 012组成的字符串,然后替换字符,使得在替换的次数最少的前提下,使得新获得的字符串中 0 , 1 , 2 0,1, 2 012这三个字符的数目相同,并且新获得的字符串字典序要尽可能的小。

输入格式:
第一行一个整数,代表字符串的长度。
第二行是一个字符串。

输出格式:
打印一个从给定的字符串中获得的最小的字符串。

输入样例1:

3
121

输出样例1:

021

输入样例2:

6
000000

输出样例2:

001122

输入样例3:

6
211200

输出样例3:

211200

输入样例4:

6
120110

输出样例4:

120120

思路
因为 0 , 1 , 2 0,1,2 0,1,2的数量要相等,即都等于字符串长度/3.统计出初始三个字符的个数,然后模拟用少的替换多的即可。注意一下替换的前后方向。
因为字典序要最小,那么用0替换1和2的时候,明显从左向右进行扫描替换会使字典序更小,同理,用2替换1和0的时候就要从右向左进行替换。1替换0从右往左,替换2从左往右。

AC代码

//有点长但是很无脑思路有了基本写一次就一遍过
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>
typedef long long ll;
#define IOS ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define endl '\n'
using namespace std;
string s;
int cnt_0, cnt_1, cnt_2;
int main()
{
    // IOS;
    int n;
    cin >> n >> s;
    int t = n / 3;
    for (int i : s)
    {
        if (i == '0')
            cnt_0++;
        else if (i == '1')
            cnt_1++;
        else
            cnt_2++;
    }
    while (cnt_0 != t || cnt_1 != t || cnt_2 != t)
    {
        if (cnt_0 < t)
        {
            for (int i = 0; i < n; i++)
            {
                if (cnt_0 == t)
                    break;
                if (s[i] == '1')
                {
                    if (cnt_1 > t)
                    {
                        s[i] = '0';
                        cnt_1--;
                        cnt_0++;
                    }
                }
                else if (s[i] == '2')
                {
                    if (cnt_2 > t)
                    {
                        s[i] = '0';
                        cnt_2--;
                        cnt_0++;
                    }
                }
            }
        }
        else if (cnt_2 < t)
        {

            for (int i = n - 1; i >= 0; i--)
            {
                if (cnt_2 == t)
                    break;
                if (s[i] == '1')
                {
                    if (cnt_1 > t)
                    {
                        s[i] = '2';
                        cnt_1--;
                        cnt_2++;
                    }
                }
                else if (s[i] == '0')
                {
                    if (cnt_0 > t)
                    {
                        s[i] = '2';
                        cnt_0--;
                        cnt_2++;
                    }
                }
            }
        }
        else if (cnt_1 < t)
        {

            if (cnt_2 > t)
            {
                for (int i = 0; i < n; i++)
                {
                    if (cnt_1 == t)
                        break;
                    if (s[i] == '2')
                    {
                        if (cnt_2 > t)
                        {
                            s[i] = '1';
                            cnt_2--;
                            cnt_1++;
                        }
                    }
                }
            }
            else if (cnt_0 > t)
                for (int i = n - 1; i >= 0; i--)
                {
                    if (cnt_1 == t)
                        break;
                    if (s[i] == '0')
                    {
                        if (cnt_0 > t)
                        {
                            s[i] = '1';
                            cnt_0--;
                            cnt_1++;
                        }
                    }
                }
        }
    }
    cout << s;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值