codeforces 719B:Anatoly and Cockroaches(思维)
2017-10-15 10:25
603 查看
codeforces 719B:Anatoly and Cockroaches(思维)
DescriptionAnatoly 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 aren
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 integern (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 needs1 turn to do this.
In the second sample, the optimum answer is to paint the second and the fourth cockroaches red. This requires2 turns.
In the third sample, the colors of cockroaches in the line are alternating already, thus the answer is0.
题意:一个只含有r和b的字符串,两种操作,1 交换字符串中任意两个元素的位置,2 把r替换为b,或者把b替换为r,输出把字符串转换为交替的序列需要的最小操作数
思路:目标字符串只有两种情况,rbrbrbrb或者brbrbrbr,分别求出给定字符串向这两种字符串转换所需的操作次数,取最小即可,那么如何求转换所需的最小次数呢,遍历一遍即可,分别求出与目标字符串不同的r 和 b的个数,例如,给定字符串与目标字符串字符不相同的位置上r有6个,b有4个,那么可以交换操作4次,修改操作2次,共6次
#include<bits/stdc++.h> using namespace std; char s[100010]; char s1[100010]; char s2[100010]; int main(void){ int n; while(scanf("%d%s",&n,s)!=EOF){ int ans=0; s1[0]='r'; s2[0]='b'; for(int i=1;i<n;i++){ if(s1[i-1]=='r'){ s1[i]='b'; }else{ s1[i]='r'; } if(s2[i-1]=='r'){ s2[i]='b'; }else{ s2[i]='r'; } } int count_r=0,count_b=0; for(int i=0;i<n;i++){ if(s[i]!=s1[i]){ if(s[i]=='r'){ count_r++; }else{ count_b++; } } } ans=abs(count_r-count_b)+min(count_r,count_b); count_r=0,count_b=0; for(int i=0;i<n;i++){ if(s[i]!=s2[i]){ if(s[i]=='r'){ count_r++; }else{ count_b++; } } } ans=min(ans,abs(count_r-count_b)+min(count_r,count_b)); printf("%d\n",ans); } return 0; }
相关文章推荐
- 【31.58%】【codeforces 719B】 Anatoly and Cockroaches
- Codeforces 719B B. Anatoly and Cockroaches
- codeforces 719B:Anatoly and Cockroaches
- Codeforces-719B-Anatoly and Cockroaches
- [codeforces] B - Anatoly and Cockroaches 贪心
- Codeforces 719B-Anatoly and Cockroaches
- codeforces 716 B. Anatoly and Cockroaches(greedy)
- Codeforces Round #373 (Div. 2) B.Anatoly and Cockroaches
- cf--B - Anatoly and Cockroaches
- Codeforces Round #373 (Div. 2) B. Anatoly and Cockroaches 水题
- B. Anatoly and Cockroaches(CF B)
- Codeforces Round #373 (Div. 2) Anatoly and Cockroaches —— 贪心
- Codeforces Round #373 (Div. 2)-B. Anatoly and Cockroaches
- B. Anatoly and Cockroaches
- Anatoly and Cockroaches Codeforces Round#373-B
- Anatoly and Cockroaches
- Codeforces Round #373 (Div. 2) B.Anatoly and Cockroaches
- 【Codeforces Round #373 (Div. 2) 】【贪心】B. Anatoly and Cockroaches
- Codeforces 766E Mahmoud and a xor trip [二进制,]【数学+思维】
- CodeForces 547D Mike and Fish 思维