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. VMware或者KVM克隆的虚拟机网卡无法启动

    在VMware里克隆出来的CentOS Linux.. ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting ...

  2. FTP服务器访问主动模式、被动模式

    在公司里面,经常需要访问外网FTP取资料等情况.但是有时用windows界面访问经常遇到各种问题. 下面介绍两种客户端是如何访问ftp服务器. 首先我们需要说明:防火墙,是阻拦外界与内部的通讯的一道关 ...

  3. php中fopen不能创建中文文件名文件的问题

    之前网页的chartset用的是utf-8,文件也用utf-8,然后用fopen()创建一个中文文件名的文件时问题就出来了,文件名都是乱 码! 查看了很多文档试了不少方法都解决不了,本来想着用别的方法 ...

  4. MySQL之视图、触发器、存储过程、函数、事务、数据库锁

    一.视图 视图:是一个虚拟表,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据. 视图的特点: 1.视图的列可以来自不同的表,是表的抽象和逻辑意义上建立的新关系: 2.视图是由基本 ...

  5. 洛谷 3391 【模板】文艺平衡树 Treap区间翻转

    [题解] 用Treap维护这个序列. 加入的时候直接插入到末尾,这样Treap就变成一棵以插入时间先后为排序关键字的二叉搜索树. 对于翻转操作,我们分裂出需要翻转的区间,给这个区间的root打一个翻转 ...

  6. PAT 1107 Social Clusters

    When register on a social network, you are always asked to specify your hobbies in order to find som ...

  7. Redis 原子操作INCR

    The content below come from http://try.redis.io/ There is something special about INCR. Why do we pr ...

  8. flask运行环境搭建(nginx+gunicorn)

    系统:CentOS7.2(阿里云ESC) 1.python版本,使用的是默认的python2.7(或者先安装python3) 2.安装nginx,yum install -y nginx 3.安装vi ...

  9. jquery源码分析(三)——工具函数

    jQuery.extend({ expando: "jQuery" + ( version + Math.random() ).replace( /\D/g, "&quo ...

  10. vue 组件通信传值

    父子组件通信: 子组件 <template> <div> <h3 @click="alerrt"> 我是子组件一</h3> < ...