Codeforces 719B Anatoly and Cockroaches【思维】

来源:互联网 发布:mac怎么下游戏 编辑:程序博客网 时间:2024/06/05 03:51

B. Anatoly and Cockroaches
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 aren 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
5rbbrr
Output
1
Input
5bbbbb
Output
2
Input
3rbr
Output
0
Note

In the first sample, Anatoly has to swap third and fourth cockroaches. He needs1 turn to do this.

In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires2 turns.

In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is0.


题目大意:

现在进行两种操作:

①互换两个位子。

②将一个位子的颜色翻转。

问最少操作次数,使得两种颜色间隔排列。


思路:


考虑最终答案,要么是rbrbrb.............要么就是brbrbr.................

那么对于一种情况来说,其最小操作次数就是max(奇数位子上错误字符个数,偶数位子上错误字符个数);

那么对于两种情况的操作次数取最小就是答案。


Ac代码:

#include<stdio.h>#include<string.h>#include<iostream>#include<stack>using namespace std;char a[150000];int main(){    int n;    while(~scanf("%d",&n))    {        scanf("%s",a);        int cont1=0,cont2=0;        for(int i=0;i<n;i++)        {            if(i%2==0)            {                if(a[i]=='r')cont1++;            }            else            {                if(a[i]=='b')cont2++;            }        }        int output=max(cont1,cont2);        cont1=cont2=0;        for(int i=0;i<n;i++)        {            if(i%2==1)            {                if(a[i]=='r')cont1++;            }            else if(a[i]=='b')cont2++;        }        output=min(output,max(cont1,cont2));        printf("%d\n",output);    }}