codeforces 719B:Anatoly and Cockroaches

Description

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

5rbbrr

Output

1

Input

5bbbbb

Output

2

Input

3rbr

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.

正解:贪心

解题报告;

  Xlight他们都看成了只能交换相邻的,调了好久,论不看题的危害。

  考虑最终序列只有可能有2种情况,那么分别枚举,两个答案取一个min即可。

  考虑直接贪心,首先我们可以统计出有多少个错位的元素,最后直接把能交换的全部交换,否则就暴力染色就可以了。

 1 //It is made by jump~
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 using namespace std;
14 typedef long long LL;
15 const int inf = (1<<30);
16 const int MAXN = 100011;
17 int n,a[MAXN];
18 int cnt[3];
19 int ans,ans2;
20
21 inline int getint()
22 {
23     int w=0,q=0; char c=getchar();
24     while((c<‘0‘ || c>‘9‘) && c!=‘-‘) c=getchar(); if(c==‘-‘) q=1,c=getchar();
25     while (c>=‘0‘ && c<=‘9‘) w=w*10+c-‘0‘, c=getchar(); return q ? -w : w;
26 }
27
28 inline void work(){
29     n=getint(); char c;
30     for(int i=1;i<=n;i++) {
31     c=getchar(); while(c!=‘r‘ && c!=‘b‘) c=getchar();
32     if(c==‘b‘) a[i]=1; else a[i]=0;
33     }
34     int tag=1;  int minl;
35     for(int i=1;i<=n;i++) {
36     if(tag!=a[i]){
37         cnt[tag]++;
38         if(cnt[tag^1]>0) {
39         minl=min(cnt[tag^1],cnt[tag]);
40         cnt[tag]-=minl; cnt[tag^1]-=minl;
41         ans+=minl;
42         }
43     }
44     tag^=1;
45     }
46     minl=min(cnt[1],cnt[0]); ans+=minl; cnt[1]-=minl; cnt[0]-=minl;
47     ans+=cnt[1]; ans+=cnt[0];
48
49     tag=0;  cnt[1]=cnt[0]=0;
50     for(int i=1;i<=n;i++) {
51     if(tag!=a[i]){
52         cnt[tag]++;
53         if(cnt[tag^1]>0) {
54         minl=min(cnt[tag^1],cnt[tag]);
55         cnt[tag]-=minl; cnt[tag^1]-=minl;
56         ans2+=minl;
57         }
58     }
59     tag^=1;
60     }
61     minl=min(cnt[1],cnt[0]); ans2+=minl; cnt[1]-=minl; cnt[0]-=minl;
62     ans2+=cnt[1]; ans2+=cnt[0];
63
64     printf("%d",min(ans,ans2));
65 }
66
67 int main()
68 {
69     work();
70     return 0;
71 }
时间: 2024-10-11 01:03:07

codeforces 719B:Anatoly and Cockroaches的相关文章

Codeforces 449D:Jzzhu and Numbers

Codeforces 449D:Jzzhu and Numbers 题目链接:http://codeforces.com/problemset/status?friends=on 题目大意:给出$n$个数,求有多少种组合使得$a_{i_1}\&a_{i_2}\&...\&a_{i_k}=0(0 \leqslant i < n)$,答案对$10^9+7$取模. 容斥原理+DP 设位与$(\&)$后二进制表示中有$k$个$1$的组合数为$A_k$,则有, $A_0=$所有

Codeforces 757B:Bash&#39;s Big Day(分解因子+Hash)

http://codeforces.com/problemset/problem/757/B 题意:给出n个数,求一个最大的集合并且这个集合中的元素gcd的结果不等于1. 思路:一开始把素数表打出来,发现有9k+个数,不能暴力枚举.发现O(sqrt(n)*n)似乎可行,就枚举因子,然后出现过的因子就在Hash[]加1,最后枚举素数表里的元素,找出现次数最多的,因为那些数都可以映射在素数表里面.注意最少的ans是1. 1 #include <cstdio> 2 #include <algo

Codeforces 754E:Dasha and cyclic table

Codeforces 754E:Dasha and cyclic table 题目链接:http://codeforces.com/problemset/problem/754/E 题目大意:$A$矩阵($size(A)=n \times m$,仅含'a'-'z')在整个平面做周期延拓,问$B$矩阵($size(B)=r \times c$,包含'a'-'z'及'?','?'为通配符)在哪些位置能与$A$矩阵匹配.输出$n \times m$的01矩阵,1表示在该位置匹配. 枚举+bitset常

Codeforces 798D:Mike and distribution

Codeforces 798D:Mike and distributio 题目链接:http://codeforces.com/problemset/problem/798/D 题目大意:给出两个大小为$n$的数列$A,B$,现要求从这两个数列相同位置取出$K(K \leqslant n/2+1)$个数,使得$2 \times subA>sumA$且$2 \times subB>sumB$. 想法题 我们需要从数列$A$和数列$B$中取出$K$个数,使得这$K$个数的和比剩下$n-K$个数的和

Codeforces 855B:Marvolo Gaunt&#39;s Ring(枚举,前后缀)

B. Marvolo Gaunt's Ring Professor Dumbledore is helping Harry destroy the Horcruxes. He went to Gaunt Shack as he suspected a Horcrux to be present there. He saw Marvolo Gaunt's Ring and identified it as a Horcrux. Although he destroyed it, he is sti

CodeForces 719B. Anatoly and Cockroaches

链接: [http://codeforces.com/group/1EzrFFyOc0/contest/719/problem/B] 题意: cockroaches:蟑螂.有两种颜色red和black.输入n,另一行输入n个字符,r代表red,b代表black,那个人喜欢蟑螂颜色交替排列. 你有两种操作:1.swap任意两个字符.2.你可以用颜料把某种颜色变成另一种颜色.为了满足那个人最少需要几个操作. 思路: 交替排列有两种情况:1.rbrbrb.....2.brbrbr....分别计算这两种

CodeForces 719B Anatoly and Cockroaches (水题贪心)

题意:给定一个序列,让你用最少的操作把它变成交替的,操作有两种,任意交换两种,再就是把一种变成另一种. 析:贪心,策略是分别从br开始和rb开始然后取最优,先交换,交换是最优的,不行再变色. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <c

CodeForces 719B Anatoly and Cockroaches 思维锻炼题

题目大意:有一排蟑螂,只有r和b两种颜色,你可以交换任意两只蟑螂的位置,或涂改一个蟑螂的颜色,使其变成r和b交互排列的形式.问做少的操作次数. 题目思路:更改后的队列只有两种形式:长度为n以r开头:长度为n以b开头.与初始串进行比较并统计改变次数记作ans,算出必须进行的涂色操作的次数step,我们可以把交换两只不同颜色的蟑螂的位置看做进行了两次涂改操作,其次数为(ans-step). 那么得出改变为模板串的最小操作次数为:step+(ans-step)/2; #include<iostream

Codeforces 348B:Apple Tree(DFS+LCM+思维)

http://codeforces.com/contest/348/problem/B 题意:给一棵树,每个叶子结点有w[i]个苹果,每个子树的苹果数量为该子树所有叶子结点苹果数量之和,要使得每个结点的各个子树苹果数量相等,求至少需要拿走的苹果数量. 思路:一开始以为只要使得所有子树之和相同就行了. 1 void dfs(int u, int fa) { 2 int num = 0, mi = INF; 3 for(int i = head[u]; ~i; i = edge[i].nxt) {