CF1102D Balanced Ternary String(构造)

15 篇文章 0 订阅
3 篇文章 0 订阅

D. Balanced Ternary String
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
3
121
outputCopy
021
inputCopy
6
000000
outputCopy
001122
inputCopy
6
211200
outputCopy
211200
inputCopy
6
120110
outputCopy
120120
题意:给你一个字符串,长度为3的倍数,字符串仅包含’0’,‘1’,‘2’,你可以改变一些值,使字符串中的0 1 2 字符的数量一致,有多种改法,输出字典序最小的
思路:
分类讨论:
当需要改0字符的时候,显然改变前面的0字符会让字符串字典序变大,所以应该改后面的0
如001100 应该改为001122
当需要改变1字符的时候,如果改为0字符,显然可以直接修改,而需要改为2的好,肯定越后面越 好
如111111 应该改为001122
当需要改2字符的时候,直接修改就行了,先0后1
如222222 应该改为001122
O(n)后答案就出来了
代码:

#include<bits/stdc++.h>
#define LL long long
#define Max 100005
const LL mod=1e9+7;
const LL LL_MAX=9223372036854775807;
using namespace std;
char a[3*Max];
int n;
int main()
{
    scanf("%d%s",&n,a);
    int c0=0,c1=0,c2=0;
    for(int i=0; i<n; i++)//统计字符0,1,2的数量
    {
        if(a[i]=='0')
            c0++;
        else if(a[i]=='1')
            c1++;
        else if(a[i]=='2')
            c2++;
    }
    int num0,num1,num2;
    num0=c0-n/3;//算出缺少的字符,和可以修改的字符,缺少是负数,可修改为正数
    num1=c1-n/3;
    num2=c2-n/3;
//   printf("%d %d %d\n",num0,num1,num2);
    for(int i=0; i<n; i++)
    {
        if(a[i]=='1' && num1>0)
        {
            //printf("%d\n",t);
            if(num0<0)
            {
                a[i]='0';
                num0++;
                num1--;
            }
            else if(c1==num1)
            {
                if(num0<0)
                {
                    a[i]='0';
                    num0++;
                    num1--;
                }
                else if(num2<0)
                {
                    a[i]='2';
                    num2++;
                    num1--;
                }
            }
            c1--;
        }
        else if(a[i]=='0' && num0>0)
        {
            if(c0==num0 && c0>0)
            {
                if(num1<0)
                {
                    a[i]='1';
                    num1++;
                    num0--;
                }
                else if(num2<0)
                {
                    a[i]='2';
                    num2++;
                    num0--;
                }
            }
            c0--;
        }
       else if(a[i]=='2' && num2>0)
        {
            if(num0<0)
            {
                a[i]='0';
                num0++;
                num2--;
            }
            else if(num1<0){
                a[i]='1';
                num1++;
                num2--;
            }
            c2--;
        }
    }
    printf("%s\n",a);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值