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 ...
随机推荐
- vue 组件 子向父亲通信用自定义方法用事件监听
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>T ...
- MySQL 双主问题集
最近试用MySQL高可用方案,需要配MySQL双主,对期间遇到的问题做下记录. 1.导出锁表问题 mysqldump 命令增加参数 --skip-opt -q 可避免导出时锁表: 2.导出\导入所有数 ...
- Trailing Zeroes (I) LightOJ - 1028(求因子个数)
题意: 给出一个N 求N有多少个别的进制的数有后导零 解析: 对于一个别的进制的数要转化为10进制 (我们暂且只分析二进制就好啦) An * 2^(n-1) + An-1 * 2^(n-2) + `` ...
- shell实践(一)---判断远程服务器中文件是否存在
1.判断一个文件的常见形式为 if [ -f filename ] #此处有-e和-f的区别 注意: 1)中括号之间的空格: 2)filename最好是绝对路径,在判断远程服务器中文件是否存在时尤为 ...
- CF993E Nikita and Order Statistics 【fft】
题目链接 CF993E 题解 我们记小于\(x\)的位置为\(1\),否则为\(0\) 区间由端点决定,转为两点前缀和相减 我们统计出每一种前缀和个数,记为\(A[i]\)表示值为\(i\)的位置出现 ...
- 20135239 Linux内核分析 期中总结
期中总结 链接 网易云课堂 MOOC 摘录与实验内容 第一周: http://www.cnblogs.com/20135239-yxlm/p/5216842.html 第二周: http://www. ...
- 解题:USACO07FEB The Cow Lexicon
题面 第一次做Trie上dp,感谢 @i207M 的资瓷 对子串们建立一棵Trie,设$dp[i][j]$表示到母串第$i$位为止在$Trie$上的$j$号节点时的最小修改数量,然后就可以枚举母串各位 ...
- [SDOI2009] HH去散步 (矩阵乘法)
link $solution:$ 将边化为点后重新建矩阵,跑$T-1$幂即可(因为跑的是新边). 最后直接找与$x,y$所相连的边即可. #include<iostream> #inclu ...
- iterm2切换显示屏vim乱行解决
http://note.youdao.com/noteshare?id=5aec9d82cc3a95b6909e9966b4aa3227
- 利用ansible来做kubernetes 1.10.3集群高可用的一键部署
请读者务必保持环境一致 安装过程中需要下载所需系统包,请务必使所有节点连上互联网. 本次安装的集群节点信息 实验环境:VMware的虚拟机 IP地址 主机名 CPU 内存 192.168.77.133 ...