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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章