Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)

题意:给你一个长度为\(3*n\)的字符串,要求修改最少的次数,使得字符串中\(0,1,2\)的个数相同,并且在最少次数的情况下使字典序最小.
题解:贪心,\(0\)一定放在前面,\(1\)和\(2\)放后面,首先统计\(0,1,2\)的个数,因为题目要求字典序最小,所以我们先从左边开始遍历,如果\(2\)的个数大于\(n/3\),那么再看\(0\)和\(1\)的个数情况将其替换成\(0\)或\(1\),对\(1\)也是如此,然后我们再反着遍历,首先考虑\(1\)的情况,再考虑\(0\)的情况.具体的细节看代码吧.
代码:
int n;
char s[N];
map<int,int> mp; int main() {
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
n=read();
scanf("%s",s+1);
int cnt=n/3;
for(int i=1;i<=n;++i){
if(s[i]=='0') mp[0]++;
else if(s[i]=='1') mp[1]++;
else mp[2]++;
} for(int i=1;i<=n;++i){
if(s[i]=='2'){
if(mp[2]>cnt){
if(mp[0]<cnt) s[i]='0',mp[0]++,mp[2]--;
else if(mp[1]<cnt) s[i]='1',mp[1]++,mp[2]--;
}
}
else if(s[i]=='1'){
if(mp[1]>cnt){
if(mp[0]<cnt) s[i]='0',mp[0]++,mp[1]--;
}
}
} for(int i=n;i>=1;--i){
if(s[i]=='1' && mp[1]>cnt){
if(mp[2]<cnt) s[i]='2',mp[2]++,mp[1]--;
}
else if(s[i]=='0' && mp[0]>cnt){
if(mp[2]<cnt) s[i]='2',mp[2]++,mp[0]--;
else if(mp[1]<cnt) s[i]='1',mp[1]++,mp[0]--;
}
} for(int i=1;i<=n;++i){
printf("%c",s[i]);
} return 0;
}
Codeforces Round #531 (Div. 3) D. Balanced Ternary String (贪心)的更多相关文章
- Codeforces Round #531 (Div. 3) ABCDEF题解
Codeforces Round #531 (Div. 3) 题目总链接:https://codeforces.com/contest/1102 A. Integer Sequence Dividin ...
- Codeforces Round #184 (Div. 2) E. Playing with String(博弈)
题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...
- Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和
Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- 字符串处理 Codeforces Round #297 (Div. 2) B. Pasha and String
题目传送门 /* 题意:给出m个位置,每次把[p,len-p+1]内的字符子串反转,输出最后的结果 字符串处理:朴素的方法超时,想到结果要么是反转要么没有反转,所以记录 每个转换的次数,把每次要反转的 ...
- Codeforces Round #297 (Div. 2)C. Ilya and Sticks 贪心
Codeforces Round #297 (Div. 2)C. Ilya and Sticks Time Limit: 2 Sec Memory Limit: 256 MBSubmit: xxx ...
- Codeforces Round #877 (Div. 2) B. - Nikita and string
题目链接:http://codeforces.com/contest/877/problem/B Nikita and string time limit per test2 seconds memo ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- Codeforces Round #544 (Div. 3) C. Balanced Team
链接:https://codeforces.com/contest/1133/problem/C 题意: 给n个数, 在这n个数中选最多n个数,来组成一个队伍. 保证这n个数的最大最小差值不大于5. ...
- Codeforces Round #531 (Div. 3)
A:瞎猜. #include <bits/stdc++.h> using namespace std; int main(){ ios::sync_with_stdio(false); i ...
随机推荐
- mysql中的基本注入函数
1. 常见数据库注入函数: MYSQL: and length((user))>10 ACCESS: and (select count() from MSysAccessObject)> ...
- linux最大打开文件句柄数
linux最大打开文件句柄数,即打开文件数最大限制,就是规定的单个进程能够打开的最大文件句柄数量(Socket连接也算在里面,默认大小1024) liunx中文件句柄有两个限制,一种是用户级的,一种是 ...
- stat filename
查看文件的mtime,atime,ctime 3个时间
- 【UML】基本介绍与类图(依赖、泛化、实现、关联、聚合、组合关系)
文章目录 UML基本介绍 UML图 UML类图 类图-依赖关系(Dependence) 类图-泛化关系(generalization) 类图-实现关系(Implementation) 类图-关联关系( ...
- 【Oracle】查看当前连接数和最大连接数
查看当前数据库连接数 select count(*) from v$session where username is not null; select count(*) from v$process ...
- oracle字符集与乱码(转)
作者:hcling97 http://blog.sina.com.cn/hcling97 2013年5月15日 转载请注明出处 字符集问题一直叫人头疼,究其原因还是不能完全明白其运作原理. 在整 ...
- 关于springboot项目通过jar包启动之后无法读取项目根路径静态资源
在一次项目开发过程中,项目根路径下存放了一张图片,生成二维码的时候调用了该图片作为二维码的logo,在windows环境下二维码可以正常生成,但是部署到生产测试环境之后二维码生成报错,FileNotF ...
- In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego Ongaro and John Ousterhout.
In Search of an Understandable Consensus Algorithm" (https://raft.github.io/raft.pdf) by Diego ...
- Go Code Review Comments
Go Code Review Comments https://golang.org/wiki/CodeReviewComments
- 亿级用户下的新浪微博平台架构 前端机(提供 API 接口服务),队列机(处理上行业务逻辑,主要是数据写入),存储(mc、mysql、mcq、redis 、HBase等)
https://mp.weixin.qq.com/s/f319mm6QsetwxntvSXpKxg 亿级用户下的新浪微博平台架构 炼数成金前沿推荐 2014-12-04 序言 新浪微博在2014年3月 ...