首页 > 代码库 > Codeforces Round #373 (Div. 2) B

Codeforces Round #373 (Div. 2) B

Description

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

题意:给你颜色排列,和两种操作,一种是交换颜色,一种是改变颜色,问你最少的操作,可以使得颜色交替排列

解法:首先交替排列应该是rbrbrbrbrbrbr.. 或者brbrbrbrbr 那么讨论两种情况,一种是偶数位是r 一种是偶数位是b

再求出每种情况中,哪些位置是不符合的,比如rrbbbr 中r不在符合位置的有两个,b不在符合位置的也有两个,交换就是取两个的最小值,涂色是求它们的差值

两种情况再求一次最小值

#include<bits/stdc++.h>using namespace std;int n, a[10000];int dp[100000][3];set<char>q;map<char,int>q1,q2;char s[100010];int main(){    // string s;    scanf("%d",&n);    scanf("%s",s);    int sum1,sum2;    for(int i=0; i<n; i++)    {        if(i%2==0&&s[i]!=‘r‘)        {            q1[s[i]]++;        }        else if(i%2&&s[i]!=‘b‘)        {            q1[s[i]]++;        }    }    sum1=fabs(q1[‘r‘]-q1[‘b‘])+min(q1[‘r‘],q1[‘b‘]);;    q1.clear();    //cout<<sum1<<endl;    for(int i=0; i<n; i++)    {        if(i%2==0&&s[i]!=‘b‘)        {            q2[s[i]]++;        }        else if(i%2&&s[i]!=‘r‘)        {            q2[s[i]]++;        }    }//int Min2=min(q2[‘r‘],q2[‘b‘]);    sum2=fabs(q2[‘r‘]-q2[‘b‘])+min(q2[‘r‘],q2[‘b‘]);    q2.clear();    printf("%d\n",min(sum1,sum2));    return 0;}

  

Codeforces Round #373 (Div. 2) B