Codeforces Round #373 (Div. 2) Anatoly and Cockroaches —— 贪心
题目链接:http://codeforces.com/contest/719/problem/B
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.
题解:
1.遍历一遍,找出b放错的个数和r放错的个数, min(b,r)即为交换的次数, max(b,r)-min(b,r)即为涂色的次数, 所以总的次数为max(b,r)
2.由于序列只能是rbrbrb……或者brbrbr……,所以分两次遍历, 然后取最小值, 即ans = min(max(b1,r1), max(b2,r2))
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 100000+10;
char s[maxn],a[maxn]; int main()
{
int n, t1,t2, ans;
scanf("%d",&n);
scanf("%s",s); t1 = t2 = 0;
for(int i = 0; i<n; i++)
{
if((i&1) && s[i]=='r')
t1++; if(!(i&1) && s[i]=='b')
t2++;
} ans = max(t1,t2);
t1 = t2 = 0;
for(int i = 0; i<n; i++)
{
if((i&1) && s[i]=='b')
t1++; if(!(i&1) && s[i]=='r')
t2++;
} ans = min( ans, max(t1,t2) );
printf("%d\n",ans);
}
Codeforces Round #373 (Div. 2) Anatoly and Cockroaches —— 贪心的更多相关文章
- Codeforces Round #373 (Div. 2)A B
Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...
- Codeforces Round #373 (Div. 1)
Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...
- 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) A B C 水 贪心 模拟(四舍五入进位)
A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...
- 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 ...
- 【Codeforces】Codeforces Round #373 (Div. 2)
B. Anatoly and Cockroaches Anatoly lives in the university dorm as many other students do. As you kn ...
- Codeforces Round #373 (Div. 2) B
Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...
- 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 ...
- 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 ...
随机推荐
- jvm 问题分析
jmap dump:file=[文件名].dump [进程号] 生成dump root@VM-185-251-ubuntu:/opt/scripts# jmap -dump:file=three.d ...
- Spring Boot中使用AOP记录请求日志
这周看别人写的springboot后端代码中有使用AOP记录请求日志,以前没接触过,因此学习下. 一.AOP简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编 ...
- Maven的构建配置文件(Build Profiles)
在命令行使用构建配置文件时,是-P,比如:mvn -Pinput 注意:这里的构建配置文件并不是一个真正的文件,而是通过指定参数来做特定的事. 以下内容引用自https://ayayui.gitboo ...
- es6系列-变量的解构赋值
git地址: https://github.com/rainnaZR/es6-study/tree/master/src/destructuring 变量的解构赋值 变量的解构赋值: 数组, 对象, ...
- Redis Sentinel 情况下bind地址设置
Redis Sentinel 情况下bind地址设置 1个master,2个slave,3个sentinel的情况下,注意bind地址的时候不要写0.0.0.0,会导致绑定多个地址, 然后sentin ...
- [c#菜鸟]lambda表达式
what 一.定义 Lambda 表达式是一种可用于创建 委托 或 表达式目录树 类型的 匿名函数 .通过使用 lambda 表达式,可以写入可作为参数传递或作为函数调用值返回的本地函数.(微软) 理 ...
- 以面试官的角度看strcpy函数
一:笔试或者面试的总结 之 一 (1)在笔试或者面试中常常会被问道,strcpy memmove memcpy 函数的实现.有时也会问你STL 中string的 split 和 trim的实现.有的 ...
- 网站的根目录下有一个文件robots.txt ,它是啥?
我相信很多人有过这个疑问,这个robots文件是干嘛的? 我想问,各位搜索淘宝时,是否发现(禁止爬虫抓取提供快页) 关于详细语法,请看:http://zhidao.baidu.com/question ...
- vue2.0 + vux 项目搭建
1.快速搭建项目模板 因为项目使用vux,所以推荐使用vux官网的airyland/vux2 模板,vue-cli工具是vue项目的搭建脚手架 默认为 webpack2 模板,如果你需要使用webpa ...
- Linq实现SQL in
比如 Id in (1,2,3) int[] a={1,2,3}; list.Where(x=>a.Contains(x.Id))