#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
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 () {
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
272 KB |
Output is correct |
6 |
Correct |
2 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
272 KB |
Output is correct |
6 |
Correct |
2 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
292 KB |
Output is correct |
18 |
Execution timed out |
1082 ms |
204 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
288 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
272 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
272 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
292 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
272 KB |
Output is correct |
5 |
Correct |
1 ms |
272 KB |
Output is correct |
6 |
Correct |
2 ms |
292 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
292 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
296 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
292 KB |
Output is correct |
17 |
Correct |
0 ms |
292 KB |
Output is correct |
18 |
Execution timed out |
1082 ms |
204 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |