题目大意:有一排蟑螂,只有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】插入排序

    今晚更新几个排序算法 插入排序,时间复杂度O(n^2),从前往后遍历,每遍历到一个值的时候,其前面的所有值已经完成排序,把这个值插入适当位置 void intersort(int *nums,int ...

  2. Storm常见问题处理

    错误1:发布topologies到远程集群时,出现Nimbus host is not set异常.异常内容如下所示: [root@xop-dev-a bin]# ./storm jar /home/ ...

  3. java.net.NoRouteToHostException: No route to host

    报错信息: java.net.NoRouteToHostException: No route to host        at java.net.PlainSocketImpl.socketCon ...

  4. 垂直分割群集模型与多通道引擎 -- ESFramework 4.0 进阶(10)

    在ESFramework 4.0 进阶(09)-- ESPlatform 支持的三种群集模型一文中,我们介绍了ESPlatform支持的三种群集模型 -- 垂直分割模型.水平分割模型.交叉模型.我们看 ...

  5. LeetCode OJ 85. Maximal Rectangle

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  6. apicloud

    <!doctype html> <html class="no-js"> <head> <meta charset="utf-8 ...

  7. SharePoint 2013 InfoPath 无法保存下列表单

    转载自:http://www.cnblogs.com/jianyus/p/3470121.html 在使用InfoPath发布表单,发布到SharePoint服务器报错,如下介绍: 环境:Window ...

  8. 3.编写Java应用程序。首先定义一个描述银行账户的Account类,包括成员变 量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次, 编写一个主类,在主类中测试Account类的功能。

    Account package com.hanqi.test; public class Account { private String zhanghao;private double yve; A ...

  9. Mybatis批量更新数据

    转载:http://blog.csdn.net/tolcf/article/details/39213217 第一种方式 <update id="updateBatch" p ...

  10. js 禁止重复提交

    /*分享按钮*/ var flag = true; $(".recommed a").click(function() { if (flag == true) { getServe ...