• 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;
    }
    

      



  • 相关阅读:
    Python--day43--mysql自增列之起始值和步长
    Python--day43--补充之主键和外键
    Python--day42--MySQL外键定义及创建
    Python--day42--mysql操作数据库及数据表和基本增删改查
    Python--day42--mysql创建用户及授权
    sql数据库基础
    Python--day41--线程池--python标准模块concurrent.futures
    C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统
    ASP.NET 使用Session,避免用户F5刷新时重复提交(转)
    (重要,部署和发布)c# webApi 服务端和客户端 详细实例
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10630223.html
Copyright © 2020-2023  润新知