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 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 = 200 + 5;
const int NN = 1e5;
const int mod = (1e9 + 7);
const int inf = 1e18;
int R, G;
void legenda_ne_umret() {
int n;
cin >> n;
string s, ss;
cin >> s;
ss = s;
for (int i = 0; i < n; i++) {
if (s[i] == 'R') R++;
if (s[i] == 'G') G++;
}
int ans1 = inf, ans = inf;
if (abs (R - G) > 1) {
cout << -1;
return;
}
if(R >= G){
ans = 0;
for(int i = 0 ; i < n ; i++){
if(i % 2 == 0){
for(int j = i; j < n ; j++){
if(s[j] == 'R'){
ans += (j - i);
swap(s[i] , s[j]);
break;
}
}
}
else{
for(int j = i ; j < n ; j++){
if(s[j] == 'G'){
ans += (j - i);
swap(s[i] , s[j]);
break;
}
}
}
}
}
if(G >= R){
ans1 = 0;
s = ss;
for(int i = 0 ; i < n ; i++){
if(i % 2){
for(int j = i ; j < n ; j++){
if(s[j] == 'R'){
ans1 += (j - i);
swap(s[i] , s[j]);
break;
}
}
}
else{
for(int j = i ; j < n ; j++){
if(s[j] == 'G'){
ans1 += (j - i);
swap(s[i] , s[j]);
break;
}
}
}
}
}
cout << min(ans1 , 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 |
---|
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... |