#include <iostream>
#include<vector>
using namespace std;
string alt(int n, bool c) {
string ans = "";
while (n--)
ans += "RG"[c], c ^= 1;
return ans;
}
int sol(string s, string t) { //s->t
int n = s.length(), ans = 0;
for (int i = 0; i < n; ++i)
if (s[i] != t[i]) {
int val = 0;
for(int j = i + 1; j < n; ++j)
if (s[j] == t[i]) {
val = j; break;
}
ans += val - i, swap(s[i], s[val]);
}
return ans;
}
int32_t main()
{
int n, r = 0, g = 0; cin >> n;
string s, a1 = alt(n, 0), a2 = alt(n, 1); cin >> s;
for (int i = 0; i < n; ++i)
if (s[i] == 'R') r++;
else g++;
if (abs(g - r) > 1) {cout << -1; return 0;}
int ans = 1e8;
if (g >= r) ans = min(ans, sol(s, a2));
if (r >= g) ans = min(ans, sol(s, a1));
cout << ans;
}
# | 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... |