zoukankan      html  css  js  c++  java
  • 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
    ",min(sum1,sum2));
        return 0;
    }
    

      

  • 相关阅读:
    常用坐标系椭球参数整理
    ArcEngine编辑保存错误:Unable to create logfile system tables
    ArcEngine:The XY domain on the spatial reference is not set or invalid错误
    dockManager中DockPanel的刷新问题!
    ibatis实现Iterate的使用
    mongodb用子文档做为查询条件的两种方法
    Eclipse中的文件导航插件StartExplorer
    mongoVUE的增删改查操作使用说明
    什么是脏读,不可重复读,幻读
    转:Maven常用命令
  • 原文地址:https://www.cnblogs.com/yinghualuowu/p/5902722.html
Copyright © 2011-2022 走看看