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的更多相关文章

  1. Codeforces Round #373 (Div. 1)

    Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...

  2. Codeforces Round #373 (Div. 2)A B

    Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...

  3. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题

    C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...

  4. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade —— 贪心 + 字符串处理

    题目链接:http://codeforces.com/problemset/problem/719/C C. Efim and Strange Grade time limit per test 1 ...

  5. Codeforces Round #373 (Div. 2)

    A,B,C傻逼题,就不说了. E题: #include <iostream> #include <cstdio> #include <cstring> #inclu ...

  6. Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  8. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

  9. Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 水题

    A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...

  10. 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E

    http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...

随机推荐

  1. CSS自定义弹出框

    <script type="text/javascript" language="javascript"> function sAlert(str) ...

  2. How to export a template in Visual Studio?

    Create a customize template file: 1.template arguments introduction like: 上图只是其中一部分,更多请查看文后的参考资源 tem ...

  3. Java基础(4):Scanner输入的典型应用

    import java.util.Scanner; /* * 功能:为指定的成绩加分,直到分数大于等于60为止 * 输出加分前的成绩和加分后的成绩,并且统计加分的次数 * 步骤: * 1.定义一个变量 ...

  4. 侧菜单栏的实现SlidingPaneLayout

    SlidingPaneLayout分为两部分,上面的 左划出部分和没划出的时候 <?xml version="1.0" encoding="utf-8"? ...

  5. 自定义FragmentTabHost--实现View重复加载问题

    1,接着上篇的Fragment+FragmentTabHost搭建简单的底部功能切换框架,效果如下: 结果在项目中用到的时候发现Fragment+FragmentTabHost实现的时候每一次切换底部 ...

  6. GitHub和SourceTree入门教程——(转载),希望能帮到有需要的人

    -->本教程适用于主流的开源网站github和bitbucket,个人认为sourceTree还是比较好用的git客户端,支持windows和mac os. -->soureceTree的 ...

  7. CSS3 Transform Matrix

    css3中的transform让我们操作变形变得很简单,诸如,translate–移动,scale–缩放,rotate–旋转,skew–斜切.这几个属性很方便,也很简单,但是其中matrix我们就不常 ...

  8. Java中的数组操作进阶

    package com.mi.array; import java.util.Arrays; /** * System.arraycopy(i, 0, j, 0, i.length);这种复制会覆盖目 ...

  9. Ubuntu + CentOS7 搭建tftp Server

    基于Ubuntu系统做的tftp服务器,基于CentOS 7都差不多,书写了关键命令,测试过Ubuntu 12.0.4 和CentOS 7环境 1.介绍tftp服务器     TFTP(Trivial ...

  10. unix

    BSD (Berkeley Software Distribution,伯克利软件套件)是Unix的衍生系统,在1977至1995年间由加州大学伯克利分校开发和发布的.历史上, BSD曾经被认为是UN ...