【CodeForces 719B】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 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.
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
5
rbbrr
1
5
bbbbb
2
3
rbr
0
题意
有n个字符,要么是r,要么是b,要使其成为交替出现的字符所组成的字符串
如:rbrbrbr 或 brbrbrbrb……
每次你可以修改一个字符,或者交换两个字符的位置。
问你最少改变的次数是多少
#include<bits/stdc++.h> using namespace std; int main() { char a[100005]; int n,s1=0,s2=0,minn,i; scanf("%d %s",&n,a); for(i=0;i<n;i++) { if(i%2) //rbrbrbr { if(a[i]!='r') s1++; } else { if(a[i]!='b') s2++; } } minn=abs(s1-s2)+min(s1,s2); //他们的差(直接变色)+奇数位和偶数位错误的最小值(交换) s1=s2=0; for(i=0;i<n;i++) { if(i%2) //brbrbrb { if(a[i]!='b') s1++; } else { if(a[i]!='r') s2++; } } minn=min(minn,abs(s1-s2)+min(s1,s2)); cout<<minn<<endl; }
上一篇: 喝羊奶有什么好处
下一篇: 梅毒患者如何饮食?推荐梅毒的5种食疗方法