Anatoly and Cockroaches
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also living there together with students. Cockroaches might be of two colors: black and red. There are n cockroaches living in Anatoly's room.
Anatoly just made all his cockroaches to form a single line. As he is a perfectionist, he would like the colors of cockroaches in the line to alternate. He has a can of black paint and a can of red paint. In one turn he can either swap any two cockroaches, or take any single cockroach and change it's color.
Help Anatoly find out the minimum number of turns he needs to make the colors of cockroaches in the line alternate.
Input
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the number of cockroaches.
The second line contains a string of length n, consisting of characters 'b' and 'r' that denote black cockroach and red cockroach respectively.
Output
Print one integer — the minimum number of moves Anatoly has to perform in order to make the colors of cockroaches in the line to alternate.
Example
5
rbbrr
1
5
bbbbb
2
3
rbr
0
Note
In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1turn to do this.
In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires 2 turns.
In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is 0.
要求标准是红黑交叉,无非是红开头,或者黑开头,按照这两个标准比对,各需要多少次取小,如何比对呢,记录红色不对的个数,和黑色不对的个数,一红一黑交换,多出来的改颜色,所以就是找出红和黑的最大值,这样问题就简单了。
代码:
#include<bits/stdc++.h>
using namespace std; int main()
{
int n;
cin>>n;
string s;
cin>>s;
int a,b,c,d;
a = b = c = d = ;
for(int i = ;i < n;i ++)
{
if(s[i] == 'r' && i%)a ++;
else if(s[i] == 'b' && i% == )b ++;
if(s[i] == 'r' && i% == )c ++;
else if(s[i] == 'b' && i%)d ++;
}
cout<<(max(a,b)>max(c,d)?max(c,d):max(a,b));
}
Anatoly and Cockroaches的更多相关文章
- Codeforces 719B Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:st ...
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...
- B. Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test 1 second memory limit per test 256 megabytes input st ...
- Codeforces Round #373 (Div. 2) Anatoly and Cockroaches —— 贪心
题目链接:http://codeforces.com/contest/719/problem/B B. Anatoly and Cockroaches time limit per test 1 se ...
- codeforces 719B:Anatoly and Cockroaches
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- 【31.58%】【codeforces 719B】 Anatoly and Cockroaches
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Codeforces 719B Anatoly and Cockroaches(元素的交叉排列问题)
题目链接:http://codeforces.com/problemset/problem/719/B 题目大意: 有一队蟑螂用字符串表示,有黑色 ‘b’ 和红色 'r' 两种颜色,你想使这队蟑螂颜色 ...
- CodeForces 719B Anatoly and Cockroaches 思维锻炼题
题目大意:有一排蟑螂,只有r和b两种颜色,你可以交换任意两只蟑螂的位置,或涂改一个蟑螂的颜色,使其变成r和b交互排列的形式.问做少的操作次数. 题目思路:更改后的队列只有两种形式:长度为n以r开头:长 ...
- CodeForces 719B Anatoly and Cockroaches (水题贪心)
题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
随机推荐
- (26)SQLite集成与用法
概述 在Cocos2d-x中,简单数据存储,可以使用UserDefault.那么如何存储大量,不规则的数据?我们可以使用SQLite数据库存储数据.SQLite是使用非常广泛的嵌入式数据库,它有小巧 ...
- springcloud13---zuul
Zuul:API GATEWAY (服务网关): http://blog.daocloud.io/microservices-2/ 一个客户端不同的功能请求不同的微服务,那么客户端要知道所有微服务的 ...
- SQL学习笔记三(补充-2)之MySQL数据类型
阅读目录 一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型 一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 ...
- GreenOpenPaint的实现(二)打开显示图片
1.DOC中添加,核心图片文件保存在这里.之所以不用Mat,是因为CVVImage有更好的输入输出函数. 我这里直接使用了public public: CvvImage m_image; 2.重载打开 ...
- ubuntu 18.04 64bit没有声音如何解决
一.背景 1.1 笔者的机器有两张声卡,使用aplay -l可以列举出来,一张是内置声卡,另一张是显卡自带的声卡,说明声卡驱动是ok的 1.2 笔者是在浏览器中播放视频无声音 二.尝试 2.1 尝试使 ...
- R软件中排序:sort(),rank(),order()
在R中,和排序相关的函数主要有三个:sort(),rank(),order(). sort(x)是对向量x进行排序,返回值排序后的数值向量.rank()是求秩的函数,它的返回值是这个向量中对应元 ...
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树
E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...
- python 获取列表大于指定长度的元素
def long_words(n, str): word_len = [] txt = str.split(" ") for x in txt: if len(x) > n: ...
- Vue.js 计算属性是什么
Vue.js 计算属性是什么 一.总结 一句话总结: 模板 表达式 维护 在模板中表达式非常便利,但是它们实际上只用于简单的操作.模板是为了描述视图的结构.在模板中放入太多的逻辑会让模板过重且难以维护 ...
- 2-15-MySQL进阶
select select 字段列表 from 数据表 [[as] 别名] [where 条件] 别名: 数据表 [[as] 别名] select AA.money,BB.name from prod ...