Codeforces-719B-Anatoly and Cockroaches
来源:互联网 发布:男子遭遇网络诈骗 编辑:程序博客网 时间:2024/06/05 06:23
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.
题意:给你一个字符串,r和b组成,两种操作,替换,和交换,问最小操作数达到rbrbrbrb….或者brbrbrbr….
题解:两种都找一遍取最小值
#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<cmath>using namespace std;char s[100005];int main(){ int n; while(scanf("%d",&n)!=EOF) { scanf("%s",s); int cnt1=0,cnt2=0; for(int i=0;i<n;i++) { if(i%2==1) { if(s[i]!='b') cnt1++; } else { if(s[i]!='r') cnt2++; } } int sum1=abs(cnt1-cnt2)+min(cnt1,cnt2); int cnt3=0,cnt4=0; for(int i=0;i<n;i++) { if(i%2==1) { if(s[i]!='r') cnt3++; } else { if(s[i]!='b') cnt4++; } } int sum2=abs(cnt3-cnt4)+min(cnt3,cnt4); int sum=min(sum1,sum2); printf("%d\n",sum); } return 0;}
- Codeforces 719B B. Anatoly and Cockroaches
- 【31.58%】【codeforces 719B】 Anatoly and Cockroaches
- Codeforces-719B-Anatoly and Cockroaches
- Codeforces 719B. Anatoly and Cockroaches
- CodeForces 719B. Anatoly and Cockroaches
- Codeforces 719B-Anatoly and Cockroaches
- Codeforces 719B Anatoly and Cockroaches【思维】
- codeforces 719B:Anatoly and Cockroaches(思维)
- CodeForces 719B Anatoly and Cockroaches(小强排序)
- CodeForces-719B Anatoly and Cockroaches(思维题)
- Anatoly and Cockroaches Codeforces Round#373-B
- [codeforces] B - Anatoly and Cockroaches 贪心
- B. Anatoly and Cockroaches
- cf--B - Anatoly and Cockroaches
- codeforces 716 B. Anatoly and Cockroaches(greedy)
- 【Codeforces Round #373 (Div. 2) 】【贪心】B. Anatoly and Cockroaches
- Codeforces Round #373 (Div. 2) B.Anatoly and Cockroaches
- Codeforces Round #373 (Div. 2)-B. Anatoly and Cockroaches
- EL表达式,分页 ,集合遍历
- 软件工程_实验1_流程控制语句_详细解答
- 配置云主机远程访问mysql数据库注意问题
- MongDB基础学习(六)—— 【MongoDB for Java】Java操作MongoDB
- 对Sql Server表字段进行修改
- Codeforces-719B-Anatoly and Cockroaches
- 跟着鬼哥学爬虫-4-根据需求爬工作需要内容
- WPF DevExpress 更换主题
- 设计模式(15)------解释器模式
- TI zigbee UART的两种模式
- Mac 下使用ISIS 处理数据 -CTX,HiRISE,LRO
- 设计模式(16)------迭代器模式
- 设计模式(17)------中介者模式
- 这只是一个测试博客Visual Studio Ultimate 2013,vs2013.5_ult_chs.iso,vs2013.5_ult_enu.iso