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.
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.
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.
5 rbbrr
1
5 bbbbb
2
3 rbr
0
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.
题目大意:
现在进行两种操作:
①互换两个位子。
②将一个位子的颜色翻转。
问最少操作次数,使得两种颜色间隔排列。
思路:
考虑最终答案,要么是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);
}
}