#include <bits/stdc++.h>
#define int long long
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define sz size()
#define yes "YES"
#define no "NO"
#define IOI ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define pf push_front
#define pb push_back
#define S second
#define F first
using namespace std;
const int N = 20 + 5;
const int NN = 20;
const int mod = (1e9 + 7);
const int inf = 1e18;
int n, m, R, ans, G;
void legenda_ne_umret() {
string s;
cin >> n >> s;
queue<int> posg, posr;
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
posr.push(i);
R++;
}
else {
posg.push(i);
G++;
}
}
if (abs(R - G) > 1) {
cout << -1;
return;
}
if (G > R) {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (s[i] == 'G') {
posg.pop();continue;
}
ans += posg.front() - i;
// cout << i << ' ' << posg.front() << '\n';
for (int j = posg.front(); j > i; j--) swap(s[j], s[j - 1]);
posg.pop();
}
else {
if (s[i] == 'R') {
posr.pop();continue;
}
ans += posr.front() - i;
// cout << i << ' ' << posr.front() << '\n';
for (int j = posr.front(); j > i; j--) swap(s[j], s[j - 1]);
posr.pop();
}
}
cout << ans;
}
else {
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
if (s[i] == 'R') {
posr.pop();continue;
}
ans += posr.front() - i;
// cout << i << ' ' << posr.front() << '\n';
for (int j = posr.front(); j > i; j--) swap(s[j], s[j - 1]);
posr.pop();
}
else {
if (s[i] == 'G') {
posg.pop();continue;
}
ans += posg.front() - i;
// cout << i << ' ' << posg.front() << '\n';
for (int j = posg.front(); j > i; j--) swap(s[j], s[j - 1]);
posg.pop();
}
}
cout << ans;
}
}
signed main() {
IOI;
// freopen("maze.in", "r", stdin);
// freopen("maze.out", "w", stdout);
/////////////////////////////////////////////
int t = 1;
// cin >> t;
for (int i = 1; i <= t; i++) {
// cout << "Case " << i << ":\n";
legenda_ne_umret();
// cout << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |