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

【题解】

这不是道广搜题0 0

可以很容易想到目标序列有两种

1和0分别代表两种不同颜色;

1.以1开头

2.以0开头

构造出这样的目标序列。

然后把所给的初始序列和目标序列对比。

一出现某个位置x不一样就先尝试把

初始序列的x位置上的东西换成目标序列的颜色colorx。

但是换的条件是。换之后。要确定colorx是小于等于目标序列的colorx的颜色个数。

否则就不换.

最后我们可以保证改变之后的序列的两种颜色的数目和目标序列的两种颜色的数目是一样的。

然后如果再有不同就进行交换操作(肯定比单个改变优);

110

101

可以以这个例子看到。进行一次交换必有两个位置都配对了。

可以想象。最后两种颜色都与目标序列的两种颜色数目相同了。

则肯定交换swap操作所需要的数目更少。

就是这样吧。

自己看代码;

【代码】

#include <cstdio>
#include <algorithm>
#include <bitset> using namespace std; const int MAXN = 109000; char s[MAXN];
int len;
long long final_ans = -1;
bitset <MAXN> a, temp, b; void input_data()
{
scanf("%d", &len);
scanf("%s", s);
for (int i = 1; i <= len; i++)
if (s[i - 1] == 'b')
a[i] = 0;
else
a[i] = 1;
} void get_ans(bool chushi)
{
long long temp_ans = 0;
temp = a;
b[1] = chushi;
for (int i = 2; i <= len; i++)
b[i] = !b[i - 1];
int c1[2], c2[2], tc1[2];
c1[1] = temp.count();
c2[1] = b.count();
c1[0] = len - c1[1];
c2[0] = len - c2[1];
for (int i = 1; i <= len; i++)
if (temp[i] != b[i])
{
for (int i = 0; i <= 1; i++)
tc1[i] = c1[i];
int pre = abs(tc1[0] - tc1[1]);
tc1[b[i]]++;
tc1[!b[i]]--;
if (tc1[b[i]] <= c2[b[i]])//如果改变之后这种颜色的数目小于等于目标序列这种颜色数目
{//则这个改变是最优的。
temp[i] = b[i];
for (int i = 0; i <= 1; i++)
c1[i] = tc1[i];
temp_ans++;
}
}
int t2 = 0;
for (int i = 1; i <= len; i++)//如果还存在不同的。就进行交换操作。
if (temp[i] != b[i])
t2++;
t2 /= 2;//交换可以修正两个不同的位置不匹配;
temp_ans += t2;
if (final_ans == -1)
final_ans = temp_ans;
else
final_ans = min(final_ans, temp_ans);
} void output_ans()
{
printf("%I64d\n", final_ans);
} int main()
{
//freopen("F:\\rush.txt", "r", stdin);
input_data();
get_ans(0);
get_ans(1);
output_ans();
return 0;
}

【31.58%】【codeforces 719B】 Anatoly and Cockroaches的更多相关文章

  1. codeforces 719B:Anatoly and Cockroaches

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

  2. 【31.58%】【codeforces 682D】Alyona and Strings

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  4. 【31.72%】【codeforces 604B】More Cowbell

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【21.58%】【codeforces 746D】Green and Black Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【58.33%】【codeforces 747B】Mammoth's Genome Decoding

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【中途相遇法】【STL】BAPC2014 K Key to Knowledge (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  8. 【codeforces 785D】Anton and School - 2

    [题目链接]:http://codeforces.com/contest/785/problem/D [题意] 给你一个长度为n的括号序列; 让你删掉若干个括号之后,整个序列变成前x个括号为左括号,后 ...

  9. 【30.23%】【codeforces 552C】Vanya and Scales

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. Codefroces 831B Keyboard Layouts

    B. Keyboard Layouts time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. ORA-10458: standby database requires recovery

    搭建DG最后一步打开时报错如下:   SQL> alter database open read only; alter database open read only * ERROR at l ...

  3. [Javascript] Iterate Over Items with JavaScript's for-of Loop

    In this lesson we will understand the For Of loop in Javascript which was introduced in ES6. The for ...

  4. elasticsearch选举master

    数据节点确定了master以后.会开启MasterPinger线程来与其保持心跳通信的逻辑假设通信过程中链接断开,会通过MasterNodeFailureListener监听器进行回调处理处理过程中会 ...

  5. onWindowFocusChanged-屏幕焦点函数回调情况

    1.这个函数的具体作用不太清楚,但网上有人说是 ,当activity得到或者失去焦点的时候,就会调用这个方法 先看如下代码 @Override public void onWindowFocusCha ...

  6. PDF.Js的使用—javascript中前端显示pdf文件

    PDF.Js的使用—javascript中前端显示pdf文件 写于2018/12/6 起因是一个图片展示页面需要展示pdf格式的文件,所以查了半天决定使用pdf.js,我也不求有多了解它,能实现我想要 ...

  7. Flask项目之手机端租房网站的实战开发(十二)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  8. [置顶] WebService学习总结(1)——WebService相关概念

    一.序言 大家或多或少都听过 WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成 分.但是不得不承认的是W ...

  9. Redfield Water Ripples 2.02(水波滤镜中文绿色版-支持CC)

    Redfield Water Ripples 能够用于设计具有高度现实主义风格的水波纹特效,它提供了诸多属性选项,其 3D 渲染品质另人映像深刻.此滤镜很易用,其随机设定生成器可创建差点儿无限的水波纹 ...

  10. C++组合数(combination)的实现

    实现: (nm) 既需要计算组合的总数 (32)=3: 也需要分别获得每一种组合的情形,用于穷举搜索: 1, 2; 1, 3; 2, 3 1. 递归实现 // picked + toPick == m ...