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的值最大. 题解: 假设我们已经知道 ...
随机推荐
- 数据库部分(MySql)_2
分组查询 分组查询通常和聚合函数结合使用,查询条件中每个XXX就以XXX为分组的条件: 格式:每个A的平均B select avg(B) from 表名 group by A; having 在whe ...
- 怎么使用小程序的data-*属性?
参考文档:小程序事件 怎么使用小程序的data-*属性?[data-type,data-num,……] dataset 在组件中可以定义数据,这些数据将会通过事件传递给 SERVICE. 书写方式: ...
- Docker介绍基本概念(一)
Docker介绍基本概念 1.什么是Docker? 说实话关于Docker是什么并太好说,下面我通过四点向你说明Docker到底是个什么东西. Docker是世界领先的软件容器平台. Docker使用 ...
- ubuntu中subline无法使用搜狗输入法
今天使用subline编写python程序,发现在ubuntu下无法调用搜狗输入法输入中文,结果一番搜索发现github上的sublime-text-imfix项目能修复此问题,项目地址是:https ...
- css 小知识点:inline/inline-block/line-height
inline: 此元素会被显示为内联元素,元素前后没有换行符.因此:无法设置宽度和高度- inline-block: 行内块元素.元素前后没有换行符(CSS2.1 新增的值) 用通俗的话讲,就是不独占 ...
- ATOM中MARKDOWN的使用小结
1.ctrl+shift+p ; 输入ssvvp ; 回车 2.输入markdown-preview进行安装 3.打开任意.md 文件 ; 按 ctrl-shift-m 进行预览
- Jedis 简单案例
POM 依赖 <!-- https://mvnrepository.com/artifact/redis.clients/jedis --> <dependency> < ...
- OpenVDB for Mitsuba
https://github.com/zhoub/mitsuba-vdb
- 【效率工具】SSH一键登录脚本(可一键从跳板机登录线上服务器)
说明 前阵子上线,一次性上了十个服务,一直上到凌晨才完事,期间每个服务都要先输入跳板机的登录信息来登录跳板机,然后再输入线上服务器的信息来登录线上服务器,实在是太过于麻烦,而且有些服务还有好几台服务器 ...
- SQL SERVER LINUX
以前在Linux平台上访问SQL Server时常用的方式有:http://www.freetds.org/https://sourceforge.net/projects/jtds/这方面的资料已经 ...