Codeforces-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.
Input
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.
Output
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.
Examples
input
Copy
3
BRB
output
Copy
1
GRB
input
Copy
7
RGBGRBB
output
Copy
3
RGBRGBR
思路:一共有6种情况,选出一个改变最小的
代码:
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
char a[200005];
char b[200005];
char c[200005];
int main() {
int n;
cin>>n;
scanf("%s",b);
int len=strlen(b);
strcpy(a,b);
int maxn=999999;
int s=0;
for(int t=0; t<len; t+=3) {
if(a[t]!='R'&&t<len) {
a[t]='R';
s++;
}
if(a[t+1]!='G'&&t+1<len) {
a[t+1]='G';
s++;
}
if(a[t+2]!='B'&&t+2<len) {
a[t+2]='B';
s++;
}
}
if(s<maxn) {
maxn=s;
strcpy(c,a);
}
strcpy(a,b);
s=0;
for(int t=0; t<len; t+=3) {
if(a[t]!='R'&&t<len) {
a[t]='R';
s++;
}
if(a[t+1]!='B'&&t+1<len) {
a[t+1]='B';
s++;
}
if(a[t+2]!='G'&&t+2<len) {
a[t+2]='G';
s++;
}
}
if(s<maxn) {
maxn=s;
strcpy(c,a);
}
strcpy(a,b);
s=0;
for(int t=0; t<len; t+=3) {
if(a[t]!='G'&&t<len) {
a[t]='G';
s++;
}
if(a[t+1]!='R'&&t+1<len) {
a[t+1]='R';
s++;
}
if(a[t+2]!='B'&&t+2<len) {
a[t+2]='B';
s++;
}
}
if(s<maxn) {
maxn=s;
strcpy(c,a);
}
strcpy(a,b);
s=0;
for(int t=0; t<len; t+=3) {
if(a[t]!='G'&&t<len) {
a[t]='G';
s++;
}
if(a[t+1]!='B'&&t+1<len) {
a[t+1]='B';
s++;
}
if(a[t+2]!='R'&&t+2<len) {
a[t+2]='R';
s++;
}
}
if(s<maxn) {
maxn=s;
strcpy(c,a);
}
strcpy(a,b);
s=0;
for(int t=0; t<len; t+=3) {
if(a[t]!='B'&&t<len) {
a[t]='B';
s++;
}
if(a[t+1]!='G'&&t+1<len) {
a[t+1]='G';
s++;
}
if(a[t+2]!='R'&&t+2<len) {
a[t+2]='R';
s++;
}
}
if(s<maxn) {
maxn=s;
strcpy(c,a);
}
strcpy(a,b);
s=0;
for(int t=0; t<len; t+=3) {
if(a[t]!='B'&&t<len) {
a[t]='B';
s++;
}
if(a[t+1]!='R'&&t+1<len) {
a[t+1]='R';
s++;
}
if(a[t+2]!='G'&&t+2<len) {
a[t+2]='G';
s++;
}
}
if(s<maxn) {
maxn=s;
strcpy(c,a);
}
cout<<maxn<<endl;
puts(c);
return 0;
}
Codeforces-C-Nice Garland(枚举+暴力)的更多相关文章
- Tomb Raider HihoCoder - 1829 (二进制枚举+暴力)(The 2018 ACM-ICPC Asia Beijing First Round Online Contest)
Lara Croft, the fiercely independent daughter of a missing adventurer, must push herself beyond her ...
- HDU 3484 Matrix Game 枚举暴力
上次周赛碰到这个题目,居然都没思路,真是不应该啊,起码也应该想到枚举法. 因为题目只允许每一row进行reverse操作,而每两列可以进行交换操作,所以首先把row的变化固定下来,即枚举第一列与第1- ...
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces 626D Jerry's Protest(暴力枚举+概率)
D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- codeforces 675B B. Restoring Painting(暴力枚举)
题目链接: B. Restoring Painting time limit per test 1 second memory limit per test 256 megabytes input s ...
- CodeForces - 593A -2Char(思维+暴力枚举)
Andrew often reads articles in his favorite magazine 2Char. The main feature of these articles is th ...
- Codeforces Gym 100015H Hidden Code 暴力
Hidden Code 题目连接: http://codeforces.com/gym/100015/attachments Description It's time to put your hac ...
- Educational Codeforces Round 61 C 枚举 + 差分前缀和
https://codeforces.com/contest/1132/problem/C 枚举 + 差分前缀和 题意 有一段[1,n]的线段,有q个区间,选择其中q-2个区间,使得覆盖线段上的点最多 ...
- codeforces 650D D. Image Preview (暴力+二分+dp)
题目链接: http://codeforces.com/contest/651/problem/D D. Image Preview time limit per test 1 second memo ...
随机推荐
- JS获取页面URL信息
下面我们举例一个URL,然后获得它的各个组成部分: http://i.cnblogs.com/EditPosts.aspx?opt=1 window.location.href (设置或获取整个 UR ...
- 《Android应用性能优化》 第6章 性能评测和剖析
1.时间测量 System.currentTimeMillis 精读和准确度可能不够:更改系统时间会影响结果:UTC时间1970/1/1 00:00:00到现在的毫秒数 System.nanoTime ...
- 九款常用的JS代码高亮工具
代码高亮很重要,特别是当我们想要在网站或博客中展示我们的代码的时候.通过在网站或博客中启用代码高亮,读者更方便的读取代码块. 有很多免费而且有用的代码高亮脚本.这些脚本大部分由Javascripts编 ...
- SQl Server 表链接查询
之前漏下了,这里补一偏 select * from student,score ——笛卡尔积 可以想想成c#里面的多维函数的样子,打印时每一张表的每一条数据都会连带着第二张表的所有数据 两个表的连接: ...
- gearman client的doBackground 与doNormal方法的区别
doNormal方法是阻塞的,需要等到worker处理完之后才返回,否则一直阻塞住; doBackground 方法是非阻塞的,只要将数据发送到gearmand之后,就立马返回,不等待worker的处 ...
- Python 安装 第三方库的安装技巧
Python 安装 第三方库的安装技巧 我的电脑:Windows 10 64位. Python IDE 软件:PyCharm 2016.1.4 Python version : Python 3.5. ...
- Mind Map - FreeMind
FreeMind[1]是一款基于java的免费的脑图(mind mapping)制作与管理软件.FreeMind开发项目组正致力于使其成为一款高效率的工具.FreeMind具有一键“展开/折叠”功能以 ...
- 算法Sedgewick第四版-第1章基础-2.1Elementary Sortss-007归并排序(自下而上)
一. 1. 2. 3. 二.代码 package algorithms.mergesort22; import algorithms.util.StdIn; import algorithms.uti ...
- C++面试笔记--字符串
基本上求职者进行笔试没有不考字符串的.字符串也是一种相对简单的数据结构,容易被考.事实上,字符创也是一个考验程序猿编程规范和编程习惯的重要考点. 1.替换空格:实现一个函数,把字符串中的每个空格替换成 ...
- IIC协议解释
IIC协议解释 (1)概述 I2C(Inter-Integrated Circuit BUS) 集成电路总线,该总线由NXP(原PHILIPS)公司设计,多用于主控制器和从器件间的主从通信,在小数据量 ...