#include <bits/stdc++.h>
using namespace std;
#define int long long
typedef pair<int, int> pii;
const int MXN = 405;
int n;
string s;
vector<int> R, G;
int SOLVE0() { // RGRG...RGRGR
int ans = 0;
for (int i = 0; i < n / 2; i++) {
ans += abs(i * 2 + 1 - G[i]);
}
return ans;
}
int SOLVE1() { // RGRG...RGRG, GRGR...GRGR
int ans0 = 0, ans1 = 0;
for (int i = 0; i < n / 2; i++) {
ans0 += abs(i * 2 + 1 - G[i]);
ans1 += abs(i * 2 + 1 - R[i]);
}
return min(ans0, ans1);
}
int SOLVE2() { // GRGR...GRGRG
int ans = 0;
for (int i = 0; i < n / 2; i++) {
ans += abs(i * 2 + 1 - R[i]);
}
return ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
for (int i = 0; i < n; i++) {
if (s[i] == 'R') R.push_back(i);
else if (s[i] == 'G') G.push_back(i);
else return 0;
}
if (R.size() == G.size() + 1) cout << SOLVE0() << endl;
else if (R.size() == G.size()) cout << SOLVE1() << endl;
else if (R.size() + 1 == G.size()) cout << SOLVE2() << endl;
else cout << -1 << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |