#include<bits/stdc++.h>
using namespace std;
int f(string s, string st) {
int n = st.size();
int ans = 0;
for(int i = 0; i < n; i++) {
if(s[i] != st[i]) {
for(int j = i + 1; j < n; j++) {
if(st[i] == s[j]) {
ans += (j - i);
swap(st[i], st[j]);
break;
}
}
}
}
return ans;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
string st;
cin >> st;
set<int> s[3];
for(int i = 0; i < n; i++) {
if(st[i] == 'R') {
s[0].insert(i);
st[i] = '0';
}
if(st[i] == 'G') {
s[1].insert(i);
st[i] = '1';
}
if(st[i] == 'Y') {
s[2].insert(i);
st[i] = '2';
}
}
string t = "", ts = "";
for(int i = 0; i < n; i++) {
if(i % 2 == 0) {
t += '0';
ts += '1';
} else {
t += '1';
ts += '0';
}
}
if(s[2].empty()) {
int N = s[0].size(), N1 = s[1].size();
int ans = 0;
if(abs(N - N1) > 1) cout << -1 << '\n';
else if(N > N1) cout << f(t, st) << '\n';
else if(N1 > N) cout << f(ts, st) << '\n';
else cout << min(f(t, st), f(ts, st)) << '\n';
}
return 0;
}
# | 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... |