题意:灯有三种颜色R,G,B。只要同一种颜色相邻就不可以。问最少需要换几次,可以使在一串灯中没有相邻灯的颜色相同。

思路:贪心思路:我们知道一个程序都是要子阶段,然后子阶段各个组合起来形成这个程序。那么对于每一个子阶段的贪心,就能形成整个程序的贪心。

贪心思路就是:只要出现相邻的相同a[i]==a[i+1], 然后,就选择一个a[i+1]!=a[i]&&a[i+1]!=a[i+2]的情况。注意:最后2个字母相同时,需要自己处理一下。

#include<iostream>
using namespace std;
const int maxn = 2e5 + ;
int main(){
int n; char st[maxn];
cin >> n; cin >> st;
if (n < ){
cout << << endl;
cout << st << endl;
}
else{
int ans = ;
for (int i = ; i < n - ; ++i){
if (st[i] == st[i + ]&&i<n-){
if (st[i] == 'G'&&st[i + ] == 'G'){ st[i + ] = 'R'; }
else if (st[i] == 'G'&&st[i + ] == 'R'){ st[i + ] = 'B'; }
else if (st[i] == 'G'&&st[i + ] == 'B'){ st[i + ] = 'R'; }
else if (st[i] == 'B'&&st[i + ] == 'B'){ st[i + ] = 'R'; }
else if (st[i] == 'B'&&st[i + ] == 'R'){ st[i + ] = 'G'; }
else if (st[i] == 'B'&&st[i + ] == 'G'){ st[i + ] = 'R'; }
else if (st[i] == 'R'&&st[i + ] == 'R'){ st[i + ] = 'B'; }
else if (st[i] == 'R'&&st[i + ] == 'G'){ st[i + ] = 'B'; }
else if (st[i] == 'R'&&st[i + ] == 'B'){ st[i + ] = 'G'; }
ans++;
}
else if (i == n - && st[i] == st[i + ]){
// cout << st[i] << st[i + 1] << endl;
if (st[i] == 'B'){ st[i + ] = 'G'; }
else if (st[i] == 'G'){ st[i + ] = 'R'; }
else if (st[i] == 'R'){ st[i + ] = 'G'; }
ans++;
}
}
cout << ans << endl;
cout << st << endl;
}
}

D. Diverse Garland的更多相关文章

  1. 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 i ...

  2. Diverse Garland CodeForces - 1108D (贪心+暴力枚举)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  3. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

  4. D. Diverse Garland-----CF字符串

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. Codeforces-D-Diverse Garland(思维)

    You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...

  6. Codeforces Round #535 (Div. 3) 解题报告

    CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...

  7. CF刷刷水题找自信1

    CF 1108A Two distinct points 题目意思:给你两个线段的起点和终点,让你给出两个不同的点,这两点分别处于两个不同的线段之中.解题思路:题目说如果存在多种可能的点,随意一组答案 ...

  8. Codeforces Round #535 (Div. 3) 题解

    Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...

  9. Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]

    hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...

随机推荐

  1. 4 ;XHTML表格

    1.表格的基本格式 2.<table>标签下的常用属性 3.<table>标签下的边框设置 4.<tr><th><td>标签下的常用属性 5 ...

  2. CSS3效果:5种预载动画效果

    实现如图所示的动画效果: 预载动画一:双旋圈 在两个不同方向旋转的圆圈.我们对内圈的转速定义了一个CSS代码,即内圈比外圈的速率快2倍.实现如图所示: html代码: <body style=& ...

  3. BZOJ 2463: [中山市选2009]谁能赢呢?(智商)

    Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3110  Solved: 2250[Submit][Status][Discuss] Descript ...

  4. cf24D. Broken robot(高斯消元)

    题意 题目链接 Sol 今天上午的A题.想出来怎么做了但是没时间写了qwq 思路很简单,首先把转移方程列一下,发现每一个位置只会从下一行/左右转移过来,而且第N行都是0,那么往下转移的都可以回带. 剩 ...

  5. js中判断空及获取当前服务的根路径

    function isValue(o) { return (this.isObject(o) || this.isString(o) || this.isNumber(o) || this.isBoo ...

  6. mybatis 自动生成文件配置

    maven 依赖配置: <!-- sql server --><dependency> <groupId>com.microsoft.sqlserver</g ...

  7. Nginx 配置下载附件让浏览器提示用户是否保存

    Nginx配置下载附件让浏览器提示用户是否保存   by:授客  QQ:1033553122   测试环境 nginx-1.10.0 问题描述: 前端页面,IE11浏览器下请求下载附件模板,针对xls ...

  8. 图说OOP基础(一)

    本文用图形化的形式描述OOP的相关知识.对OOP进行系统化的梳理,以便掌握,仅供学习分享使用,如有不足之处,还请指正. 涉及知识点: OOP的相关知识 OOP知识总图 [Object-Orientat ...

  9. 你不可不知的Java引用类型之——SoftReference源码详解

    定义 SoftReference是软引用,其引用的对象在内存不足的时候会被回收.只有软引用指向的对象称为软可达(softly-reachable)对象. 说明 垃圾回收器会在内存不足,经过一次垃圾回收 ...

  10. JavaWeb入门笔记

    Java web笔记 一.HTTP协议 HTTP(超文本传输协议),它是一种主流B/S架构中应用的通信协议.具有以下特点: 1.无状态 服务端不会记录客户端每次提交的请求,服务器一旦相应客户端之后,就 ...