题目大意:有一排蟑螂,只有r和b两种颜色,你可以交换任意两只蟑螂的位置,或涂改一个蟑螂的颜色,使其变成r和b交互排列的形式。问做少的操作次数。

题目思路:更改后的队列只有两种形式:长度为n以r开头;长度为n以b开头。与初始串进行比较并统计改变次数记作ans,算出必须进行的涂色操作的次数step,我们可以把交换两只不同颜色的蟑螂的位置看做进行了两次涂改操作,其次数为(ans-step)。

那么得出改变为模板串的最小操作次数为:step+(ans-step)/2;

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include<math.h>
#include<map>
#define INF 0x3f3f3f3f
#define MAX 1000005
#define Temp 1000000000 using namespace std; char str[MAX],t[MAX]; int change(int n,int k)
{
int op=k,rsum=,bsum=,r,b,ans=;
for(int i=;i<n;i++)//获取模板串
{
if(op==)
t[i]='r';
else
t[i]='b';
op*=-;
}
for(int i=;i<n;i++)//统计初始串r,b的数目,和模板串相较于初始串的改变次数
{
if(str[i]=='r')
rsum++;
else
bsum++;
if(str[i]!=t[i])
ans++;
}
if(k==)
{
if(n%==)
{
r=n/;
b=n/;
}
else
{
r=n/+;
b=n/;
}
}
else if(k==-)
{
if(n%==)
{
r=n/;
b=n/;
}
else
{
r=n/;
b=n/+;
}
}
int step=abs(r-rsum);//必须的涂色操作的次数
ans=step+(ans-step)/;
return ans;
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
scanf("%s",str);
int k1=change(n,);
int k2=change(n,-);
int ans=min(k1,k2);
printf("%d\n",ans);
}
return ;
}

CodeForces 719B Anatoly and Cockroaches 思维锻炼题的更多相关文章

  1. Codeforces 719B Anatoly and Cockroaches

    B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:st ...

  2. CodeForces 719B Anatoly and Cockroaches (水题贪心)

    题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...

  3. Codeforces 719B Anatoly and Cockroaches(元素的交叉排列问题)

    题目链接:http://codeforces.com/problemset/problem/719/B 题目大意: 有一队蟑螂用字符串表示,有黑色 ‘b’ 和红色 'r' 两种颜色,你想使这队蟑螂颜色 ...

  4. 【CodeForces - 707B】Bakery(思维水题)

    Bakery Descriptions 玛莎想在从1到n的n个城市中开一家自己的面包店,在其中一个城市烘焙松饼. 为了在她的面包房烘焙松饼,玛莎需要从一些储存的地方建立面粉供应.只有k个仓库,位于不同 ...

  5. A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题

    ---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...

  6. Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题

    B. Anatoly and Cockroaches 题目连接: http://codeforces.com/contest/719/problem/B Description Anatoly liv ...

  7. 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 ...

  8. B. Anatoly and Cockroaches

    B. Anatoly and Cockroaches time limit per test 1 second memory limit per test 256 megabytes input st ...

  9. C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)

    Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...

随机推荐

  1. 【Sort】多种排序

    这篇文章包含了插入排序,希尔排序,堆排序,归并排序和快速排序,是前几篇文章的集合. 一共包括三个文件 sort.h sort.cpp main.cpp 1.main.cpp #include < ...

  2. infix to postfix 完整版

    #include<iostream> #include<stack> #include<string> #include<deque> using na ...

  3. Struts文件上传

    首先要加入上传文件需要的jar文件 commons-fileupload-1.2.1.jar commomons-io-1.3.2.jar不同版本的strutsjar文件的版本也可能不同,一般在配置s ...

  4. Zeppelin使用spark解释器

    Zeppelin为0.5.6 Zeppelin默认自带本地spark,可以不依赖任何集群,下载bin包,解压安装就可以使用. 使用其他的spark集群在yarn模式下. 配置: vi zeppelin ...

  5. HDU 1258 Sum It Up(DFS)

    题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...

  6. [ An Ac a Day ^_^ ] HDU 1257 基础dp 最长上升子序列

    最近两天在迎新 看来只能接着水题了…… 新生培训的任务分配 作为一个有担当的学长 自觉去选了动态规划…… 然后我觉得我可以开始水动态规划了…… 今天水一发最长上升子序列…… kuangbin有nlog ...

  7. 洛谷-语文成绩-[有奖]洛谷5月月赛:kkksc03的三大神器

    题目背景 Background语文考试结束了,成绩还是一如既往地有问题. 题目描述 Description语文老师总是写错成绩,所以当她修改成绩的时候,总是累得不行.她总是要一遍遍地给某些同学增加分数 ...

  8. 批量去除office超链接

    mac下: fn+shift+comman+F9 windows下:control+shift+F9

  9. android activity中监听View测量完成的4种方式

    在开发中经常碰到需要在activity初始化完成后获得控件大小的情况. 但是这个操作我们不能在oncreate.onresume等生命周期方法中调用,因为我们不知道何时view才能初始化完成 为此,特 ...

  10. JavaScript 构造函数 prototype属性和_proto_和原型链 constructor属性 apply(),call()和bind() 关键字this

    1.构造函数: 通常构造函数首字母需要大写,主要是为了区别ECMAScript的其它函数.(高程三 P145) 构造函数与其他函数的唯一区别,就在于调用它们的方式不同.只要通过new来调用,任何函数都 ...