-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntonyAndCockroaches.cpp
More file actions
62 lines (56 loc) · 1.49 KB
/
AntonyAndCockroaches.cpp
File metadata and controls
62 lines (56 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
int n;
scanf("%d", &n);
char *cocks = new char[n];
bool isRed = false;
for(int i = 0; i < n; i++) {
cin >> cocks[i];
if(i == 0) {
if(cocks[i] == 'b') isRed = true;
}
}
int swapCnt = 0, colorChangeCnt = 0;
for(int i = 1; i < n; i++) {
if(!isRed && cocks[i] == 'r') {
bool isFind = false;
for(int j = i + 1; j < n; j++) {
if(cocks[j] == 'b') {
cocks[j] = 'r';
swapCnt++;
isFind = true;
break;
}
}
if(!isFind) {
colorChangeCnt += (n - i) / 2;
if((n - i) % 2 != 0) {
colorChangeCnt++;
}
break;
}
}else if(isRed && cocks[i] == 'b') {
bool isFind = false;
for(int j = i + 1; j < n; j++) {
if(cocks[j] == 'r') {
cocks[j] = 'b';
swapCnt++;
isFind = true;
break;
}
}
if(!isFind) {
colorChangeCnt += (n - i) / 2;
if((n - i) % 2 != 0) {
colorChangeCnt++;
}
break;
}
}
isRed = !isRed;
}
cout << swapCnt + colorChangeCnt << '\n';
return 0;
}