zoukankan      html  css  js  c++  java
  • 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.

    要求标准是红黑交叉,无非是红开头,或者黑开头,按照这两个标准比对,各需要多少次取小,如何比对呢,记录红色不对的个数,和黑色不对的个数,一红一黑交换,多出来的改颜色,所以就是找出红和黑的最大值,这样问题就简单了。

    代码:

    #include<bits/stdc++.h>
    using namespace std;
    
    int main()
    {
        int n;
        cin>>n;
        string s;
        cin>>s;
        int a,b,c,d;
        a = b = c = d = 0;
        for(int i = 0;i < n;i ++)
        {
            if(s[i] == 'r' && i%2)a ++;
            else if(s[i] == 'b' && i%2 == 0)b ++;
            if(s[i] == 'r' && i%2 == 0)c ++;
            else if(s[i] == 'b' && i%2)d ++;
        }
        cout<<(max(a,b)>max(c,d)?max(c,d):max(a,b));
    }
  • 相关阅读:
    前后端不分离的springboot项目问题:页面框架问题
    SpringBoot使用Filter过滤器处理是否登录的过滤时,用response.sendRedirect()转发报错
    mysql千万级数据优化查询
    java进阶学习的一些思路
    java的List列表转成Tree(树形)结构列表
    cmd 一键获取 所有连接过的wifi 密码
    SQLMap用户手册【超详细】
    Python:SQLMAP参数中文解释
    初识sql注入及sqlmap
    wwwscan网站目录文件批量扫描工具
  • 原文地址:https://www.cnblogs.com/8023spz/p/7668567.html
Copyright © 2011-2022 走看看