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 ...
随机推荐
- js 小程序获取本周七天
data: { weekdays:['','','','','','',''] }, onLoad: function (options) { let that = this; let now ...
- Cent7安装mysql5.7.11全过程
下载mysql(注:其他版本未测试) https://cdn.mysql.com/archives/mysql-5.7/mysql-boost-5.7.11.tar.gz 1.安装依赖包 yum -y ...
- POJ1815_Friendship
一个无向图,问你删除多少点后,可以隔断起点到终点的所有路径?输出字典序最小的删点方案. 求最小点割,先拆点,容量为1,普通边容量无穷,最大流即为应删点数. 需要求出字典序最小的方案,可以从小到大枚举所 ...
- # DZY Love Math 系列
DZY Love Math 系列 [BOZJ3309] DZY Loves Math 顺着套路就能得到:\(Ans = \sum_{T=1}\lfloor \frac{n}{T} \rfloor \l ...
- 洛谷 P1878 舞蹈课 解题报告
P1878 舞蹈课 题目描述 有\(n\)个人参加一个舞蹈课.每个人的舞蹈技术由整数来决定.在舞蹈课的开始,他们从左到右站成一排.当这一排中至少有一对相邻的异性时,舞蹈技术相差最小的那一对会出列并开始 ...
- java多线程 -- ConcurrentHashMap 锁分段 机制
hashtable效率低ConcurrentHashMap 线程安全,效率高 Java 5.0 在 java.util.concurrent 包中提供了多种并发容器类来改进同步容器 的性能. Conc ...
- 【纪中集训2019.3.13】fft
题意: 描述 一共有\(n+m\)道题,其中\(n\)道答案是\(A\),\(m\)道答案是\(B\): 你事先知道\(n和m\),问在最优情况下的期望答错次数,对\(998244353\)取模: 范 ...
- sublime_Text3中snippet设置信息头(包括作者、日期)
1.tool->new snippet(工具->新代码段) 创建一个新的snippet,并保存为author.sublime-snippet(最好在该目录(User)下再创建一个MySni ...
- win10不能被远程解决方案(开启远程桌面,防火墙仍不能被远程解决方案)
开启远程桌面,防火墙仍不能被远程解决方案 1.“Win+R”→“gpedit.msc” 2.依次展开“计算机配置”→“管理模版”→“系统”→“凭据分配”→找到“允许分配保存的凭据用于仅NTLM服务器身 ...
- 数据库之SQLite的介绍与使用20180705
一.SQLite 简介 1.介绍 SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌 ...