• Codeforces 1108D


    题目链接: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-1][y]+(c[i]!=x)) ,且满足 $x!=y$。

    另外唯一需要注意的是,如果 $dp[i][x]$ 更新了,就记录一下前一个灯笼的颜色。

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+5;
    int n,c[maxn];
    int dp[maxn][3];
    int pre[maxn][3];
    char ch[3]={'R','G','B'};
    int main()
    {
        cin>>n;
        string s; cin>>s;
        for(int i=1;i<=s.size();i++)
        {
            if(s[i-1]=='R') c[i]=0;
            if(s[i-1]=='G') c[i]=1;
            if(s[i-1]=='B') c[i]=2;
        }
    
        memset(dp,0x3f,sizeof(dp));
        dp[1][0]=(c[1]!=0), dp[1][1]=(c[1]!=1), dp[1][2]=(c[1]!=2);
        for(int i=2;i<=n;i++)
        {
            for(int x=0;x<3;x++)
            {
                for(int y=0;y<3;y++)
                {
                    if(x==y) continue;
                    if(dp[i][x]>dp[i-1][y]+(c[i]!=x))
                    {
                        dp[i][x]=dp[i-1][y]+(c[i]!=x);
                        pre[i][x]=y;
                    }
                }
            }
        }
    
        int ans=0x3f3f3f3f,color;
        for(int k=0;k<3;k++) if(ans>dp[n][k]) ans=dp[n][k], color=k;
    
        cout<<ans<<endl;
        s.clear();
        for(int i=n;i>=1;i--) s=ch[color]+s, color=pre[i][color];
        cout<<s<<endl;
    }
  • 相关阅读:
    【SQL Server数据迁移】32位的机器:SQL Server中查询ORACLE的数据
    ORACLE存储过程,循环语法和游标
    Oracle变量的定义、赋值及使用
    Oracle数据库常用语法
    Windows下将JAVA jar注册成windows服务
    Java压缩图片
    如何去除图片背景?这款一键抠图软件帮你
    远程办公,这 13 个工具比微信、QQ更好用
    java程序在windows后台执行的办法
    Usage of API documented as @since 1.6+……的解决办法
  • 原文地址:https://www.cnblogs.com/dilthey/p/10454314.html
Copyright © 2020-2023  润新知