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 ...
随机推荐
- csuoj 1395: Timebomb
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1395 1395: Timebomb Time Limit: 1 Sec Memory Limit ...
- C#:IO
1. File和Directory类 这两个类属于静态类,经常用到的比如File.Exists(string path), Directory.Exists(string path), Directo ...
- Andriod环境搭建
安卓是一款现在在移动端十分流行的系统,本人出于好奇心,希望彻底了解安卓的开发技. 首先了解一下安卓的系统构架,安卓大致分为四层架构,五块区域: 1.Linux内核层 Andriod是基于Linux2. ...
- CCF真题之节日
201503-3 问题描述 有一类节日的日期并不是固定的,而是以“a月的第b个星期c”的形式定下来的,比如说母亲节就定为每年的五月的第二个星期日. 现在,给你a,b,c和y1, y2(1850 ≤ y ...
- 夺命雷公狗---DEDECMS----7dedecms目录结构
我们dedecms的目录结构其实只需要一张图即可明了了,如下图所示:
- zw版【转发·台湾nvp系列Delphi例程】HALCON Histogram
zw版[转发·台湾nvp系列Delphi例程]HALCON Histogram unit Unit1;interfaceuses Windows, Messages, SysUtils, Varian ...
- [tp3.2.1]大D构建模型
使用大(写字母)D方法: 如果,在默认到Home模块下面找不到UserModel模块,那么就会到Common模块下去找. 而如果此时在Common模块下还是找不到UserModel,那就会调用Mode ...
- an'gularjs 环境搭建之NodeJS、NPM安装配置步骤(windows版本)
NodeJS.NPM安装配置步骤(windows版本) :http://xiaoyaojones.blog.163.com/blog/static/28370125201351501113581/ ...
- 视频处理控件TVideoGrabber视频捕捉设设备相关问题
选择一个视频捕捉设备 首先设置 VideoSource = vs_VideoCaptureDevice来选择一个视频捕捉设备作为一个视频源. 通过指定VideoDevice属性来选择当前的视频捕捉设备 ...
- overflow的劲爆知识点
1.属性 visible(默认) hidden(此处是隐藏不是裁剪) scroll(滚动条) auto(智能路线 当超出范围时则出现滚动条) inherit 不常用 存在兼容性问题 2.进入CSS3 ...