CodeForces
来源:互联网 发布:朗诗地产北京公司知乎 编辑:程序博客网 时间:2024/06/09 23:20
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.
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 needs 1turn 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.
就是一个普通的贪心,大致题意就是给定现在蟑螂的颜色排序,可以给蟑螂染色或者交换两个蟑螂的位置,求出最后让蟑螂颜色相间所需要的最少步骤。
最后蟑螂要求颜色相间,那么最后的顺序要不就是rbrbrbrbrbrb……或者是brbrbrbrbrbr……
用三个数组,一个保存现在的蟑螂顺序,一个保存rbrbrbrbrb的顺序,一个保存brbrbrbr的顺序。然后让第一个数组和第二第三个数组分别作比较,找出b和r各有多少个不同。
注意交换顺序和再染色之间,首选应该是交换顺序,无法交换时再选择染色。
#include<iostream>#include<stdio.h>#include<string>#include<math.h>#include<queue>#include<vector>#include<string.h>#include<iterator>using namespace std;typedef unsigned long long ll;char color[100000],c1[100000],c2[100000];int main(){ int n,i,j; int r1,b1,r2,b2; while(~scanf("%d",&n)){ cin>>color; for(i=0;i<n;i++){ if(i%2==0){ c1[i] = 'r';c2[i] = 'b'; } else{ c1[i] = 'b';c2[i] = 'r'; } } r1 = b1 = r2 = b2 = 0; for(i=0;i<n;i++){ if(color[i]!=c1[i]){ if(color[i]=='r') r1++; else b1++; } if(color[i]!=c2[i]){ if(color[i]=='r') r2++; else b2++; } } int m1 = max(r1,b1); int m2 = max(r2,b2); int re = min(m1,m2); cout<<re<<endl; } return 0;}
- codeforces~~~
- Codeforces
- codeforces
- Codeforces
- codeforces
- codeforces
- Codeforces
- Codeforces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- CodeForces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Codeforces
- Paper-Reading
- 爬取糗事百科
- 整数区间(贪心)
- Swift3.0之String转换成基本数据类型 Int CGFloat Double
- 新手向解密加密后的dll 提取dll 适用于简单加密后的apk
- CodeForces
- POJ-2159 最小费用最大流
- Shell输入输出重定向~11
- Eclipse最实用快捷键
- 五.Core组件进阶(3.访问图像中的像素点)
- Android四大组件之一:广播
- MATLAB数据类型
- RCTEventEmitter使用
- Linux 内核