//#pragma GCC target ("avx2,sse2")
//#pragma GCC optimization ("Ofast")
//#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree <pair <ll, int>, null_type, less <pair <ll, int> >, rb_tree_tag, tree_order_statistics_node_update>
#define ll long long
#define ull unsigned long long
#define db double
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define fi first
#define se second
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define endl "\n"
#define left fsdsdfoisf
#define sum dpsdfioppsf
#define assign xcvjlkdjfio
#define trie fksdfkjkfnjuiv
#define next sidlfjsfkl
#define merge sdfksdkfsldf
//#define int long long
using namespace std;
void dout() {
cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << H << ' ';
dout(T...);
}
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
const int N = 63, inf = 1e9;
int n, dp[N][N][N][3], R, G, Y;
vector <int> r, g, y;
string s;
void upd(int & x, int y) {
x = min(x, y);
}
void solve(int tc) {
// check for (int i = 0; i < n; j++)
cin >> n >> s;
for (int i = 1; i <= n; i++) {
if (s[i - 1] == 'R') {
r.pb(i);
} else if (s[i - 1] == 'G') {
g.pb(i);
} else {
y.pb(i);
}
}
R = sz(r), G = sz(g), Y = sz(y);
for (int i = 1; i <= n; i++) {
for (int j = 0; j <= min(i, R); j++) {
for (int k = 0; k <= min(i - j, G); k++) {
int l = i - j - k;
if (l > Y) {
continue;
}
for (int h = 0; h < 3; h++) {
dp[j][k][l][h] = inf;
}
if (j > 0) {
upd(dp[j][k][l][0], dp[j - 1][k][l][1]);
upd(dp[j][k][l][0], dp[j - 1][k][l][2]);
int cnt = 0;
for (int h = 0; h < k; h++) {
if (g[h] > r[j - 1]) {
cnt++;
}
}
for (int h = 0; h < l; h++) {
if (y[h] > r[j - 1]) {
cnt++;
}
}
dp[j][k][l][0] += abs(r[j - 1] + cnt - i);
}
if (k > 0) {
upd(dp[j][k][l][1], dp[j][k - 1][l][0]);
upd(dp[j][k][l][1], dp[j][k - 1][l][2]);
int cnt = 0;
for (int h = 0; h < j; h++) {
if (r[h] > g[k - 1]) {
cnt++;
}
}
for (int h = 0; h < l; h++) {
if (y[h] > g[k - 1]) {
cnt++;
}
}
dp[j][k][l][1] += abs(g[k - 1] + cnt - i);
}
if (l > 0) {
upd(dp[j][k][l][2], dp[j][k][l - 1][0]);
upd(dp[j][k][l][2], dp[j][k][l - 1][1]);
int cnt = 0;
for (int h = 0; h < j; h++) {
if (r[h] > y[l - 1]) {
cnt++;
}
}
for (int h = 0; h < k; h++) {
if (g[h] > y[l - 1]) {
cnt++;
}
}
dp[j][k][l][2] += abs(y[l - 1] + cnt - i);
}
}
}
}
int res = inf;
for (int i = 0; i < 3; i++) {
upd(res, dp[R][G][Y][i]);
}
if (res == inf) {
res = -1;
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int tc = 1;
// cin >> tc;
for (int i = 0; i < tc; i++) {
solve(i);
// cleanup();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
4 ms |
760 KB |
Output is correct |
19 |
Correct |
4 ms |
760 KB |
Output is correct |
20 |
Correct |
3 ms |
760 KB |
Output is correct |
21 |
Correct |
3 ms |
760 KB |
Output is correct |
22 |
Correct |
3 ms |
632 KB |
Output is correct |
23 |
Correct |
4 ms |
764 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
1148 KB |
Output is correct |
26 |
Correct |
3 ms |
1144 KB |
Output is correct |
27 |
Correct |
3 ms |
888 KB |
Output is correct |
28 |
Correct |
3 ms |
760 KB |
Output is correct |
29 |
Correct |
3 ms |
888 KB |
Output is correct |
30 |
Correct |
3 ms |
760 KB |
Output is correct |
31 |
Correct |
3 ms |
632 KB |
Output is correct |
32 |
Correct |
3 ms |
760 KB |
Output is correct |
33 |
Correct |
3 ms |
1144 KB |
Output is correct |
34 |
Correct |
3 ms |
1016 KB |
Output is correct |
35 |
Correct |
3 ms |
760 KB |
Output is correct |
36 |
Correct |
3 ms |
632 KB |
Output is correct |
37 |
Correct |
3 ms |
632 KB |
Output is correct |
38 |
Correct |
3 ms |
632 KB |
Output is correct |
39 |
Correct |
3 ms |
732 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
3 ms |
760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Runtime error |
7 ms |
4088 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
504 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
380 KB |
Output is correct |
18 |
Correct |
4 ms |
760 KB |
Output is correct |
19 |
Correct |
4 ms |
760 KB |
Output is correct |
20 |
Correct |
3 ms |
760 KB |
Output is correct |
21 |
Correct |
3 ms |
760 KB |
Output is correct |
22 |
Correct |
3 ms |
632 KB |
Output is correct |
23 |
Correct |
4 ms |
764 KB |
Output is correct |
24 |
Correct |
3 ms |
632 KB |
Output is correct |
25 |
Correct |
3 ms |
1148 KB |
Output is correct |
26 |
Correct |
3 ms |
1144 KB |
Output is correct |
27 |
Correct |
3 ms |
888 KB |
Output is correct |
28 |
Correct |
3 ms |
760 KB |
Output is correct |
29 |
Correct |
3 ms |
888 KB |
Output is correct |
30 |
Correct |
3 ms |
760 KB |
Output is correct |
31 |
Correct |
3 ms |
632 KB |
Output is correct |
32 |
Correct |
3 ms |
760 KB |
Output is correct |
33 |
Correct |
3 ms |
1144 KB |
Output is correct |
34 |
Correct |
3 ms |
1016 KB |
Output is correct |
35 |
Correct |
3 ms |
760 KB |
Output is correct |
36 |
Correct |
3 ms |
632 KB |
Output is correct |
37 |
Correct |
3 ms |
632 KB |
Output is correct |
38 |
Correct |
3 ms |
632 KB |
Output is correct |
39 |
Correct |
3 ms |
732 KB |
Output is correct |
40 |
Correct |
2 ms |
376 KB |
Output is correct |
41 |
Correct |
3 ms |
760 KB |
Output is correct |
42 |
Correct |
2 ms |
376 KB |
Output is correct |
43 |
Runtime error |
7 ms |
4088 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
44 |
Halted |
0 ms |
0 KB |
- |