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 ...
随机推荐
- 【Linux】系统打开文件最大数量限制(进程打开的最大文件句柄数设置)
利用ulimit命令可以对资源的可用性进行控制. -H选项和-S选项分别表示对给定资源的硬限制(hard limit)和软限制(soft limit)进行设置. 硬限制(hard limit)一旦被设 ...
- 【MySql】[ERROR] Can't read from messagefile '/usr/share/mysql/english/errmsg.sys'
[root@zhang bin]# ./mysql_install_db --datadir=/usr/local/mysql/mydata/data/ 2018-08-18 03:09:14 [WA ...
- 【ORACLE】删除表空间,没有删除数据文件怎么办?解决办法
创建表空间 SQL> create tablespace TEST datafile='+DATA/rac/datafile/test01.dbf' size 1g; Tablespace cr ...
- 转发:[服务器]SSL安装证书教程
[服务器]SSL安装证书教程 来自阿里云教程 Tomcat服务器安装SSL证书 安装PFX格式证书 https://help.aliyun.com/document_detail/98576.ht ...
- 惠普电脑(HP PHILIPS系列)安装ubuntu后无法连接WIFI解决方案(手动安装8821CE驱动)
一步一步来, 先说环境: 我的电脑是HP PHILIPS系列,ubuntu版本是16.04 背景: win10安装ubuntu后发现无法连接wifi(但win10系统可以连接WIFI),在ubuntu ...
- IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerp
[root@k8s-master ~]# scp /etc/sysctl.d/k8s.conf root@192.168.30.23:/etc/sysctl.d/k8s.conf@@@@@@@@@@@ ...
- bootstrap弹出层嵌套弹出层后文本框不能获得焦点输入
如图上 我从页面打开一个bootstrap弹出层 然后又在 bootstrap弹出层的基础上打开一个layui的弹出层 打开后发现文本域获取不到焦点不能输入内容 而该弹出层显示的层级体现出来了 按钮 ...
- springAOP的概述及使用
Spring AOP SpringAOP是Spring中非常重要的功能模块之一,该模块提供了面向切面编程,在事务处理,日志记录,安全控制等操作中广泛使用. SpringAOP的基本概念 AOP的概念 ...
- 1V转3.3V稳压供电的芯片电路图
1V转3.3V供电是简单的,仅需要一个芯片和三个外围元件即可组成这样的一个1V转3.3V的电路图和升压电路了.可以持续稳定地供电3.3V给模块或者MCU灯电路.让后端工作稳定,同时也能控制电路的功耗. ...
- physical CPU vs logical CPU vs Core vs Thread vs Socket(翻译)
原文地址: http://www.daniloaz.com/en/differences-between-physical-cpu-vs-logical-cpu-vs-core-vs-thread-v ...