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都不能取了,问你最后取完最大的和是多少. ...
随机推荐
- 阿里云日志服务采集自建Kubernetes日志(标准输出日志)
日志服务支持通过Logtail采集Kubernetes集群日志,并支持CRD(CustomResourceDefinition)进行采集配置管理.本文主要介绍如何安装并使用Logtail采集Kuber ...
- JAVA中对List<Map<String,Object>>中的中文汉字进行排序
转载于:http://blog.csdn.net/flykos/article/details/54631573 参考:http://www.jb51.net/article/88710.htm 本篇 ...
- Gitbook 命令行工具
1.Gitbook 简介 1.1 Gitbook GitBook 是一个基于 Node.js 开发的命令行工具,使用它可以很方便的管理电子书,GitBook 是目前最流行的开源书籍写作方案. 使用 G ...
- CentOS 7 安装配置zabbix 3.2.8
运行环境:CentOS 7.2 LNMP(已提前安装好此环境) 1.首先导入zabbix安装源# rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x ...
- latex学习(四)tlmgr
官网说明文档:https://tug.org/texlive/doc/tlmgr.html,2018版已经被冻结了,所以tlmgr也不会更新了,要等到下一个大的版本才能更新. 1.用tlmgr查看已经 ...
- linux下安装Oracle时交换空间不足的解决方法
摘:linux下安装Oracle时交换空间不足的解决方法 linux上安装Oracle时交换空间不足的解决办法 增加交换空间有两种方法: 严格的说,在系统安装完后只有一种方法可以增加swap,那就是本 ...
- 假设分配给命令的连接位于本地挂起事务中,ExecuteReader 要求命令拥有事务。命令的 Transaction 属性尚未初始化
{System.InvalidOperationException: 假设分配给命令的连接位于本地挂起事务中.ExecuteReader 要求命令拥有事务.命令的 Transaction 属性尚未初始 ...
- jquery checkbox checked 却不显示对勾
$("input").attr("checked", true); 或 $("input").attr("checked" ...
- 前端项目微金所1 - bootstrap模板,Compatible(兼容),Viewport(视口),条件注释,第三方依赖,MediaQuery媒体查询
前端项目微金所笔记1 基础的bootstrap模板 <!DOCTYPE html> <html lang="en"> <head> <me ...
- hdoj:2035
#include <iostream> using namespace std; int main() { long a, b; && b != ) { long resu ...