codeforces 719B:Anatoly and Cockroaches
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.
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.
正解:贪心
解题报告;
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的更多相关文章
- 【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
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 ...
- 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(元素的交叉排列问题)
题目链接: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开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
- Anatoly and Cockroaches
Anatoly lives in the university dorm as many other students do. As you know, cockroaches are also li ...
随机推荐
- MongoDB学习(三)数据导入导出及备份恢复
这几天想着公司要用MongoDB,自然就要用到数据导入导出,就自己学习了一下. 在Mongo学习(二)中就讲到了在bin目录下有一些工具,本篇就是使用这些工具进行数据的导入导出及备份恢复. 注意:以下 ...
- Android Studio中提示:Project SDK is not defined
Android Studio中提示:Project SDK is not defined 2015 年 4 月 1 日 下午 9:04crifan已有2209人围观我来说几句 [背景] 之前用Andr ...
- MySql表大小、行大小和列大小的限制
参见官网文档: http://dev.mysql.com/doc/refman/5.7/en/table-size-limit.htmlhttp://dev.mysql.com/doc/refman/ ...
- opencv3中的机器学习算法之:EM算法
不同于其它的机器学习模型,EM算法是一种非监督的学习算法,它的输入数据事先不需要进行标注.相反,该算法从给定的样本集中,能计算出高斯混和参数的最大似然估计.也能得到每个样本对应的标注值,类似于kmea ...
- Android 动画之AlphaAnimation应用详解
窗口的动画效果,淡入淡出什么的,有些游戏的欢迎动画,logo的淡入淡出效果就使用AlphaAnimation.AlphaAnimation(0.01f, 1.0f); 从0.01f到1.0f渐变.学过 ...
- Gradle tip #2: understanding syntax
In the Part 1 we talked about tasks and different stages of the build lifecycle. But after I publish ...
- Java系列: 我的第一个spring aop练习
看<Spring in action>有一段时间了,陆续也都看懂了,但是看懂和自己动手写确实是两回事,今天花了几个小时陆续开始安装spring,开始使用DI,然后使用AOP,在写AOP例子 ...
- 深入探究javascript的 {} 语句块
今日学习解析json字符串,用到了一个eval()方法,解析字符串的时候为什么需要加上括号呢?摸不着头脑.原来javascript中{}语句块具有二义性,不加括号会出错,理解这种二义性对我们理解jav ...
- 自己留存:mysql full text 支持中文的一个设定
innodb_ft_min_token_size=1 ft_min_word_len=1
- sqlserver日期函数 dateadd,datediff ,datepart ,datename,convert
reference:http://www.cnblogs.com/coconut_zhang/archive/2009/02/02/1382598.html http://blog.itpub.net ...