zoukankan      html  css  js  c++  java
  • D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse 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 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 n1n−1 the condition titi+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.

    Input

    The first line of the input contains one integer nn (1n21051≤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 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.

    Examples
    input
    Copy
    9
    RBGRRBRGG
    
    output
    Copy
    2
    RBGRGBRGR
    
    input
    Copy
    8
    BBBGBRRR
    
    output
    Copy
    2
    BRBGBRGR
    
    input
    Copy
    13
    BBRRRRGGGGGRR
    
    output
    Copy
    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
    %s
    ", 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
    ", ans);
    	printf("%s
    ", s);
    	return 0;
    }
    

      



  • 相关阅读:
    第九章:Elasticsearch集群优化及相关节点配置说明
    Linux115条常用命令
    第一章:引擎介绍
    第七章:JAVA引用ESWCF及部分方法说明
    第八章:SCRT搭建ES搜索引擎步骤
    第三章:ES分词简单说明
    第二章:ES索引说明
    疑难杂症:Java中Scanner连续获取int和String型发生错误.
    java实现中缀表达式转后缀表达式
    编程题:输出字符集合的所有排列组合。
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10630223.html
Copyright © 2011-2022 走看看