B. 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 toalternate. 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.

Examples
input
5
rbbrr
output
1
input
5
bbbbb
output
2
input
3
rbr
output
0
Note

In the first sample, Anatoly has to swap third and fourth cockroaches. He needs 1 turn 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.

思路:

  细想只有两种模式,一种brbrbr... 另一种rbrbrb... 只需要统计这两种模式下,需要的两种操作数中最小的一个,即是答案。

swap操作可以通过取Min来实现。接下来只需要统计每种模式下,需要变换的在字母个数就行(r2b or b2r)

Code:

  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector> using namespace std; const int Maxn = ;
char str[Maxn];
char str2[Maxn];
int main()
{
int n;
cin>>n;
scanf("%s",str);
int len = n, b2r = , r2b = , _b2r = , _r2b = ;
for(int i = ; i < len; i ++){
if(i % == ){
if(str[i] == 'r') r2b ++;
if(str[i] == 'b') b2r ++;
}else{
if(str[i] == 'r') _r2b ++;
if(str[i] == 'b') _b2r ++;
} }
cout<< min(max(r2b, _b2r), max(b2r, _r2b))<<endl;
return ;
}

【Codeforces】Codeforces Round #373 (Div. 2)的更多相关文章

  1. 【Codeforces】CF Round #592 (Div. 2) - 题解

    Problem - A Tomorrow is a difficult day for Polycarp: he has to attend \(a\) lectures and \(b\) prac ...

  2. 【二分】CF Round #587 (Div. 3)E2 Numerical Sequence (hard version)

    题目大意 有一个无限长的数字序列,其组成为1 1 2 1 2 3 1.......1 2 ... n...,即重复的1~1,1~2....1~n,给你一个\(k\),求第\(k(k<=10^{1 ...

  3. Codeforces Round #373 (Div. 2)A B

    Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...

  4. Codeforces Round #373 (Div. 1)

    Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...

  5. 【题解】Codeforces 961G Partitions

    [题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \s ...

  6. Codeforces 716A Crazy Computer 【模拟】 (Codeforces Round #372 (Div. 2))

    A. Crazy Computer time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. Codeforces 716B Complete the Word【模拟】 (Codeforces Round #372 (Div. 2))

    B. Complete the Word time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. 【Codeforces】Codeforces Round #551 (Div. 2)

    Codeforces Round #551 (Div. 2) 算是放弃颓废决定好好打比赛好好刷题的开始吧 A. Serval and Bus 处理每个巴士最早到站且大于t的时间 #include &l ...

  9. 【codeforces】Codeforces Round #277 (Div. 2) 解读

    门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include ...

  10. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

随机推荐

  1. codevs——4189 字典&&HihoCoder #1014 : Trie树

    题目描述 Description 最经,skyzhong得到了一本好厉害的字典,这个字典里整整有n个单词(1<=n<=200000) 现在skyzhong需要在字典里查询以某一段字母开头的 ...

  2. 标准C 语言总结

    ***************C语言****************** --day01-- Linux是一个和Windows类似的操作系统 通常通过终端软件使用Linux操作系统 终端软件里只能使用 ...

  3. uva253 Cube painting(UVA - 253)

    题目大意 输入有三种颜色判断两个骰子是否相同 思路(借鉴) ①先用string输入那12个字符,然后for出两个骰子各自的字符串 ②这里用的算法是先找出第一个的三个面与第二个的六个面去比较,如果找到相 ...

  4. Linux:在安装虚拟机时如何选择网络类型?

    如图所示工作站提供了5种网络模式,我们主要用的就是上面3种:桥接模式,NAT,仅主机 1,仅主机模式 仅主机模式:虚拟机用过vmnet1网卡与宿主机通信,但是不能与物理局域网内其他主机通信,可利用虚拟 ...

  5. Mysql 模糊查询总结

    语法: SELECT 字段集 FROM 表名 WHERE 字段名 LIKE 条件 模糊查询有四种匹配模式: 1.%:匹配任意0个或多个字符. 2._:匹配任意1个字符. 3.[]:匹配括号内所列字符中 ...

  6. 第四节:Web爬虫之pyquery解析库

    PyQuery库也是一个非常强大又灵活的网页解析库,如果你有前端开发经验的,都应该接触过jQuery,那么PyQuery就是你非常绝佳的选择,PyQuery 是 Python 仿照 jQuery 的严 ...

  7. 【Codeforces 225C】Barcode

    [链接] 我是链接,点我呀:) [题意] 让你把每一列都染成一样的颜色 要求连续相同颜色的列的长度都大于等于x小于等于y 问你最少的染色次数 [题解] 先求出每一列染成#或者.需要染色多少次 设f[0 ...

  8. web开发如何使用高德地图API(四)通过AMap.Marker自定义标点

    说两句: 以下内容除了我自己写的部分,其他部分在高德开放平台都有(可点击外链访问). 我所整理的内容以实际项目为基础希望更有针对性的,更精简. 点击直奔主题. 准备工作: 首先,注册开发者账号,成为高 ...

  9. [COGS 2264]魔法传输

    [COGS 2264]魔法传输 题目 自从看了<哈利波特>,小Y就十分渴望获得魔法值.于是他和一群向往魔法的孩子(当然这些孩子们都是不会魔法的)来到了哈利波特的家,大家坐成一排.哈利波特会 ...

  10. hdu 4280

    题意:求XY平面上最左边的点到最右边的点的最大流. 分析:数据量大,EK算法TLE,要用SAP算法.SAP算法用的是 http://www.cnblogs.com/kuangbin/archive/2 ...