Codeforces 719B Anatoly and Cockroaches【思维】
来源:互联网 发布:mac怎么下游戏 编辑:程序博客网 时间:2024/06/05 03:51
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 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.
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.
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.
5rbbrr
1
5bbbbb
2
3rbr
0
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.
题目大意:
现在进行两种操作:
①互换两个位子。
②将一个位子的颜色翻转。
问最少操作次数,使得两种颜色间隔排列。
思路:
考虑最终答案,要么是rbrbrb.............要么就是brbrbr.................
那么对于一种情况来说,其最小操作次数就是max(奇数位子上错误字符个数,偶数位子上错误字符个数);
那么对于两种情况的操作次数取最小就是答案。
Ac代码:
#include<stdio.h>#include<string.h>#include<iostream>#include<stack>using namespace std;char a[150000];int main(){ int n; while(~scanf("%d",&n)) { scanf("%s",a); int cont1=0,cont2=0; for(int i=0;i<n;i++) { if(i%2==0) { if(a[i]=='r')cont1++; } else { if(a[i]=='b')cont2++; } } int output=max(cont1,cont2); cont1=cont2=0; for(int i=0;i<n;i++) { if(i%2==1) { if(a[i]=='r')cont1++; } else if(a[i]=='b')cont2++; } output=min(output,max(cont1,cont2)); printf("%d\n",output); }}
- Codeforces 719B Anatoly and Cockroaches【思维】
- codeforces 719B:Anatoly and Cockroaches(思维)
- CodeForces-719B Anatoly and Cockroaches(思维题)
- 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(小强排序)
- 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
- Log4j(二)----配置文件篇
- Oracle和MySQL在SQL语句方面的区别
- Linux下安装Tomcat
- 循环常见案例
- easyUI&ztree
- Codeforces 719B Anatoly and Cockroaches【思维】
- C++宏观导图
- 热门自然语言理解和语音API开发平台对比
- 19.实现单点触摸画圆
- linux 压缩解压命令
- eclipse+scala+spark+maven的配置
- 假设有一个二维数组:int a[3][3]={初始化了值};求出一种数字——在这一行最大在这一列最小 前提:最大值只有一个,最小值也只有一个。
- MySQL与Oracle SQL语言差异比较
- 【JZOJ5213】期末考试