1.23 codeforces div3 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.
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 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.
3
BRB
1
GRB
7
RGBGRBB
3
RGBRGBR 题意:输入一个字符串,按照GRB或者这三个字符组成的其他的顺序依次出现。计算哪些字符违反规则,并纠正。要使纠正次数最少,输出纠正次数和纠正过的字符串。
思路:使用next_permutation()函数,把GRB按照字典序组合一遍,然后再判断输入字符串与这6个组合的不同次数。找到最小的次数,保存这个组合。然后输出
下面是ac代码
#include<bits/stdc++.h>
using namespace std; char p[3] = {'R','G','B'};
int ans = 1e9; int main(){
string s,res;
int n;
cin>>n;
cin>>s;
sort(p,p+3); do{
int count = 0;
for(int i = 0; i < n; i++)
if(s[i] != p[i%3]) // p 会溢出,所以 mod 3 。
count++; if(count < ans)
ans = count, res = p; }while(next_permutation(p,p+3)); cout<<ans<<endl;
for(int i = 0; i < n; i++)
{
cout<<res[i%3];
}
return 0;
}
1.23 codeforces div3 C.Nice Garland的更多相关文章
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
- 【codeforces 758B】Blown Garland
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces Div3 #501 A-E(2) F以后补
感觉自己有点强迫症 不都写出来就找理由不写题解 http://codeforces.com/contest/1015 题目链接 A. Points in Segments 题目意思 n个线段 ...
- Codeforces 758B:Blown Garland(模拟)
http://codeforces.com/problemset/problem/758/B 题意:给出一个字符串,每4个位置对应一个颜色,如果为‘!’的话,代表该灯泡是坏的,问最后每个颜色坏的灯泡的 ...
- CF codeforces A. New Year Garland【Educational Codeforces Round 79 (Rated for Div. 2)】
A. New Year Garland time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Div3 #498 A-F
. A. Adjacent Replacement ...
- 【codeforces div3】【E. Cyclic Components】
E. Cyclic Components time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 2018.09.23 codeforces 1053B. Vasya and Good Sequences(前缀和)
传送门 考试的时候卡了一会儿. 显然这个答案只跟二进制位为1的数量有关. 还有一个显然的结论. 对于一个区间[l,r][l,r][l,r],如果其中单个数二进制位为1的数量最大值不到区间所有数二进制位 ...
- 2018.09.23 codeforces 1053A. In Search of an Easy Problem(gcd)
传送门 今天的签到题. 有一个很显然的结论,gcd(n∗m,k)≤2gcd(n*m,k)\le 2gcd(n∗m,k)≤2. 本蒟蒻是用的行列式求三角形面积证明的. 如果满足这个条件,就可以直接构造出 ...
随机推荐
- [knowledge][http] http
关于,底层承载协议的规定. HTTP communication usually takes place over TCP/IP connections. The [], but other port ...
- 文件处理----Properties文件处理
properties是一种属性文件,这种文件以key=value格式存储内容,代码中可以使用Properties类来读取这个文件,然后得到数据. 当配置文件用,由于难以表达层次,复杂点可以使用xml做 ...
- iOS只给矩形两个边加圆角
- (void)updataTopCornerRadius { CGRect clipRect = CGRectMake(, , self.headPhotoIv.width, self.headPh ...
- 转:centos7/rhel7安装较高版本ruby2.2/2.3/2.4+
centos7/rhel7安装较高版本ruby2.2/2.3/2.4+ 环境需求: 在Centos7.3中,通过yum安装ruby的版本是2.0.0,但是如果有些应用需要高版本的ruby环境,比 ...
- 创建日志表记录DML操作和DDL操作
创建一个日志表,记录dept表的DML操作 create table dept_log(logid number,type varchar2(50),logdate date,deptno numbe ...
- java开发中乱码的解决
总结一下,在JavaWeb中针对各种情况处理中文乱码的方法. 首先我们看下,一个请求响应的流程 浏览器------------------>Servlet容器---------------> ...
- css盒子模型之边框
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 关于WARN Dispatcher:68 - Could not find action or result报错
出现这个错 00:03:37,142 WARN Dispatcher:68 - Could not find action or result: /crm/linkMan_addLinkMan.act ...
- 云serverlinux又一次挂载指定文件夹(非扩充)
版权声明:本文为博主原创文章.转载请注明出处. https://blog.csdn.net/liuensong/article/details/27548771 新买的香港云server,系统仅仅能在 ...
- 使用Postgres,Nginx和Gunicorn将Django配置到服务器上
先决条件 首先你得先在Digital Ocean上有一台自己的服务器,如果还没有,可参考教程如何在Digital Ocean上申请服务器. 为了方便起见,我将本教程分为两部分.第一部分(步骤1 - 6 ...