ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

C. Nice Garland Codeforces Round #535 (Div. 3) 思维题

2019-03-31 09:50:58  阅读:295  来源: 互联网

标签:Garland garland Codeforces 535 lamps output input include nice


C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard input output 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 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.

Input

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.

Output

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.

Examples input Copy
3
BRB
output Copy
1
GRB
input Copy
7
RGBGRBB
output Copy
3
RGBRGBR


这个是一个思维题,需要我们找找规律,我自己没有找到。,看了题解才会的。
这个对3取模和这里有三个数字有着莫大的联系,它的这个要求,决定了这个排列应该是对这三个数的全排的一个循环,所以这个时候就只要一一比对,找到消耗最小的
就可以了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include <vector>
#include <algorithm>
#include <string>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
char s[maxn];
int main()
{
	int n,mark=0;
	cin >> n;
	cin >> s;
	int ans = inf;
	int len = strlen(s);
	string a[6] = { "RBG","RGB","BRG","BGR","GBR","GRB" };
	for(int i=0;i<6;i++)
	{
		int cnt = 0;
		for(int j=0;j<len;j++)
		{
			if (a[i][j % 3] != s[j]) cnt++;
		}
		if(cnt<ans)
		{
			mark = i;
			ans = cnt;
		}
	}
	printf("%d\n", ans);
	for(int i=0;i<len;i++)
	{
		printf("%c", a[mark][i % 3]);
	}
	printf("\n");
	return 0;
}

  





标签:Garland,garland,Codeforces,535,lamps,output,input,include,nice
来源: https://www.cnblogs.com/EchoZQN/p/10630285.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有