CodeForces 719B Anatoly and Cockroaches 思维锻炼题
题目大意:有一排蟑螂,只有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 思维锻炼题的更多相关文章
- Codeforces 719B Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test:1 second memory limit per test:256 megabytes input:st ...
- CodeForces 719B Anatoly and Cockroaches (水题贪心)
题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下 ...
- Codeforces 719B Anatoly and Cockroaches(元素的交叉排列问题)
题目链接:http://codeforces.com/problemset/problem/719/B 题目大意: 有一队蟑螂用字符串表示,有黑色 ‘b’ 和红色 'r' 两种颜色,你想使这队蟑螂颜色 ...
- 【CodeForces - 707B】Bakery(思维水题)
Bakery Descriptions 玛莎想在从1到n的n个城市中开一家自己的面包店,在其中一个城市烘焙松饼. 为了在她的面包房烘焙松饼,玛莎需要从一些储存的地方建立面粉供应.只有k个仓库,位于不同 ...
- A. Yellow Cards ( Codeforces Round #585 (Div. 2) 思维水题
---恢复内容开始--- output standard output The final match of the Berland Football Cup has been held recent ...
- 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) Anatoly and Cockroaches —— 贪心
题目链接:http://codeforces.com/contest/719/problem/B B. Anatoly and Cockroaches time limit per test 1 se ...
- B. Anatoly and Cockroaches
B. Anatoly and Cockroaches time limit per test 1 second memory limit per test 256 megabytes input st ...
- C Alyona and Spreadsheet Codeforces Round #401(Div. 2)(思维)
Alyona and Spreadsheet 这就是一道思维的题,谈不上算法什么的,但我当时就是不会,直到别人告诉了我,我才懂了的.唉 为什么总是这么弱呢? [题目链接]Alyona and Spre ...
随机推荐
- Linux入门(六)ubuntu下vim编辑器安装与使用
原文链接:http://jingyan.baidu.com/article/219f4bf7d03187de452d3862.html 一.安装 vim 控制台版本的vim随ubuntu一起发行,因此 ...
- java 邮件发送 apache commons-email
package com.sun.mail;import org.apache.commons.mail.Email;import org.apache.commons.mail.EmailExcept ...
- ggplot2 theme相关设置—文本调整
在geom设置和scale设置之后,要想把图画的漂亮,theme设置是比不可少的 在theme 设置中element_text()是一项很重要的内容 element_text(family = NUL ...
- hdu和poj的基础dp30道
题目转自:https://crazyac.wordpress.com/dp%E4%B8%93%E8%BE%91/ 1.hdu 1864 最大报销额 唔,用网上的算法连自己的数据都没过,hdu的数据居然 ...
- 基于C++的顺序表的实现
顺序表,是数据结构中按顺序方式存储的线性表,又称向量.具有方便检索的特点.以下,是笔者学习是基于C++实现的顺序表代码,贴上来当网页笔记用. #include <iostream> usi ...
- Shell grep 和正则
在grep中正则有三种模式, 分别是基础(-G), 扩展(-E)和Perl(-P)模式 basic 模式 不支持\d, 支持\w \s \b. ?, +, {, |, (, and ) 在basic ...
- unity 退到桌面的 OnApplicationPause
void OnApplicationFocus( bool isFocus ) { // Debug.Log("--------OnApplicationPause---" + i ...
- 让人头疼的CSS兼容
先说点Hack的知识(真正的高手是不用Hack的,但要成为高手必须通过Hack这一关) /* CSS属性级Hack */ color:red; /* 所有浏览器可识别*/ _color:red; /* ...
- User already has more than 'max_user_connections' active connections
vi /etc/my.cnf修改 max_user_connections 参数的数值,重启 MySQL 服务器
- sysadmin_basement
基本部分 1. 系统硬件 1.1. 主板 获取序列号 $ sudo dmidecode -t system |grep -i Serial Serial Number: CNU9032T3N 获取厂商 ...