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开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
随机推荐
- 528. Random Pick with Weight
1. 问题 给定一个权重数组w,w[i]表示下标i的权重,根据权重从数组中随机抽取下标. 2. 思路 这道题相当于 497. Random Point in Non-overlapping Recta ...
- Flex开发框架cairngorm入门实例
Cairngorm是flex开发的mvc框架,现在在 adobe旗下.它架构和eclipse插件开发gef架构很相似,具体工作原理如下: 该框架就是一个Cairngorm.swc文件,大家可以到Cai ...
- Java生成PDF之iTextPDF的使用
今天做财务方面相关数据的导出功能,需要导出PDF和Excel,在项目经理那里得知有一个叫iTextPDF的java框架导出PDF文件很好用,于是拿来玩儿玩儿. package com.smart.pr ...
- 20145227鄢曼君《网络对抗》逆向及Bof基础
20145227鄢曼君<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任 ...
- Michael-Scott非阻塞队列(lock-free)算法的C实现
Michael-Scott非阻塞队列算法,即MS-queue算法,是1 9 9 6 年由Maged . M .Michael and M. L. Scott提出的,是最为经典的并发FIFO队列上的算法 ...
- BZOJ 2005: [Noi2010]能量采集(莫比乌斯反演)
http://www.lydsy.com/JudgeOnline/problem.php?id=2005 题意: 思路: 首先要知道一点是,某个坐标(x,y)与(0,0)之间的整数点的个数为gcd ...
- poj 2828 Buy Tickets 树状数组
Buy Tickets Description Railway tickets were difficult to buy around the Lunar New Year in China, so ...
- Linux查看版本当前操作系统内核信息
1. # uname -a (Linux查看版本当前操作系统内核信息) 输出 Linux xxx --generic #~-Ubuntu SMP Wed Jul :: UTC x86_64 x86_6 ...
- python 获取复数的实部虚部
#Initialize a complex number cn = complex(,) print("Complex Number: ",cn) print("Comp ...
- 指定html转pdf文档
1.资源 <script type="text/javascript" src="./js/canvg2.js"></script> & ...