This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define speed ios_base::sync_with_stdio(0), cin.tie(0)
#define int long long
#define st first
#define nd second
#define pb push_back
#define love our_kolhoz
using namespace std;
const int N = 1e5 + 10, inf = 1e18, mod = 1e9 + 7;
//koz qorqaq
int n, g, r, y;
string a;
void solve() {
cin >> n >> a;
for (int i = 0; i < n; i++) {
if (a[i] == 'R') {
r++;
}
else if (a[i] == 'G') {
g++;
}
else {
y++;
}
}
if (y > r + g || r > y + g || g > r + y) {
cout << -1;
return;
}
int ans = 0;
for (int i = n - 1; i > 0; i--) {
if (a[i] == a[i - 1]) {
for (int j = i; j >= 0; j--) {
if (a[j] != a[i]) {
swap(a[j], a[i - 1]);
ans += (i - 1 - j);
break;
}
}
}
}
for (int i = 0; i < n - 1; i++) {
if (a[i] == a[i + 1]) {
for (int j = i + 1; j < n; j++) {
if (a[j] != a[i]) {
swap(a[j], a[i + 1]);
ans += (j - i - 1);
break;
}
}
}
}
//cout << a << ' ';
cout << ans;
}
signed main() {
speed;
//freopen("points.in ", "r", stdin);
//freopen("points.out", "w", stdout);
int _t = 1;
//cin >> _t;
while (_t--)
solve();
return 0;
}
// sau bol
// there's really no way of winning
// if in their eyes you'll always be a dumb blonde...
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |