Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
You have a garland consisting of n lamps. Each lamp is colored red, green or blue. The color of the i-th lamp is si ('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 1) have distinct colors.
In other words, if the obtained garland is $t$ then for each $i$ from $1$ to $n-1$ the condition $t_i≠t_{i+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.
Input
The first line of the input contains one integer $n (1≤n≤2⋅10^5)$ — the number of lamps.
The second line of the input contains the string s consisting of n characters 'R', 'G' and 'B' — colors of lamps in the garland.
Output
In the first line of the output print one integer r — 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 t of length n — a diverse garland obtained from the initial one with minimum number of recolors. If there are multiple optimal solutions, print any of them.
Examples
input
9
RBGRRBRGG
output
2
RBGRGBRGR
input
8
BBBGBRRR
output
2
BRBGBRGR
input
13
BBRRRRGGGGGRR
output
6
BGRBRBGBGBGRG
题意:
$n$ 个灯笼排成一排,每个灯笼初始颜色可能为 $R(ed),G(reen),B(lue)$ 其中的一种,现在要求你尽量少地改变一些灯笼的颜色,使得这一排的灯笼中任意相邻的两个颜色都不相同,我们称这样的一排灯笼为“多样化的”。
题解:
令 $f[i][0,1,2]$ 表示前 $i$ 个灯笼,第 $i$ 个的颜色为 $0$ 或者 $1$ 或者 $2$ 时,最少要改变多少个灯笼的颜色才能使这前 $i$ 个灯笼“多样化的”。
转移方程很简单: dp[i][x] = min(dp[i][x],dp[i-][y]+(c[i]!=x)) ,且满足 $x!=y$。
另外唯一需要注意的是,如果 $dp[i][x]$ 更新了,就记录一下前一个灯笼的颜色。
AC代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+;
int n,c[maxn];
int dp[maxn][];
int pre[maxn][];
char ch[]={'R','G','B'};
int main()
{
cin>>n;
string s; cin>>s;
for(int i=;i<=s.size();i++)
{
if(s[i-]=='R') c[i]=;
if(s[i-]=='G') c[i]=;
if(s[i-]=='B') c[i]=;
} memset(dp,0x3f,sizeof(dp));
dp[][]=(c[]!=), dp[][]=(c[]!=), dp[][]=(c[]!=);
for(int i=;i<=n;i++)
{
for(int x=;x<;x++)
{
for(int y=;y<;y++)
{
if(x==y) continue;
if(dp[i][x]>dp[i-][y]+(c[i]!=x))
{
dp[i][x]=dp[i-][y]+(c[i]!=x);
pre[i][x]=y;
}
}
}
} int ans=0x3f3f3f3f,color;
for(int k=;k<;k++) if(ans>dp[n][k]) ans=dp[n][k], color=k; cout<<ans<<endl;
s.clear();
for(int i=n;i>=;i--) s=ch[color]+s, color=pre[i][color];
cout<<s<<endl;
}
Codeforces 1108D - Diverse Garland - [简单DP]的更多相关文章
- Codeforces - 702A - Maximum Increase - 简单dp
DP的学习计划,刷 https://codeforces.com/problemset?order=BY_RATING_ASC&tags=dp 遇到了这道题 https://codeforce ...
- Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学
https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...
- Codeforces - 474D - Flowers - 构造 - 简单dp
https://codeforces.com/problemset/problem/474/D 这道题挺好的,思路是这样. 我们要找一个01串,其中0的段要被划分为若干个连续k的0. 我们设想一个长度 ...
- Codeforces - 909C - Python Indentation - 简单dp
http://codeforces.com/problemset/problem/909/C 好像以前做过,但是当时没做出来,看了题解也不太懂. 一开始以为只有上面的for有了循环体,这里的state ...
- CodeForces 30C Shooting Gallery 简单dp
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/qq574857122/article/details/36944227 题目链接:点击打开链接 给定 ...
- Codeforces - 1033C - Permutation Game - 简单dp - 简单数论
https://codeforces.com/problemset/problem/1033/C 一开始觉得自己的答案会TLE,但是吸取徐州赛区的经验去莽了一发. 其实因为下面这个公式是 $O(nlo ...
- CodeForces 22B Bargaining Table 简单DP
题目很好理解,问你的是在所给的图中周长最长的矩形是多长嗯用坐标(x1, y1, x2, y2)表示一个矩形,暴力图中所有矩形易得递推式:(x1, y1, x2, y2)为矩形的充要条件为: (x1, ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
随机推荐
- Ubuntu 下搭建 Android 开发环境(图文)
转自 http://dawndiy.com/archives/153/ 1.安装JDK 1.下载JDK 目前最新的JDK版本是:Java SE Development Kit 7u5 下载地址: 查看 ...
- JS 引擎执行机制
JS JS 是单线程语音 JS 的 Event Loop 是 JS 的执行机制.类似于 Android Handler 消息分发机制 JS 单线程 技术的出现都跟现实世界里的应用场景密切相关 JS 单 ...
- Atitit 医学之道 attilax总结
Atitit 医学之道 attilax总结 1. 相关的学科3 1.1. 口腔医学 ok3 1.2. 人体解剖学 ok3 1.3. 生理学 ok3 1.4. 病理学 ok3 1.5. 骨伤科学 ...
- Intent的作用和表现形式简单介绍
Intent的作用和表现形式简单介绍 1.描写叙述:Intent负责相应用中一次操作的动作,动作涉及的数据,附加数据进行描写叙述.系统或者应用依据此Intent的描写叙述,负责找到相应的组件,将Int ...
- 【被C折腾系列】用C调DIOCP编码客户端通信
前几天有个朋友,说他们公司做手游,服务端用的DIOCP3里面做文件服务器,客户端用cocos-x,在调试与diocp通信时老是失败! 于是,我下载了一个Codeblocks经过几个小时的折腾,终于折腾 ...
- FTP实验
一.安装 sudo apt-get install vsftpd service vsftpd start 启动vsftpd服务 如果在不设置任何的情况下,可以以匿名的方式访问该ftp. 这时候你可以 ...
- Oracle---常用SQL语法和数据对象
1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSERT INTO 表名(字段名1, 字 ...
- pip离线安装软件包
1. 首先一台主机上安装所有python包,然后运行如下命令下载依赖包: pip freeze > requirements pip download -r requirements 当然可以在 ...
- Java Utils工具类大全(转)
源码和jar见:https://github.com/evil0ps/utils #Java Utils --- 封装了一些常用Java操作方法,便于重复开发利用. 另外希望身为Java牛牛的你们一起 ...
- linux下依赖库的版本问题引起的安装失败:libssl-dev版本问题无法安装 :libssl-dev : 依赖: libssl1.0.0 (= 1.0.1-4ubuntu3) 但是 1.0.1-4ubuntu5.31 正要被安装
依赖库版本问题引起的安装失败解决方法如下有两种: 1.是由于源需要更新,如下操作: libssl-dev : 依赖: libssl0.9.8 (= 0.9.8o-1ubuntu4) 但是 0.9.8o ...