D. Diverse Garland
题意:灯有三种颜色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的更多相关文章
- 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 ...
- Diverse Garland CodeForces - 1108D (贪心+暴力枚举)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
- D. Diverse Garland-----CF字符串
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Codeforces-D-Diverse Garland(思维)
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ...
- Codeforces Round #535 (Div. 3) 解题报告
CF1108A. Two distinct points 做法:模拟 如果两者左端点重合就第二条的左端点++就好,然后输出左端点 #include <bits/stdc++.h> usin ...
- CF刷刷水题找自信1
CF 1108A Two distinct points 题目意思:给你两个线段的起点和终点,让你给出两个不同的点,这两点分别处于两个不同的线段之中.解题思路:题目说如果存在多种可能的点,随意一组答案 ...
- Codeforces Round #535 (Div. 3) 题解
Codeforces Round #535 (Div. 3) 题目总链接:https://codeforces.com/contest/1108 太懒了啊~好久之前的我现在才更新,赶紧补上吧,不能漏掉 ...
- Codeforces Round #535 (Div. 3) [codeforces div3 难度测评]
hhhh感觉我真的太久没有接触过OI了 大约是前天听到JK他们约着一起刷codeforces,假期里觉得有些颓废的我忽然也心血来潮来看看题目 今天看codeforces才知道居然有div3了,感觉应该 ...
随机推荐
- 并发之AQS
一.概述 谈到并发,不得不谈ReentrantLock:而谈到ReentrantLock,不得不谈AbstractQueuedSynchronizer(AQS)! 类如其名,抽象的队列式的同步器,AQ ...
- 理解Promise的三种姿势
译者按: 对于Promise,也许你会用了,却并不理解:也许你理解了,却只可意会不可言传.这篇博客将从3个简单的视角理解Promise,应该对你有所帮助. 原文: Three ways of unde ...
- 弹性盒模型flex
一.flex flex是flexible box的缩写,意为“弹性布局”: 定义弹性布局 display:flex; box{ display:flex; } 二.基本定义 我只简单的说一下容器和项目 ...
- 【转录】原来Github上的README.md文件这么有意思——Markdown语言详解
之前一直在使用github,也在上面分享了不少的项目和Demo,每次创建新项目的时候,使用的都是默认的README.md文件,也不曾对这个文件有过什么了解.但是在看到别人写的项目的README.md里 ...
- 二进制安装 kubernetes 1.12(二) - 安装docker, 部署Flannel网络
在 node 节点上安装 docker 参考 https://www.cnblogs.com/klvchen/p/8468855.html Flannel 工作原理: 部署Flannel网络 在 ma ...
- js 从一个对象中找到属性值相等的集合
getobjs: function(objs, key, value) { var result = []; for (var i in objs) { var obj = $(objs[i]); i ...
- GIS开发之计算四参数,七参数
一.四参数 想要通过控制点计算四参数,首先需要知道四参数的相关原理,推荐这篇文章: http://www.docin.com/p-1197326043.html 根据上面的计算公式,使用最小二乘法计算 ...
- Openlayer3中应用的技术
ol3-ext有很多很丰富的效果,可以不用重复造轮子,ol3-ext示例大全:http://viglino.github.io/ol3-ext/ 在本次项目中使用到了ol3-ext的两个功能:图层管理 ...
- (网页)java数组去重总结(转)
转自CSDN: 1.背景 根据不同的业务逻辑,经常会遇到数组中存在多个重复元素的场合,总结了下数组的排序,留个记录. 2.实现方法 总结了四种方法,接下来进行展示 1.方法一 //数组去重方法一 ...
- canvas学习总结四:绘制虚线
上一章节我们说到,线性路径的绘制,主要利用movoTo(),lineTo()等方法,当然 Canvas 2D API 也提供了虚线的绘制方法,CanvasRenderingContext2D.setL ...