This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define ll int
#define F first
#define S second
#define ull unsigned long long
#define db double
#define ldb long double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ordered_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
#define all(x) x.begin(), x.end()
const int mod = 1e9 + 7;
const int N = 35001;
using namespace std;
using namespace __gnu_pbds;
ll n, m, k, a, b, c, d, e, f, ans = 1e9, sum;
string s;
vector <ll> r, g, y;
void rec (ll a, ll b, ll c, string t){
if (t.size() == n){
sum = 0;
// cout << t << ' ' << s << '\n';
for (int i = 0; i < n; i++){
for (int j = i; j < n; j++){
if (s[i] == t[j]){
a = j;
break;
}
}
while (a != i){
swap (t[a], t[a - 1]);
a--;
sum++;
}
// cout << sum << ' ';
}
ans = min (ans, sum);
return;
}
if (t.size() == 0){
if (a){
rec (a - 1, b, c, t + "R");
}
if (b){
rec (a, b - 1, c, t + "G");
}
if (c){
rec (a, b, c - 1, t + "Y");
}
}
else{
if (t[t.size() - 1] == 'R'){
if (b){
rec (a, b - 1, c, t + "G");
}
if (c){
rec (a, b, c - 1, t + "Y");
}
}
if (t[t.size() - 1] == 'G'){
if (a){
rec (a - 1, b, c, t + "R");
}
if (c){
rec (a, b, c - 1, t + "Y");
}
}
if (t[t.size() - 1] == 'Y'){
if (a){
rec (a - 1, b, c, t + "R");
}
if (b){
rec (a, b - 1, c, t + "G");
}
}
}
}
signed main (){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> s;
for (int i = 0; i < s.size(); i++){
if (s[i] == 'R'){
a++;
}
else if (s[i] == 'G'){
b++;
}
else{
c++;
}
}
if (a > b + c + 1 || b > a + c + 1 || c > a + b + 1){
cout << "-1";
return 0;
}
rec (a, b, c, "");
cout << ans;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'void rec(int, int, int, std::string)':
joi2019_ho_t3.cpp:31:15: warning: comparison of integer expressions of different signedness: 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
31 | if (t.size() == n){
| ~~~~~~~~~^~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:94:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (int i = 0; i < s.size(); i++){
| ~~^~~~~~~~~~
# | 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... |