D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
1 second
256 megabytes
standard input
standard output
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 diverse.
A garland is called diverse if any two adjacent (consecutive) lamps (i. e. such lamps that the distance between their positions is 11) have distinct colors.
In other words, if the obtained garland is tt then for each ii from 11 to n−1n−1 the condition ti≠ti+1ti≠ti+1 should be satisfied.
Among all ways to recolor the initial garland to make it diverse 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 diverse garland from the given one.
In the second line of the output print one string tt of length nn — a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
9
RBGRRBRGG
2
RBGRGBRGR
8
BBBGBRRR
2
BRBGBRGR
13
BBRRRRGGGGGRR
6
BGRBRBGBGBGRG 这个题目可以用暴力枚举+贪心直接来写,很快,也可以用dp写,不过dp我觉得比较复杂,所以我放弃了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
char s[maxn];
int main()
{
int n,ans=0;
cin >> n;
cin >> s;
if(n==1)
{
printf("0\n%s\n", s);
return 0;
}
int len = strlen(s);
for(int i=1;i<len;i++)
{
if(s[i]==s[i-1])
{
ans++;
s[i] = 'R';
if (s[i] != s[i + 1]&&s[i]!=s[i-1]) continue;
s[i] = 'B';
if (s[i] != s[i + 1]&&s[i]!=s[i-1]) continue;
s[i] = 'G';
if(s[i]!=s[i+1]&&s[i]!=s[i-1]) continue;
}
}
printf("%d\n", ans);
printf("%s\n", s);
return 0;
}
D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心的更多相关文章
- C. Nice Garland Codeforces Round #535 (Div. 3) 思维题
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #535 (Div. 3) 题解
Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- Codeforces Round #535 (Div. 3) 解题报告
CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...
- Codeforces Round #535(div 3) 简要题解
Problem A. Two distinct points [题解] 显然 , 当l1不等于r2时 , (l1 , r2)是一组解 否则 , (l1 , l2)是一组合法的解 时间复杂度 : O(1 ...
- Codeforces Round #377 (Div. 2) D. Exams 贪心 + 简单模拟
http://codeforces.com/contest/732/problem/D 这题我发现很多人用二分答案,但是是不用的. 我们统计一个数值all表示要准备考试的所有日子和.+m(这些时间用来 ...
- Codeforces Round #535 (Div. 3) 1108C - Nice Garland
#include <bits/stdc++.h> using namespace std; int main() { #ifdef _DEBUG freopen("input.t ...
- Codeforces Round #535 (Div. 3) E2. Array and Segments (Hard version) 【区间更新 线段树】
传送门:http://codeforces.com/contest/1108/problem/E2 E2. Array and Segments (Hard version) time limit p ...
- Codeforces Round #535 (Div. 3)
E: 题意: 给出n个整数ai和m个区间[li,ri] 你可以选择一些区间,并且将区间内的数字都减一.你要选择一些区间,然后使得改变后的数列中maxbi-minbi的值最大. 题解: 假设我们已经知道 ...
随机推荐
- [android] 请求码和结果码的作用
当一个界面中要要开启多个带有返回值的activity时,这个时候,就需要用到请求码和结果码了 调用startActivityForResult(intent,requestCode)方法,开启acti ...
- 定时任务Task
使用注解@EnableScheduling开启定时任务,会自动扫描 定义@Component作为组件被容器扫描 对于EnableScheduling是注解在启动类上,很多开关配置都会再启动类中进行设置 ...
- markdown基础入门
一.标题 语法:# 文字 注意:1个#号代表标题1,两个代表标题2,依次类推 # 标题1 ## 标题2 ### 标题3 #### 标题4 ##### 标题5 ###### 标题6 二.加粗,斜体 语法 ...
- junit单元测试注意的问题
1.有返回值的方法不能直接测试 2.带参数的方法不能直接测试 3.访问权限在public一下的方法不能直接测试 4.static静态方法不能直接测试 5.不能给出现前四个条件中任意一个的方法添加@Te ...
- Gvim 和vim 有什么区别
Gvim 和vim 有什么区别 Gvim是windows的 vim是linux的黑色的命令符 Gvim是单独的窗口下的vim,像notepad一样. vim就是在黑乎乎的cmd窗口下的编辑器.wind ...
- 7.通用程序设计_EJ
第45条: 将局部变量的作用域最小化 该条目与第13条(使类和成员的可访问性最小)本质上是类似的.要使局部变量的作用域最小化,最有利的方法就是在第一次使用它的地方声明.在每个局部变量的声明处都应该包含 ...
- vim编辑器的设置
1.vim编辑器设置分为两种设置,临时设置和永久设置 2.临时设置开启和关闭高亮模式(目前高亮模式是开启的) etc/ man.config vim man.config 在文本编辑器中命令行模式下输 ...
- input属性为number时,如何去掉+、-号?
直接上答案 <style> input[type='number']{-moz-appearance:textfield;} input[type=number]::-webkit-inn ...
- css实现div内一段文本的两端对齐
在一个固定宽度的div内,使得P标签内的文本两端对齐: text-align: justify;text-justify:inter-ideograph; <!DOCTYPE html> ...
- JS之函数实际参数转换成数组的方法[].slice.call(arguments)
实际参数在函数中我们可以使用 arguments 对象获得 (注:形参可通过 arguments.callee 获得),虽然 arguments 对象与数组形似,但仍不是真正意义上的数组. 我们可以通 ...