codeforces 719B:Anatoly and Cockroaches(思维)

来源:互联网 发布:mac投影仪只显示桌面 编辑:程序博客网 时间:2024/06/05 00:21

codeforces 719B:Anatoly and Cockroaches(思维)

Description

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 integern (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.

题意:一个只含有r和b的字符串,两种操作,1 交换字符串中任意两个元素的位置,2 把r替换为b,或者把b替换为r,输出把字符串转换为交替的序列需要的最小操作数

思路:目标字符串只有两种情况,rbrbrbrb或者brbrbrbr,分别求出给定字符串向这两种字符串转换所需的操作次数,取最小即可,那么如何求转换所需的最小次数呢,遍历一遍即可,分别求出与目标字符串不同的r 和 b的个数,例如,给定字符串与目标字符串字符不相同的位置上r有6个,b有4个,那么可以交换操作4次,修改操作2次,共6次

#include<bits/stdc++.h>using namespace std;char s[100010];char s1[100010];char s2[100010];int main(void){int n;while(scanf("%d%s",&n,s)!=EOF){int ans=0;s1[0]='r';s2[0]='b';for(int i=1;i<n;i++){if(s1[i-1]=='r'){s1[i]='b';}else{s1[i]='r';}if(s2[i-1]=='r'){s2[i]='b';}else{s2[i]='r';}}int count_r=0,count_b=0;for(int i=0;i<n;i++){if(s[i]!=s1[i]){if(s[i]=='r'){count_r++;}else{count_b++;}}}ans=abs(count_r-count_b)+min(count_r,count_b);count_r=0,count_b=0;for(int i=0;i<n;i++){if(s[i]!=s2[i]){if(s[i]=='r'){count_r++;}else{count_b++;}}}ans=min(ans,abs(count_r-count_b)+min(count_r,count_b));printf("%d\n",ans);}return 0;} 


原创粉丝点击