题目链接:http://codeforces.com/contest/719/problem/B

B. Anatoly and Cockroaches
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Input

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.

Output

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.

Examples
input
5
rbbrr
output
1
input
5
bbbbb
output
2
input
3
rbr
output
0
Note

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.

题解:

1.遍历一遍,找出b放错的个数和r放错的个数, min(b,r)即为交换的次数, max(b,r)-min(b,r)即为涂色的次数, 所以总的次数为max(b,r)

2.由于序列只能是rbrbrb……或者brbrbr……,所以分两次遍历, 然后取最小值, 即ans = min(max(b1,r1), max(b2,r2))

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <sstream>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define ms(a, b) memset((a), (b), sizeof(a))
#define eps 0.0000001
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 100000+10;
char s[maxn],a[maxn]; int main()
{
int n, t1,t2, ans;
scanf("%d",&n);
scanf("%s",s); t1 = t2 = 0;
for(int i = 0; i<n; i++)
{
if((i&1) && s[i]=='r')
t1++; if(!(i&1) && s[i]=='b')
t2++;
} ans = max(t1,t2);
t1 = t2 = 0;
for(int i = 0; i<n; i++)
{
if((i&1) && s[i]=='b')
t1++; if(!(i&1) && s[i]=='r')
t2++;
} ans = min( ans, max(t1,t2) );
printf("%d\n",ans);
}

Codeforces Round #373 (Div. 2) Anatoly and Cockroaches —— 贪心的更多相关文章

  1. Codeforces Round #373 (Div. 2)A B

    Codeforces Round #373 (Div. 2) A. Vitya in the Countryside 这回做的好差啊,a想不到被hack的数据,b又没有想到正确的思维 = = [题目链 ...

  2. Codeforces Round #373 (Div. 1)

    Codeforces Round #373 (Div. 1) A. Efim and Strange Grade 题意 给一个长为\(n(n \le 2 \times 10^5)\)的小数,每次可以选 ...

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

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

  4. Codeforces Round #373 (Div. 2) A B C 水 贪心 模拟(四舍五入进位)

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  5. Codeforces Round #373 (Div. 2) A , B , C

    A. Vitya in the Countryside time limit per test 1 second memory limit per test 256 megabytes input s ...

  6. 【Codeforces】Codeforces Round #373 (Div. 2)

    B. Anatoly and Cockroaches Anatoly lives in the university dorm as many other students do. As you kn ...

  7. Codeforces Round #373 (Div. 2) B

    Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...

  8. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade 水题

    C. Efim and Strange Grade 题目连接: http://codeforces.com/contest/719/problem/C Description Efim just re ...

  9. Codeforces Round #373 (Div. 2) C. Efim and Strange Grade —— 贪心 + 字符串处理

    题目链接:http://codeforces.com/problemset/problem/719/C C. Efim and Strange Grade time limit per test 1 ...

随机推荐

  1. T1155 金明的预算方案 codevs

    累~~~  http://codevs.cn/problem/1155/ 题目描述 Description 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高 ...

  2. soursTree新建过程.md

    网上博客 https://www.cnblogs.com/tian-xie/p/6264104.html 主要的推送流程 完成所有项目的远程推送工作 点击git工作流选择第二个建立新的版本; 输入发布 ...

  3. SharpSSH

    SharpSSH sharpssh is a pure .NET implementation of the SSH2 client protocol suite. It provides an AP ...

  4. ※版本管理※=>☆SVN工具=>※解决地域麻烦※№→搭建自己的网络SVN (SourceForge 免费) [转]

    源文 http://blog.csdn.net/xiaoting451292510/article/details/8562570 分类: 版本管理 2013-02-01 14:44 26057人阅读 ...

  5. Git 的使用Git Bash和Git GUI

    使用Github也有一年的时间了,之前一直都是使用的Github客户端,对提交,更新,克隆,合并,分支有一定的了解和实践.一直都想试试命令行的形式,但是感觉可能桌面版的方便就没有做. 可是Github ...

  6. 转: 如何选CDN:互联网大直播时代的CDN选择指南

    from:  http://www.chnvideo.com/blog-classic-cdn.html SRS 编码器   如何选CDN:互联网大直播时代的CDN选择指南 CDN是一个服务型的公司, ...

  7. vue native

    1.示例 <el-icon icon="Setting" size="sm" @click.native="ceshi">< ...

  8. 【LeetCode】Word Break 解题报告

    Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...

  9. 2016年第七届蓝桥杯c/c++省赛B组

    2016年第七届蓝桥杯c/c++省赛B组 声明:以下答案是我自己做的.不能保证正确,须要參考正确答案的请到其它地方找. 第一题 :煤球数目 题目叙述: 有一堆煤球,堆成三角棱锥形.详细: 第一层放1个 ...

  10. 安装openSUSE网卡驱动

    网卡:英特尔 82579LM Gigabit Network Connection 先去英特尔的网站下载驱动 http://downloadcenter.intel.com/SearchResult. ...