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;
}
最新文章
- Ubuntu 16.04安装搜狗输入法
- Nodejs的模块实现
- C# 委托和Lambda---基础
- Dynamics AX 2012 R2 在AIF服务契约中使用DateTime
- IOC依赖注入简单实例
- EF LEFT JON 关联查找
- innobackupex 备份实验
- H2O与Java线程同步
- sql参数化查询避免注入漏洞的原因探析
- Java解析JSON文件的方法(一)
- Context源码分析
- IOS 看懂此文,你的block再也不需要WeakSelf弱引用了!
- 移动端 iphone锁屏文字效果
- Android进阶(二十三)Android开发过程之实例讲解
- python学习笔记(九)、模块
- mapreduce map 的个数
- npm install -g @angular/cli@latest 失败
- Java面试题归类
- P1678 烦恼的高考志愿(二分)
- 「SCOI2016」美味 解题报告