CodeForces 719B

来源:互联网 发布:java http下载文件 编辑:程序博客网 时间:2024/06/06 12:20

                                                                       B. Anatoly and Cockroache

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


题目意思:

给你一个字符串  然后给你了这个字符串的长度  然后让你从这个字符串中找到一种最有的策略 让这个字符串变成指定的格式 这种格式是brbrbr....  或者rbrbrbrbr......

任意一种  

你有两种操作  第一种是交换一个b和一个r的位置  

第二种操作是把任意的一个b改成r 或者把任意的r改成b

问你最少的操作次数  

思路:

贪心  , 这个字符串的最终状态 只有这两种  要从这个地方出发的话可能就比较好做这道题了   就是模拟这两种字符串的状态然后找到放错了位置的b的个数和放错了位置的r的个数

然后 一种方法的交换次数的总和就是 这两个值中的较大者  因为你首先会交换  这样的话就有了一个较小者  然后 你会用变换 处理较大者减去较小者的这么多次  你懂吧 就是这样

我刚开始是找了连续的b的个数和连续的r的个数  然后把他们的一半当成放错了地方的字符的个数来处理 当然gg  因为你变换了以后可能造成后面字符串的混乱 从而不是最优解  大概就是这样  咳咳

#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;const int N = 100005;int main(){    string str;    int n , ans;    cin >> n >> str;    int frist_rb = 0, frist_br = 0, wrong_b = 0, wrong_r = 0, wrong_B = 0, wrong_R = 0;    int str_length = str.length();    for (int i = 0; i < str_length; i ++) {        if(i % 2 == 0 && str[i] != 'r') wrong_r++;        else if(i % 2 == 1 && str[i] != 'b') wrong_b ++;        if(i % 2 == 0 && str[i] != 'b') wrong_B ++;        else if (i % 2 == 1 && str[i] != 'r')wrong_R ++;    }    frist_rb = max(wrong_r , wrong_b);    frist_br = max(wrong_B , wrong_R);    ans = min(frist_rb , frist_br);    cout << ans << endl;}/*10995116277767012108*/

原创粉丝点击