Codeforces 719B Anatoly and Cockroaches
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.
题目链接:http://codeforces.com/problemset/problem/719/B
分析:细想只有两种模式,一种brbrbr... 另一种rbrbrb... 只需要统计这两种模式下,需要的两种操作数中最小的一个,即是答案。
下面给出AC代码:
#include <bits/stdc++.h>
using namespace std;
const int MAXN = ;
char a[MAXN];
int main()
{
int n;
while(cin>>n)
{
scanf("%s",a);
int m = ;
int t=;
int u=;
int v=;
for(int i=; i<n; i++)
{
if(i%==)
{
if(a[i]=='r')
m++;
if(a[i]=='b')
t++;
}
else
{
if(a[i]=='r')
u++;
if(a[i]=='b')
v++;
}
}
int x=max(t,u);
int y=max(m,v);
int z=min(x,y);
printf("%d\n",z);
}
return ;
}
Codeforces 719B Anatoly and Cockroaches的更多相关文章
- 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开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
- 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 ...
- B. Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test 1 second memory limit per test 256 megabytes input st ...
- codeforces 719B:Anatoly and Cockroaches
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- 【31.58%】【codeforces 719B】 Anatoly and Cockroaches
time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...
- Anatoly and Cockroaches
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also li ...
随机推荐
- C#中Console.WriteLine()函数输出格式详解
格式项都采用如下形式: {index[,alignment][:formatString]} 其中"index"指索引占位符,这个肯定都知道: ",alignment&q ...
- HDU 3501 Calculation 2 ——Dirichlet积
[题目分析] 卷积太有趣了. 最终得出结论,互质数和为n*phi(n)/2即可. 计算(n*(n+1)/2-n-n*phi(n)/2)%md,用反正法即可证明. [代码] #include <c ...
- realvnc viewer 5.3.2无需输入用户名和密码访问远程桌面
我从https://www.realvnc.com/download/viewer/下载了realvnc viewer用于访问远程的Linux桌面,这个版本不需要安装,直接运行就可以了.但在访问远程桌 ...
- CSS长度单位详解
序言 长度单位可以总体的分为绝对长度单位和相对长度单位.CSS中最为大家熟知的无疑是px和em,但与此同时还存在pt, rem, vw, vh等其他计量单位,使用好它们可以大大增长我们的开发效率.本篇 ...
- 主流IOC框架测验(.NET)
上一篇中,我简单介绍了下Autofac的使用,有人希望能有个性能上的测试,考虑到有那么多的IOC框架,而主流的有:Castle Windsor.微软企业库中的Unity.Spring.NET.Stru ...
- 利用Selenium和Browsermob批量嗅探下载Bilibili网站视频
Rerence: http://www.liuhao.me/2016/09/20/selenium_browsermob_sniff_bilibili_video/ 日常生活中,用电脑看视频是非常频繁 ...
- Java数据流的一般操作规律总结
流的操作规律: 1,明确源和目的. 数据源:就是需要读取,可以使用两个体系:InputStream.Reader: 数据汇:就是需要写入,可以使用两个体系:OutputStream.Writer: 2 ...
- Delphi制作图像特殊显示效果
Delphi制作实现图像的各种显示效果,比如百叶窗.渐变.淡入淡出.水平交错.雨滴效果等,用鼠标点击“打开图像”按钮,可以选择图像文件导入到窗体中:点击其它各个按钮,可以实现图像显示特效,例如:点击“ ...
- #搜索# #BFS# #优先队列# ----- OpenJudge鸣人和佐助
OpenJudge 6044:鸣人和佐助 总时间限制: 1000ms 内存限制: 65536kB 描述 佐助被大蛇丸诱骗走了,鸣人在多少时间内能追上他呢? 已知一张地图(以二维矩阵的形式表示)以及佐 ...
- angular指令中,require和transclude同时设置为true时的作用
最近在学习angularJS指令的时候,对指令对象中require属性和transclude属性同时设置为true比较疑惑,于是自己动手测试一下具体差异 index.html: <simple& ...