Codeforces Round #373 (Div. 2) B
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 toalternate. 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.
题意:给你颜色排列,和两种操作,一种是交换颜色,一种是改变颜色,问你最少的操作,可以使得颜色交替排列
解法:首先交替排列应该是rbrbrbrbrbrbr.. 或者brbrbrbrbr 那么讨论两种情况,一种是偶数位是r 一种是偶数位是b
再求出每种情况中,哪些位置是不符合的,比如rrbbbr 中r不在符合位置的有两个,b不在符合位置的也有两个,交换就是取两个的最小值,涂色是求它们的差值
两种情况再求一次最小值
#include<bits/stdc++.h>
using namespace std;
int n, a[10000];
int dp[100000][3];
set<char>q;
map<char,int>q1,q2;
char s[100010];
int main()
{
// string s;
scanf("%d",&n);
scanf("%s",s);
int sum1,sum2;
for(int i=0; i<n; i++)
{
if(i%2==0&&s[i]!='r')
{
q1[s[i]]++;
}
else if(i%2&&s[i]!='b')
{
q1[s[i]]++;
}
}
sum1=fabs(q1['r']-q1['b'])+min(q1['r'],q1['b']);;
q1.clear();
//cout<<sum1<<endl;
for(int i=0; i<n; i++)
{
if(i%2==0&&s[i]!='b')
{
q2[s[i]]++;
}
else if(i%2&&s[i]!='r')
{
q2[s[i]]++;
}
}
//int Min2=min(q2['r'],q2['b']);
sum2=fabs(q2['r']-q2['b'])+min(q2['r'],q2['b']);
q2.clear();
printf("%d\n",min(sum1,sum2));
return 0;
}
Codeforces Round #373 (Div. 2) 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)A B
Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...
- 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 ...
- Codeforces Round #373 (Div. 2)
A,B,C傻逼题,就不说了. E题: #include <iostream> #include <cstdio> #include <cstring> #inclu ...
- 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) E. Sasha and Array 线段树维护矩阵
E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...
- 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. Vitya in the Countryside 水题
A. Vitya in the Countryside 题目连接: http://codeforces.com/contest/719/problem/A Description Every summ ...
- 线段树+矩阵快速幂 Codeforces Round #373 (Div. 2) E
http://codeforces.com/contest/719/problem/E 题目大意:给你一串数组a,a[i]表示第i个斐波那契数列,有如下操作 ①对[l,r]区间+一个val ②求出[l ...
随机推荐
- nyist 596 谁是最好的Coder
http://acm.nyist.net/JudgeOnline/problem.php?pid=596 谁是最好的Coder 时间限制:1000 ms | 内存限制:65535 KB 难度:0 ...
- NVL 和NVL2函数
NVL 和NVL2函数 NVL函数: nvl(exp1,exp2) -->判断exp1是否是null,如果exp1不是则返回exp1的值,如果exp1为null则返回exp2 nvl2函数: n ...
- java读properties的通用类,兼容linux和windows
package util; import java.io.IOException; import java.io.InputStream; import java.util.Properties; / ...
- 【转】转换到 COFF 期间失败: 文件无效或损坏
不知怎么本来编译好好的VS2010环境,忽然出现“转换到 COFF 期间失败: 文件无效或损坏”的链接错误.花了好多天,试了好多方法,最终解决了这个问题. 现在罗列一下这几种解决方案: 方案1 ...
- SQL分组取每组前一(或几)条记录(排名)
mysql分组取每组前几条记录(排名) 附group by与order by的研究 http://www.jb51.net/article/31590.htm --按某一字段分组取最大(小)值所在行的 ...
- C# 时间现实问题(12小时制与24小时制)
最近在修改项目中遇到时间问题,12小时制与24小时制的问题,想再次跟各位同仁提个醒. yyyy-MM-dd HH:mm:ss------大写的HH为24小时制 yyyy-MM-dd hh:mm:ss- ...
- 作为一个web开发人员,哪些技术细节是在发布站点前你需要考虑到的
前日在cnblogs上看到一遍文章<每个程序员都必读的12篇文章>,其中大多数是E文的. 先译其中一篇web相关的”每个程序员必知之WEB开发”. 原文: http://programme ...
- 【原创】JDK动态代理,此次之后,永生难忘。
动态代理,这个词在Java的世界里面经常被提起,尤其是对于部分(这里强调“部分”二字,因为有做了一两年就成大神的,实力强的令人发指,这类人无疑是非常懂动态代理这点小伎俩的)做了一两年新人来说,总是摸不 ...
- -Xms512m -Xmx1024m -XX:PermSize=256m -XX:MaxPermSize=512m
-Xms 最小堆的大小, 也就是当你的虚拟机启动后, 就会分配这么大的堆内存给你 -Xmx 是最大堆的大小 当最小堆占满后,会尝试进行GC,如果GC之后还不能得到足够的内存(GC未必会收集到所有当前可 ...
- 将Nagios监控信息存入Mysql
一.NDOUtils安装需求: nagios:安装方法:http://www.cnblogs.com/Richardzhu/p/3340638.html mysql:源码安装方法:http://www ...