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 ...
随机推荐
- block的内部实现原理
一.简单定义 block是一个指向结构体的指针,编译器将block内部代码生成对应的函数,上述结构体中的函数指针(funcPtr)指向该函数的实现: 二.相关概念 形参和实参 形参:形式参数,用于定义 ...
- linshi_temp_erweima_html
<!doctype html><html><head><meta charset="utf-8"><meta content= ...
- 【Python】@property的用法
设想我们要给一个student()类的一个实例s,添加一个score的属性,比如: s.score=999999 这个值明显是不合理的,但是它却是可行的,怎么能改变这种情况?我们能想到的就是用类方法 ...
- POJ 1082 Calendar Game(找规律博弈)
传送门 以下复制自此处:http://www.xuebuyuan.com/2028180.html 博弈论题目可以用寻找必败状态的方法解决. 第一个必败状态是2001.11.04.由此可以推出其他任何 ...
- JavaEE XML 基础知识
JavaEE XML 基础知识 @author ixenos 1. XML开头都需要一个声明 <?和?>表明这是一个处理指令 <?xml version=”1.0” encod ...
- Computation expressions and wrapper types
原文地址:http://fsharpforfunandprofit.com/posts/computation-expressions-wrapper-types/ 在上一篇中,我们介绍了“maybe ...
- openwrt启动过程(脚本)
来源: http://wiki.openwrt.org/doc/techref/preinit_mount#first.boot 基本的openwrt启动顺序为: 1.boot loader loa ...
- 一些css3的特效 javascript的window对象 定时器 延时器等ing...
风车转动代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> < ...
- Gentoo安装详解(四)-- 声卡设置
硬件检测 To choose the right driver, first detect the used audio controller. You can use lspci for this ...
- SharePoint 2013 InfoPath 无法保存下列表单
转载自:http://www.cnblogs.com/jianyus/p/3470121.html 在使用InfoPath发布表单,发布到SharePoint服务器报错,如下介绍: 环境:Window ...