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了,感觉应该 ...
随机推荐
- 2018年,JavaScript都经历了什么?
摘要: 对JSer来说,这是很有意思的1年. 本文灵感来自JavaScript Weekly周报,欢迎大家订阅. The State of JavaScript 2018 The State of J ...
- mysql日期时间函数
日期时间函数1.取得当前日期时间SELECT NOW(), SYSDATE(),CURRENT_TIMESTAMP() FROM DUAL2.取得当前日期SELECT CURDATE(),CURREN ...
- 【20190130】CSS-文字排版
字间距:letter-space:**px: 禁止中文词内换行:给每个单词设置 white-space: nowrap;
- JavaScript面向对象编程指南(六) 继承
第6章 继承 6.1 原型链 6.1.1原型链示例 原型链法:Child.prototype=new Parent(); <script> function Shape(){ this.n ...
- java Web三大组件--监听器
监听器概述 监听器(Listener)是一种特殊的Servlet技术,它可以监听Web应用的上下文信息.Servlet请求信息和Servlet会话信息,即ServletContext.ServletR ...
- Linux 线程实现模型
1.Linux 线程的调度实现可以有两种模型, 一种是完全由进程负责,进程内启动一个线程调度器,由进程内的线程调度器完成调度. 缺点是:(1)各个线程自己加主动释放cpu的流程 (2)进程可能阻塞,达 ...
- android默认开启adb调试方法分析
用adb调试android时,每次接入usb线,都会提示一个确认打开usb调试功能的窗口,有时候,我们需要默认打开usb调试功能.或者无需弹出对话框,直接默认开启.这个我们需要分析adb的流程了. a ...
- javascript的介绍、javascirpt的存在形式、javascript注释
本文内容: javascript的介绍 javascirpt的存在形式 javascript注释 javascript的介绍: javascript是一种解释性脚本语言 它的用途是给html网页增加动 ...
- MyBatis笔记----MyBatis 入门经典的两个例子: XML 定义与注解定义
----致敬MyBatis官方开放文档让大家翻译,不用看书直接看文档就行了,mybatis的中文文档还需要完备的地方 简介 什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以 ...
- javascript语言之 this概念
转载 猫猫小屋 http://www.maomao365.com/?p=837 由于javascript是一种解释性语言,运行时才会解释所有的变量值,所以对于javascript中this所指的对象是 ...