Codeforces 719B Anatoly and Cockroaches【思维】

解决通过最少操作使两种颜色交替排列的问题,允许交换位置或改变颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Anatoly and Cockroaches
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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.


题目大意:

现在进行两种操作:

①互换两个位子。

②将一个位子的颜色翻转。

问最少操作次数,使得两种颜色间隔排列。


思路:


考虑最终答案,要么是rbrbrb.............要么就是brbrbr.................

那么对于一种情况来说,其最小操作次数就是max(奇数位子上错误字符个数,偶数位子上错误字符个数);

那么对于两种情况的操作次数取最小就是答案。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<stack>
using namespace std;
char a[150000];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        scanf("%s",a);
        int cont1=0,cont2=0;
        for(int i=0;i<n;i++)
        {
            if(i%2==0)
            {
                if(a[i]=='r')cont1++;
            }
            else
            {
                if(a[i]=='b')cont2++;
            }
        }
        int output=max(cont1,cont2);
        cont1=cont2=0;
        for(int i=0;i<n;i++)
        {
            if(i%2==1)
            {
                if(a[i]=='r')cont1++;
            }
            else if(a[i]=='b')cont2++;
        }
        output=min(output,max(cont1,cont2));
        printf("%d\n",output);
    }
}















评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值