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

正解:贪心

解题报告;

  Xlight他们都看成了只能交换相邻的,调了好久,论不看题的危害。

  考虑最终序列只有可能有2种情况,那么分别枚举,两个答案取一个min即可。

  考虑直接贪心,首先我们可以统计出有多少个错位的元素,最后直接把能交换的全部交换,否则就暴力染色就可以了。

 //It is made by jump~
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <ctime>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
typedef long long LL;
const int inf = (<<);
const int MAXN = ;
int n,a[MAXN];
int cnt[];
int ans,ans2; inline int getint()
{
int w=,q=; char c=getchar();
while((c<'' || c>'') && c!='-') c=getchar(); if(c=='-') q=,c=getchar();
while (c>='' && c<='') w=w*+c-'', c=getchar(); return q ? -w : w;
} inline void work(){
n=getint(); char c;
for(int i=;i<=n;i++) {
c=getchar(); while(c!='r' && c!='b') c=getchar();
if(c=='b') a[i]=; else a[i]=;
}
int tag=; int minl;
for(int i=;i<=n;i++) {
if(tag!=a[i]){
cnt[tag]++;
if(cnt[tag^]>) {
minl=min(cnt[tag^],cnt[tag]);
cnt[tag]-=minl; cnt[tag^]-=minl;
ans+=minl;
}
}
tag^=;
}
minl=min(cnt[],cnt[]); ans+=minl; cnt[]-=minl; cnt[]-=minl;
ans+=cnt[]; ans+=cnt[]; tag=; cnt[]=cnt[]=;
for(int i=;i<=n;i++) {
if(tag!=a[i]){
cnt[tag]++;
if(cnt[tag^]>) {
minl=min(cnt[tag^],cnt[tag]);
cnt[tag]-=minl; cnt[tag^]-=minl;
ans2+=minl;
}
}
tag^=;
}
minl=min(cnt[],cnt[]); ans2+=minl; cnt[]-=minl; cnt[]-=minl;
ans2+=cnt[]; ans2+=cnt[]; printf("%d",min(ans,ans2));
} int main()
{
work();
return ;
}

codeforces 719B:Anatoly and Cockroaches的更多相关文章

  1. 【31.58%】【codeforces 719B】 Anatoly and Cockroaches

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...

  2. Codeforces 719B Anatoly and Cockroaches

    B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:st ...

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

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

  4. Codeforces Round #373 (Div. 2) Anatoly and Cockroaches —— 贪心

    题目链接:http://codeforces.com/contest/719/problem/B B. Anatoly and Cockroaches time limit per test 1 se ...

  5. B. Anatoly and Cockroaches

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

  6. Codeforces 719B Anatoly and Cockroaches(元素的交叉排列问题)

    题目链接:http://codeforces.com/problemset/problem/719/B 题目大意: 有一队蟑螂用字符串表示,有黑色 ‘b’ 和红色 'r' 两种颜色,你想使这队蟑螂颜色 ...

  7. CodeForces 719B Anatoly and Cockroaches 思维锻炼题

    题目大意:有一排蟑螂,只有r和b两种颜色,你可以交换任意两只蟑螂的位置,或涂改一个蟑螂的颜色,使其变成r和b交互排列的形式.问做少的操作次数. 题目思路:更改后的队列只有两种形式:长度为n以r开头:长 ...

  8. CodeForces 719B Anatoly and Cockroaches (水题贪心)

    题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...

  9. Anatoly and Cockroaches

    Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also li ...

随机推荐

  1. Java虚拟机工作原理详解 ( 二 )

    首先这里澄清两个概念:JVM实例和JVM执行引擎实例,JVM实例对应了一个独立运行的Java程序,而JVM执行引擎实例则对应了属于用户运行程序的线程:也就是JVM实例是进程级别,而执行引擎是线程级别的 ...

  2. 发发关于JavaScript的感慨,随手记几个js知识碎片

    最近一段时间写了很多JavaScript和jquery代码,越来越感觉js基础不牢固,写一句查半天,有时间肯定要系统的学一下. 不说了,先记一下最近学到的点东西,省的以后没时间系统学js还要再来查. ...

  3. TortoiseGIT安装过程及问题

    1,安装Git.一路默认 2,安装TortoiseGit,一路安装完毕.重启电脑. 3,配置TortoiseGit.这是最重要一步. 配置 remote,即远程地址,用于以后pull 或 push操作 ...

  4. php基础28:文件读取

    <?php //1.追加文件内容 // $fp = fopen("test.txt", "a"); // $outString = "this ...

  5. PowerDesigner16建表在SQL SERVER 2008报 对象名 'sysproperties' 无效。

    http://blog.itpub.net/30150152/viewspace-1454979/

  6. Linux 删除文件夹和文件命令

    inux删除目录很简单,很多人还是习惯用rmdir,不过一旦目录非空,就陷入深深的苦恼之中,现在使用rm -rf命令即可.直接rm就可以了,不过要加两个参数-rf 即:rm -rf 目录名字-r 就是 ...

  7. [vim配置]windows下在vim中使用gcc/g++编译调试c/cpp文件

    在Linux里面混了一个多月,vim编程用得甚爽.无奈前天将Linux里面的编程文件夹误删,而技术不精无法找回,悲痛欲绝.再者,无限怀念windows里面的游戏,并觉得现在在Linux里面也学不到什么 ...

  8. WPF打印原理,自定义打印

    一.基础知识 1.System.Printing命名空间 我们可以先看一下System.Printing命名空间,东西其实很多,功能也非常强大,可以说能够控制打印的每一个细节,曾经对PrintDial ...

  9. 接收content-type:multipart/form-data类型的参数

    一.问题描述 最近在写接口程序,该接口需要与其他公司的程序对接.对方发送content-type:multipart/form-data类型的参数,结果通过request.getParameter(p ...

  10. 点击事件touches与ios的手势UIGestureRecognizer

    .h文件 @property (weak,nonatomic) IBOutlet UILabel *messageLabel;@property (weak,nonatomic) IBOutlet U ...