【Codeforces】Codeforces Round #373 (Div. 2)

B. 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 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 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.

思路:

  细想只有两种模式,一种brbrbr... 另一种rbrbrb... 只需要统计这两种模式下,需要的两种操作数中最小的一个,即是答案。

swap操作可以通过取Min来实现。接下来只需要统计每种模式下,需要变换的在字母个数就行(r2b or b2r)

Code:

  

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

const int Maxn = 100005;
char str[Maxn];
char str2[Maxn];
int main()
{
    int n;
    cin>>n;
    scanf("%s",str);
    int len = n, b2r = 0, r2b = 0, _b2r = 0, _r2b = 0;
    for(int i = 0; i < len; i ++){
        if(i % 2 == 0){
            if(str[i] == ‘r‘) r2b ++;
            if(str[i] == ‘b‘) b2r ++;
        }else{
            if(str[i] == ‘r‘) _r2b ++;
            if(str[i] == ‘b‘) _b2r ++;
        }

    }
    cout<< min(max(r2b, _b2r), max(b2r, _r2b))<<endl;
    return 0;
}
时间: 2024-10-11 00:05:00

【Codeforces】Codeforces Round #373 (Div. 2)的相关文章

【排序】【规律】Codeforces Round #254 (Div. 2) - D. Rooter&#39;s Song

D. DZY Loves FFT Source http://codeforces.com/contest/445/problem/D Description Wherever the destination is, whoever we meet, let's render this song together. On a Cartesian coordinate plane lies a rectangular stage of size w?×?h, represented by a re

【模拟】Codeforces 707A Brain&#39;s Photos

题目链接: http://codeforces.com/problemset/problem/707/A 题目大意: 给一张N*M的图,只有六种颜色(如下),只含B,W,G的是黑白图,否则是彩色图.问这张图是什么图. 'C' (cyan) 'M' (magenta) 'Y' (yellow) 'W' (white) 'G' (grey) 'B' (black) 题目思路: [模拟] 签到题.直接mark记一下是否出现彩色即可. 1 // 2 //by coolxxx 3 //#include<b

【题解】Codeforces 961G Partitions

[题解]Codeforces 961G Partitions cf961G 好题啊哭了,但是如果没有不小心看了一下pdf后面一页的提示根本想不到 题意 已知\(U=\{w_i\}\),求: \[ \sum _{S}\sum_{s\in S}|s|\sum_{w \in s} w, S是U的一个k划分 \] 转换1 考虑这个\(|s|\)有点麻烦,稍微思考一下可以发现,我们最后的答案和\(w_i\)的分布没有关系,他们的贡献系数是一样的.答案只和他们的和有关. 转换2 考虑定位某个\(w_i\)对

【codeforces】Codeforces Round #277 (Div. 2) 解读

门户:Codeforces Round #277 (Div. 2) 486A. Calculating Function 裸公式= = #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; typedef long long LL ; LL n ; int main () { while ( ~scanf ( "%I64d" , &n )

【题解】Codeforces Round #600(Div.2)

Codeforces Round #600(Div.2) https://codeforces.com/contest/1253/problem A.Single Push 思路:数组各位相减,得到b-a之后的.如果全为0,或者只有一段非0且数字相同则可行,否则不可行.具体实现的话,可以左右两边指针向中间搜到第一个不为0的数,再判断中间是否均为同一个数.复杂度\(O(n)\). 注意:多组数据一定要判断是否需要清空.这里我a[n+1]没有清0,结果WA on test55-- AC代码: #in

【CF】codeforces round 369(div2)

*明早起来再贴代码 A [题意] 给定n*5的方格 将横向的相邻两个变成+输出   [题解] ...   B [题意] 一个n*n的正整数矩阵,有且仅有一个数为0 ,在这个位置填上一个数,使得每一列的和 每一行的和 两条对角线各自的和都相等 输出这个数   [题解]sb题.暴力一下.注意细节,否则你就像这样 (不是本人   C [题意] 一排点,用1~n表示,熊孩子要给这些点上色.最初每个点的颜色为ci.一共有m种颜色,如果ci=0表示这个点最初无色. 熊孩子们需要给最初为无色的点涂上色,往第i

【模拟】Codeforces 706A Beru-taxi

题目链接: http://codeforces.com/problemset/problem/706/A 题目大意: 家的坐标在sx,sy,有n辆车,每辆车坐标xi,yi,速度vi,问最快的一辆车什么时候到家. 题目思路: [模拟] 签到题. 1 // 2 //by coolxxx 3 // 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #include<iomanip> 8

【模拟】Codeforces 691B s-palindrome

题目链接: http://codeforces.com/problemset/problem/691/B 题目大意: 求一个字符串是不是镜像的(不是回文).是输出TAK否则RE. 题目思路: [模拟] 预处理镜像的字母,注意bd pq,从头尾开始模拟. 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<strin

【模拟】Codeforces 691C Exponential notation

题目链接: http://codeforces.com/problemset/problem/691/C 题目大意: 输入一个数,把它表示成a·10b形式(aEb).输出aEb,1<=a<10,b如果为1要省略Eb 题目思路: [模拟] 如果字符串没有‘.'我就在最后加上一个'.'方便处理. 先把头尾多余的0去掉,然后把这个数按照'.'拆成两半,统计整数部分的位数zs. 接着统计'.'后面的0的个数xs,再把所有数字放到一个数组里,再把头多余的0去掉(0.0000xx). 之后按照zs和sx的

【模拟】Codeforces 691A Fashion in Berland

题目链接: http://codeforces.com/problemset/problem/691/A 题目大意: n个数0或1,要求恰好n-1个1,如果n为1则那个数一定要是1 题目思路: [模拟] 水题一道.看错题目两次.. 1 // 2 //by coolxxx 3 //#include<bits/stdc++.h> 4 #include<iostream> 5 #include<algorithm> 6 #include<string> 7 #in