【codeforces B + 思维】
2016-09-24 10:27
369 查看
B. Anatoly and Cockroaches
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.
#include<cstdio> #include<algorithm> #include<cstring> using namespace std; char st[100011]; int main() { int N,x,y,i,ans; scanf("%d",&N); scanf("%s",st); int pl = strlen(st); x = y = 0; for(i = 0 ; i < pl; i++) { if(i % 2 == 1 && st[i] == 'b') x++; if(i % 2 == 0 && st[i] == 'r') y++; } ans = max(x,y); x = y = 0; for(i = 0 ; i < pl ; i++) { if(i % 2 == 1 && st[i] == 'r') x++; if(i % 2 == 0 && st[i] == 'b') y++; } ans = min(ans,max(x,y)); printf("%d\n",ans); return 0 ; }
相关文章推荐
- Codeforces - 612D. The Union of k-Segments 排序+思维
- Codeforces 604 C Alternative Thinking【思维】
- CodeForces - 786B Legacy(线段树 +最短路+思维好题)
- CodeForces - 821C Okabe and Boxes(思维)
- Codeforces 557C Arthur and Table【思维】
- codeforces 370div.2 C Memory and De-Evolution[逆向思维]【思维】
- Codeforces 296C Greg and Array【思维】
- Codeforces 747D Persistent Bookcase【Dfs+思维】好题!好题!
- Codeforces 591E Three States【优先队列Bfs+思维】
- 【CodeForces - 920F 】SUM and REPLACE 【线段树的区间操作+思维】
- CodeForces 628B New Skateboard 思维
- codeforces 501 C. Misha and Forest (思维)
- CodeForces - 757C - Felicity is Coming!(思维)
- CodeForces 672D (思维+二分)
- CodeForces - 729D Sea Battle(思维题)
- codeforces 777C Alyona and Spreadsheet(DP+思维)
- CodeForces - 711C Coloring Trees(DP)(思维)
- Codeforces 689E Mike and Geometry Problem 思维
- Codeforces 763B Timofey and rectangles(四色定理)(思维)
- Codeforces 652D Nested Segments【离散化+思维+树状数组】