//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;
int cnt1 = 0 , cnt2 = 0 , cnt3 = 0;
string s,t;
vector <string> q;
void rec(char last , int sz){
if(sz == n){
q.pb(t);
return;
}
int ans = 0;
if(last != 'R' && cnt1){
cnt1--;
t.pb('R');
rec('R' , sz + 1);
t.pop_back();
cnt1++;
}
if(last != 'G' && cnt2){
cnt2--;
t.pb('G');
rec('G' , sz + 1);
t.pop_back();
cnt2++;
}
if(last != 'Y' && cnt3){
cnt3--;
t.pb('Y');
rec('Y' , sz + 1);
t.pop_back();
cnt3--;
}
}
void Gold(){
cin >> n >> s;
s = '+' + s;
int ans = 0 , ans1 = INF;
for(auto it : s){
if(it == 'R') cnt1++;
else if(it == 'G') cnt2++;
if(it == 'Y') cnt3++;
}
// cout << cnt1 << ' ' <<cnt2 << ' ' << cnt3 << '\n';
// if(cnt1 + cnt2 == n){
// string t = s;
// 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);
// return;
// }
// else{
string qq = s;
rec('A' , 0);
ans = INF;
for(auto it : q){
s = qq;
int ans1 = 0;
t = '=' + it;
// cout << t << '\n';
for(int i = 1 ; i <= n ; i++){
bool ok = 0;
for(int j = i ; j <= n ; j++){
if(t[i] == s[j]){
ans1 += (j - i);
swap(s[i] , s[j]);
ok = 1;
// cout << i << ' ' << j << ' ' << ans1 << '\n';
break;
}
}
if(!ok){
ans1 = INF;
break;
}
}
// cout << t << ' ' << ans1 << '\n';
ans = min(ans , ans1);
}
if(ans > s.size()){
cout << "-1\n";
return;
}
cout << 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();
}
}
Compilation message
joi2019_ho_t3.cpp: In function 'void rec(char, long long int)':
joi2019_ho_t3.cpp:29:6: warning: unused variable 'ans' [-Wunused-variable]
29 | int ans = 0;
| ^~~
joi2019_ho_t3.cpp: In function 'void Gold()':
joi2019_ho_t3.cpp:148:10: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | if(ans > s.size()){
| ~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:55:16: warning: unused variable 'ans1' [-Wunused-variable]
55 | int ans = 0 , ans1 = INF;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
- |