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 ...
随机推荐
- 2016上海浦东汽车展览会C+罩杯 车模名单
只能帮你们到这了
- memset,memcpy,strcpy的使用与区别
1.memset 原型: extern void *memset(void *buffer, int c, int count); 功能: 把buffer所指内存区域的前count个字节设置成 ...
- oracle DCL-(grant、revoke )
1.授权GRANT <权限列表> to <user_name>; 2.收回权限REVOKE <权限列表> from <user_name>
- 返回键的复写onBackPressed()介绍
本篇文章是对Android中返回键的复写onBackPressed()进行了详细的分析介绍,需要的朋友参考下 在android开发中,当不满足触发条件就按返回键的时候,就要对此进行检测.尤其是当前Ac ...
- spark sql使用sequoiadb作为数据源
目前没有实现,理一下思路,有3中途径: 1:spark core可以使用sequoiadb最为数据源,那么是否spark sql可以直接操作sequoiadb. 2: spark sql支持Hive, ...
- mysql--表数据的操作
插入数据 --测试插入数据 INSERT 表名 VALUES|VALUE(值...)--当没有指定插入顺序的时候按照表的原来的顺序进行插入 INSERT 表名(需要赋值的字段名称) VALUES|VA ...
- 《Effective Java》第4章 类和接口
第13条:使类和成员的可访问性最小化 第一规则很简单:尽可能地使每个类或者成员不被外界访问.换句话说.应该使用与你正在编写的软件的对应功能相一致的.尽可能最小的访问级别. 对于顶层的(非嵌套的)类和接 ...
- C#获取WINDOWS系统信息
需引用System.Management命名空间, 具体代码如下: public class SysProp { public SysProp() { ManagementObjectSearcher ...
- 动态变更GridView控件列名
近段时间,确是很多专案要写,客户的个性化要求也越来越多.举个例子吧,就是从数据库取出来的字段名,在显示在GridView时,需要全部更为另外一个名称.下面的样例,并非是专案的内容,而是Insus.NE ...
- 「BZOJ 2440」完全平方数「数论分块」
题意 \(T\)组数据,每次询问第\(k\)个无平方因子的数(\(1\)不算平方因子),\(T\leq 50,k\leq 10^9\) 题解 \(k\)的范围很大,枚举肯定不行,也没什么奇妙性质,于是 ...