D - Balanced Ternary String (贪心)
题目链接:http://codeforces.com/contest/1102/problem/D
题目大意:给你一个字符串,这个字符串是由0,1,2构成的,然后让你替换字符,使得在替换的次数最少的前提下,使得新获得的字符串中0,1,2这三个字 符的数目相同,并且新获得的字符串字典序要尽可能的小。
具体思路: 我们先统计出每个字符的个数,想一下,除了三个都相等的情况下,这三个中的某一个肯定是大于n/3的,我们就枚举每一个字符。
如果是2多的话,我们就用1和0从前面进行替换。
如果是1多的话,我们就用2和0进行替换,为了保证字典序最小,我们将0从前面进行替换,2从后面进行替换,
如果是0多的话,我们就从后面开始替换,先从2开始,然后再从1开始。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+;
int num[];
char str[maxn];
int main()
{
int len;
scanf("%d",&len);
scanf("%s",str);
for(int i=; i<len; i++)
{
num[str[i]-'']++;
}
int tmp=len/;
if(num[]>tmp)
{
for(int i=len-; i>=; i--)
{
if(str[i]=='')
{
if(num[]<tmp&&num[]>tmp)
{
num[]++,num[]--,str[i]='';
}
else if(num[]<tmp&&num[]>tmp)
{
num[]++,num[]--,str[i]='';
}
}
}
}
if(num[]>tmp)
{
for(int i=; i<len; i++)
{
if(str[i]=='')
{
if(num[]<tmp&&num[]>tmp)
{
num[]++,num[]--,str[i]='';
}
}
}
for(int i=len-; i>=; i--)
{
if(str[i]=='')
{
if(num[]<tmp&&num[]>tmp)
{
num[]++;
num[]--;
str[i]='';
}
}
}
}
if(num[]>tmp)
{
for(int i=; i<len; i++)
{
if(str[i]=='')
{
if(num[]<tmp&&num[]>tmp)
{
num[]++;
num[]--;
str[i]='';
}
else if(num[]<tmp&&num[]>tmp)
{
num[]++;
num[]--;
str[i]='';
}
}
}
}
printf("%s\n",str);
}
D - Balanced Ternary String (贪心)的更多相关文章
- Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)
题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小. 题解:贪心,\(0\)一定放在前面,\(1\)和\(2\ ...
- Balanced Ternary String CodeForces - 1102D (贪心+思维)
You are given a string ss consisting of exactly nn characters, and each character is either '0', '1' ...
- Balanced Ternary String(贪心+思维)
题目链接:Balanced Ternary String 题目大意:给一个字符串,这个字符串只由0,1,2构成,然后让替换字符,使得在替换字符次数最少的前提下,使新获得的字符串中0,1,2 这三个字符 ...
- 牛客多校第四场 A Ternary String
题目描述 A ternary string is a sequence of digits, where each digit is either 0, 1, or 2. Chiaki has a t ...
- 2018牛客网暑期ACM多校训练营(第四场) A - Ternary String - [欧拉降幂公式][扩展欧拉定理]
题目链接:https://www.nowcoder.com/acm/contest/142/A 题目描述 A ternary string is a sequence of digits, where ...
- CodeForces - 1009B Minimum Ternary String
You are given a ternary string (it is a string which consists only of characters '0', '1' and '2'). ...
- 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)
链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...
- B. Minimum Ternary String (这个B有点狠)
B. Minimum Ternary String time limit per test 1 second memory limit per test 256 megabytes input sta ...
- codeforces ~ 1009 B Minimum Ternary String(超级恶心的思维题
http://codeforces.com/problemset/problem/1009/B B. Minimum Ternary String time limit per test 1 seco ...
随机推荐
- java异常处理的throw和throws的区别
1. 区别 throws是用来声明一个方法可能抛出的所有异常信息,throws是将异常声明但是不处理,而是将异常往上传,谁调用我就交给谁处理.而throw则是指抛出的一个具体的异常类型. 2.分别介绍 ...
- Install .Net Core For CentOS
Install .NET Core SDK Before you start, please remove any previous versions of .NET Core from your s ...
- HTML之绝对路径与相对路径
路径指文件存放的位置,在网页中利用路径可以引用文件,插入图像.视频等.表示路径的方法有两种:相对路径,绝对路径.以下讨论均是在HTML环境下进行. 相对路径 相对路径是指目标相对于当前文件的路径,网页 ...
- xml 类详解
- Nim游戏学习笔记
- 内存映像分析工具Eclipse Memory Analyzer
1. Eclipse Memory Analyzer安装 Help ->Eclipse Marketplace,搜索Memory,点击install,->confirm->同意证书内 ...
- PGM学习之四 Factor,Reasoning
通过上一篇文章的介绍,我们已经基本了解了:Factor是组成PGM模型的基本要素:Factor之间的运算和推理是构建高维复杂PGM模型的基础.那么接下来,我们将重点理解,Factor之间的推理(Rea ...
- 一些常用的基础Linux操作指令
复习的时候顺便分享我学的知识,虽不是什么牛的技术分享,只是一些基础,基础打好了技术慢慢就提高了!一起加油一起共勉! 具体的vi和vim命令集太多了,以后的随笔我也会分享出来,没必要全记住,记住常用的就 ...
- png?wxfrom=5&wx_lazy=1
作为一名游戏行业的视频&平面设计师,平时的工作就是为公司发行的游戏制作宣传视频.广告.平面宣传图,打交道最多的自然就是Adobe家族的设计软件,Photoshop.AfterEffects.P ...
- FourAndSix: 2.01靶机入侵
0x01 前言 FourAndSix2是易受攻击的一个靶机,主要任务是通过入侵进入到目标靶机系统然后提权,并在root目录中并读取flag.tx信息 FourAndSix2.镜像下载地址: htt ...