//don't copy pls)
/*TAAK ZDES NADO RECURSIU PISAT*/
//I'm not in the danger i am the DANGER
#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#define ll long long
#define pb push_back
#define int long long
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
#define pii pair<int,int>
#define sigma signed
using namespace std;
using namespace __gnu_pbds;
const int N = 3e5 + 5;
int mod = 1e9 + 7;
const int INF = 1e18;
int n;
string s;
void Gold(){
cin >> n >> s;
s = '+' + s;
string t = s;
int ans = 0 , ans1 = INF;
int cnt1 = 0 , cnt2 = 0;
for(auto it : s){
if(it == 'R') cnt1++;
else if(it == 'G') cnt2++;
}
if(abs(cnt1 - cnt2) > 1){
cout << "-1\n";
return;
}
// cout << cnt1 << ' ' << cnt2 << '\n';
if(cnt1 >= cnt2){
for(int i = 1 ; i <= n ; i++){
if(i % 2){
for(int j = i ; j <= n ; j++){
if(s[j] == 'R'){
ans += (j - i);
swap(s[i] , s[j]);
// cout << i << ' ' << j << '\n';
break;
}
}
}
else{
for(int j = i ; j <= n ; j++){
if(s[j] == 'G'){
ans += (j - i);
swap(s[i] , s[j]);
break;
}
}
}
}
ans1 = ans;
}
if(cnt2 >= cnt1){
ans = 0;
s = t;
for(int i = 1 ; 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;
}
}
}
}
}
cout << min(ans1 , ans);
}
sigma main(){
//freopen("txt.in","r",stdin);
//freopen("txt.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
srand(time(0));
int TT = 1;
// cin >> TT;
for(int i = 1 ; i <= TT ; i++){
//cout << "Case " << i << ": ";
Gold();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
456 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 |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
0 ms |
456 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |