B. Anatoly and Cockroaches
1 second
256 megabytes
standard input
standard output
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.
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.
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.
5
rbbrr
1
5
bbbbb
2
3
rbr
0
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.
这么道水题。。。我居然花了半个小时。。。果然我还是太弱了
#include<iostream>
#include<cstring>
using namespace std; int n,cntr=,cntb=,ans=0x7fffffff;
char s[]; int main()
{
cin>>n>>s;
for(int i=;i<n;i++)
{
if(s[i]=='r'&&i&) cntr++;
if(s[i]=='b'&&!(i&)) cntb++;
}
ans=min(ans,max(cntr,cntb));
cntr=cntb=;
for(int i=;i<n;i++)
{
if(s[i]=='b'&&i&) cntr++;
if(s[i]=='r'&&!(i&)) cntb++;
}
ans=min(ans,max(cntr,cntb));
cout<<ans<<endl;
return ;
}
B. Anatoly and Cockroaches的更多相关文章
- Codeforces 719B Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:st ...
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...
- 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 ...
- codeforces 719B:Anatoly and Cockroaches
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- Anatoly and Cockroaches
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also li ...
- 【31.58%】【codeforces 719B】 Anatoly and Cockroaches
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Codeforces 719B Anatoly and Cockroaches(元素的交叉排列问题)
题目链接:http://codeforces.com/problemset/problem/719/B 题目大意: 有一队蟑螂用字符串表示,有黑色 ‘b’ 和红色 'r' 两种颜色,你想使这队蟑螂颜色 ...
- CodeForces 719B Anatoly and Cockroaches 思维锻炼题
题目大意:有一排蟑螂,只有r和b两种颜色,你可以交换任意两只蟑螂的位置,或涂改一个蟑螂的颜色,使其变成r和b交互排列的形式.问做少的操作次数. 题目思路:更改后的队列只有两种形式:长度为n以r开头:长 ...
- CodeForces 719B Anatoly and Cockroaches (水题贪心)
题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
随机推荐
- 基于IDEA实现SSM整合框架的搭建配置流程
1.创建数据库表,以员工信息表为例子: DROP TABLE IF EXISTS `em_info`; CREATE TABLE `em_info` ( `em_id` INT(50) NOT NUL ...
- js异步加载和按需加载
function loadScript(url,callback){ var script = document.creatElement("script"); script.ty ...
- [Android]Android性能优化
安卓性能优化 性能优化的几大考虑 Mobile Context 资源受限 + 内存,普遍较小,512MB很常见,开发者的机器一般比用户的机器高端 + CPU,核心少,运算能力没有全开 + GPU,上传 ...
- 049 Group Anagrams 字谜分组
给定一个字符串数组,将相同字谜组合在一起.(字谜是指颠倒字母顺序而成的字)例如,给定 ["eat", "tea", "tan", " ...
- SPI接口的ETH芯片(ENC28J60/W5500)
一 ENC28J60:SPI接口.中断.复位.LED指示.可参看野火相应教程.简单的在单片机中实现网页服务器是参考AVRNET项目,复杂的是用LWIP协议栈.telnet用于用PC的TELNET可以远 ...
- SQL server 游标,随机数使用
SELECT * FROM [goods] DECLARE test_cursor CURSOR scroll FOR SELECT goods_no, [unitprice] FROM [goods ...
- 牛客网Java刷题知识点之面向对象java的四大特性(抽象、封装、继承、多态)
不多说,直接上干货! 面向对象java的四大特性之抽象 抽象就是有点模糊的意思,还没确定好的意思. 就比如,要定义一个方法和类.但还没确定怎么去实现它的具体一点的子方法,那我就可以用抽象类或接口.具体 ...
- 算法导论课后习题解答 第一部分 练习1.1-1->1.1-5
很高兴能和大家一起共同学习算法导论这本书.笔者将在业余时间把算法导论后面的题解以博文的形式展现出来希望能得到大家的支持谢谢.如果有可能我会做一些教学视频免费的供大家观看. 练习题选自算法导论中文第三版 ...
- kindeditor编辑器和图片上传独立分开的配置细节
关于kindeditor编辑器上传按钮的异步加载最关键的部署问题,它的上传图片的组件都已经封装得很好了的,只需要监听到页面按钮的点击事件给编辑器对象传递一些对应的初始化参数即可显示图片上传的弹窗实现异 ...
- c#线程倒计时器源码
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...