1.23 codeforces div3 C.Nice Garland
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ii-th lamp is sisi ('R', 'G' and 'B' — colors of lamps in the garland).
You have to recolor some lamps in this garland (recoloring a lamp means changing its initial color to another) in such a way that the obtained garland is nice.
A garland is called nice if any two lamps of the same color have distance divisible by three between them. I.e. if the obtained garland is tt, then for each i,ji,j such that ti=tjti=tj should be satisfied |i−j| mod 3=0|i−j| mod 3=0. The value |x||x| means absolute value of xx, the operation x mod yx mod y means remainder of xx when divided by yy.
For example, the following garlands are nice: "RGBRGBRG", "GB", "R", "GRBGRBG", "BRGBRGB". The following garlands are not nice: "RR", "RGBG".
Among all ways to recolor the initial garland to make it nice you have to choose one with the minimum number of recolored lamps. If there are multiple optimal solutions, print any of them.
The first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of lamps.
The second line of the input contains the string ss consisting of nn characters 'R', 'G' and 'B' — colors of lamps in the garland.
In the first line of the output print one integer rr — the minimum number of recolors needed to obtain a nice garland from the given one.
In the second line of the output print one string tt of length nn — a nice garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
3
BRB
1
GRB
7
RGBGRBB
3
RGBRGBR 题意:输入一个字符串,按照GRB或者这三个字符组成的其他的顺序依次出现。计算哪些字符违反规则,并纠正。要使纠正次数最少,输出纠正次数和纠正过的字符串。
思路:使用next_permutation()函数,把GRB按照字典序组合一遍,然后再判断输入字符串与这6个组合的不同次数。找到最小的次数,保存这个组合。然后输出
下面是ac代码
#include<bits/stdc++.h>
using namespace std; char p[3] = {'R','G','B'};
int ans = 1e9; int main(){
string s,res;
int n;
cin>>n;
cin>>s;
sort(p,p+3); do{
int count = 0;
for(int i = 0; i < n; i++)
if(s[i] != p[i%3]) // p 会溢出,所以 mod 3 。
count++; if(count < ans)
ans = count, res = p; }while(next_permutation(p,p+3)); cout<<ans<<endl;
for(int i = 0; i < n; i++)
{
cout<<res[i%3];
}
return 0;
}
1.23 codeforces div3 C.Nice Garland的更多相关文章
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- 【codeforces 758B】Blown Garland
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Div3 #501 A-E(2) F以后补
感觉自己有点强迫症 不都写出来就找理由不写题解 http://codeforces.com/contest/1015 题目链接 A. Points in Segments 题目意思 n个线段 ...
- Codeforces 758B:Blown Garland(模拟)
http://codeforces.com/problemset/problem/758/B 题意:给出一个字符串,每4个位置对应一个颜色,如果为‘!’的话,代表该灯泡是坏的,问最后每个颜色坏的灯泡的 ...
- CF codeforces A. New Year Garland【Educational Codeforces Round 79 (Rated for Div. 2)】
A. New Year Garland time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Div3 #498 A-F
. A. Adjacent Replacement ...
- 【codeforces div3】【E. Cyclic Components】
E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 2018.09.23 codeforces 1053B. Vasya and Good Sequences(前缀和)
传送门 考试的时候卡了一会儿. 显然这个答案只跟二进制位为1的数量有关. 还有一个显然的结论. 对于一个区间[l,r][l,r][l,r],如果其中单个数二进制位为1的数量最大值不到区间所有数二进制位 ...
- 2018.09.23 codeforces 1053A. In Search of an Easy Problem(gcd)
传送门 今天的签到题. 有一个很显然的结论,gcd(n∗m,k)≤2gcd(n*m,k)\le 2gcd(n∗m,k)≤2. 本蒟蒻是用的行列式求三角形面积证明的. 如果满足这个条件,就可以直接构造出 ...
随机推荐
- Linux and Oracle常用目录详解
目录详解 目录 内容 / 根目录,一切从这里开始 /bin 包含系统启动和运行所必需的二进制文件(程序) /boot 包含Linux内核.最初的RAM磁盘映像(系统启动时,驱动程序会用到),以及启动加 ...
- LeetCode 965 Univalued Binary Tree 解题报告
题目要求 A binary tree is univalued if every node in the tree has the same value. Return true if and onl ...
- 2018/04/14 理解oAuth2.0
最近都近没有更新博客了,卡在 oAuth 上了. 之前公司做统一身份的认证,不了解 oAuth 的我在这卡了两天. 于是决定仔细研究原理,理论指导实践. -- 什么是 oAuth ? 简单来说 oAu ...
- 如何在win+r 或者是win10的应用搜索输入subl就能打开sublime
这虽然不是什么技术贴,我实在不想开启sublime还要动鼠标,或者输入subl长长的全称,这里有两种做法: 第一种 在环境变量添加sublime安装目录的变量,一般sublime的安装目录会有subl ...
- MySQL 5.5 服务器变量详解(二)
innodb_adaptive_flushing={ON|OFF} 设定是否允许MySQL服务器根据工作负载动态调整刷写InnoDB buffer pool中的脏页的速率.动态调整刷写速率的目的在于避 ...
- blockdev命令 blkid命令 lsblk命令
blockdev命令 blkid命令 lsblk命令 http://www.jb51.net/LINUXjishu/310389.html block相关的命令 这篇文章主要介绍了Linux bl ...
- 282A
#include <iostream> #include <string> using namespace std; int main() { int n, plus, sub ...
- mosquitto SSL认证
[11]MQTT mosquitto 双向SSL认证配置方式 [12]MQTT mosquitto 单向SSL认证的配置方式 Mosquitto服务器的搭建以及SSL/TLS安全通信配置(比较可信) ...
- IOP知识点(2)
1 URL资源访问不足时,需要添加URL权限 2 重定向问题解决办法:3 cloud-service-factory 项目 gradlew方法 1 URL资源访问不足时,需要添加URL权 ...
- C语言strcasecmp()函数:判断字符串是否相等(忽略大小写)
头文件:#include <string.h> 定义函数:int strcasecmp (const char *s1, const char *s2); 函数说明:strcasecmp( ...