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. Linux系统批量化安装部署之Cobbler

    说明: Cobbler服务器系统:CentOS 5.10 64位 IP地址:192.168.21.128 需要安装部署的Linux系统: eth0(第一块网卡,用于外网)IP地址段:192.168.2 ...

  2. 1140 Jam的计数法

    1140 Jam的计数法 2006年NOIP全国联赛普及组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descri ...

  3. user database的initial size和dbcc shrinkfile

    之前我们讨论了dbcc shrinkfile改变tempdb initial size的情况.而用DBCC Shrinkfile去收缩一个user database,情况就比较简单了.让我们通过一些测 ...

  4. 入门Linux

    45分钟带你入门Linux(附:笔者在工作室开讨论班录制的视频讲解)   第一部分    熟悉Linux基本操作 一.初识Linux 1.Linux特点 ◊  开放性 ◊  多用户 ◊  多任务 ◊  ...

  5. [QoS]cisco3560限速配置案例-收集于网工泡泡

    网络中常用到这些:CISCO和H3C-MAC过滤+端口限速+端口镜像+端口隔离 不同的方式不同的思想:嘎嘎 其他各个厂商的限速链接:http://pan.baidu.com/s/1hrIMoSG 密码 ...

  6. [转]World Wind Java开发之四——搭建本地WMS服务器

    在提供地理信息系统客户端时,NASA还为用户提供了开源的WMS Server 服务器应用:World Wind WMS Server.利用这个应用,我们可以架设自己的WMS服务并使用自己的数据(也支持 ...

  7. 浅谈设计模式--单例模式(Singleton Pattern)

    题外话:好久没写blog,做知识归纳整理了.本来设计模式就是个坑,各种文章也写烂了.不过,不是自己写的东西,缺少点知识的存在感.目前还没做到光看即能记住,得写.所以准备跳入设计模式这个大坑. 开篇先贡 ...

  8. java系列:《java核心技术 卷1》学习笔记,chapter 11 调试技巧

    11. 6 调试技巧 1)一个不太为人所知却非常有效的技巧是在每个类中放一个main方法,这样就可以对每个类进行单元测试.这个方法可以保留,因为在java虚拟机只调用启动类的main方法. 2)   ...

  9. 从0开始学java——JUnit4 复习,其实基本思想还是那些,不过采用了新的注释格式的语法

    看了深入探索 JUnit 4(http://www.ibm.com/developerworks/cn/education/java/j-junit4/index.html  ) 主要是讲了新的基于注 ...

  10. 倒戈了,转投简书 -------->

    深情自白 还记得数月前那个月黑风高的晚上,笔主偶遇简书,被那婀娜多姿的Markdown输出深深吸引不能自拔,从此立下毒誓要两边同时发布.然而天有不测风云(这边的太丑),前思后想寝食难安之后作出决定,正 ...