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 <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <deque>
#include <set>
#include <stack>
#include <string>
#include <map>
#include <queue>
#define int long long
using namespace std;
const long long oo = 1000000000000000000;
long long sum, ans = 0, mx = 0, mn = 1000000000, num, pos;
/*
ViHHiPuh
(( `'-""``""-'` ))
)-__-_.._-__-(
/ --- (o _ o) --- \
\ .-* ( .0. ) *-. /
_'-. ,_ '=' _, .-'_
/ `;#'#'# - #'#'#;` \
\_)) -----'#'----- ((_/
# --------- #
'# ------- ------ #'
/..-'# ------- #'-.\
_\...-\'# -- #'/-.../_
((____)- '#' -(____))
cout << fixed << setprecision(6) << x;
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
freopen ( "sum.in", "r", stdin )
*/
int n;
string s, ss;
int f( int o = 0 ) {
if ( o == s.size() )
return 0;
int mnn = oo;
int r, g, y;
r = g = y = -1;
for ( int i = o; i < s.size(); i ++ ) {
if ( ss[ i ] == 'R' && !( ~r ) ) {
r = i;
}
if ( ss[ i ] == 'Y' && !( ~y ) )
y = i;
if ( ss[ i ] == 'G' && !( ~g ) )
g = i;
}
if ( o && ss[ o - 1 ] == 'Y' )
y = -1;
if ( o && ss[ o - 1 ] == 'G' )
g = -1;
if ( o && ss[ o - 1 ] == 'R' )
r = -1;
if ( r != -1 ) {
for ( int i = r - 1; i >= o; i -- ) {
swap( ss[ i ], ss[ i + 1 ] );
}
mnn = min( mnn, f( o + 1 ) + ( r - o ) );
for ( int i = o + 1; i <= r; i ++ ) {
swap( ss[ i ], ss[ i - 1 ] );
}
}
if ( ~g ) {
for ( int i = g - 1; i >= o; i -- ) {
swap( ss[ i ], ss[ i + 1 ] );
}
mnn = min( mnn, f( o + 1 ) + ( g - o ) );
for ( int i = o + 1; i <= g; i ++ ) {
swap( ss[ i ], ss[ i - 1 ] );
}
}
if ( ~y ) {
for ( int i = y - 1; i >= o; i -- ) {
swap( ss[ i ], ss[ i + 1 ] );
}
mnn = min( mnn, f( o + 1 ) + ( y - o ) );
for ( int i = o + 1; i <= y; i ++ ) {
swap( ss[ i ], ss[ i - 1 ] );
}
}
return mnn;
}
main () {
cin >> n;
cin >> s;
ss = s;
ans = f(0);
if ( ans == oo )
cout << -1;
else
cout << ans;
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'long long int f(long long int)':
joi2019_ho_t3.cpp:50:9: 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]
50 | if ( o == s.size() )
| ~~^~~~~~~~~~~
joi2019_ho_t3.cpp:55:21: 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]
55 | for ( int i = o; i < s.size(); i ++ ) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp: At global scope:
joi2019_ho_t3.cpp:100:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
100 | main () {
| ^~~~
# | 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... |