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.

Example
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 1turn 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.


就是一个普通的贪心,大致题意就是给定现在蟑螂的颜色排序,可以给蟑螂染色或者交换两个蟑螂的位置,求出最后让蟑螂颜色相间所需要的最少步骤。

最后蟑螂要求颜色相间,那么最后的顺序要不就是rbrbrbrbrbrb……或者是brbrbrbrbrbr……

用三个数组,一个保存现在的蟑螂顺序,一个保存rbrbrbrbrb的顺序,一个保存brbrbrbr的顺序。然后让第一个数组和第二第三个数组分别作比较,找出b和r各有多少个不同。

注意交换顺序和再染色之间,首选应该是交换顺序,无法交换时再选择染色。

#include<iostream>
#include<stdio.h>
#include<string>
#include<math.h>
#include<queue>
#include<vector>
#include<string.h>
#include<iterator>
using namespace std;
typedef unsigned long long ll;
char color[100000],c1[100000],c2[100000];
int main()
{
    int n,i,j;
    int r1,b1,r2,b2;
    while(~scanf("%d",&n)){
        cin>>color;
        for(i=0;i<n;i++){
            if(i%2==0){
                c1[i] = 'r';c2[i] = 'b';
            }
            else{
                c1[i] = 'b';c2[i] = 'r';
            }
        }
        r1 = b1 = r2 = b2 = 0;
        for(i=0;i<n;i++){
            if(color[i]!=c1[i]){
                if(color[i]=='r') r1++;
                else b1++;
            }
            if(color[i]!=c2[i]){
                if(color[i]=='r') r2++;
                else b2++;
            }
        }
        int m1 = max(r1,b1);
        int m2 = max(r2,b2);
        int re = min(m1,m2);
        cout<<re<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值