//#include"holiday.h"
#include <bits/stdc++.h>
#define ll long long
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define pii pair<int,int>
#define pll pair<ll,ll>
#define MP make_pair
#define fs first
#define se second
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define pb push_back
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 4e2 + 7;
int Mod = 1e9 + 7;///lon
const int INF = 1e9;
const ll BASE = 137;
const int szBL = 450;
inline void minimize (int &A, int B) {
if (A > B) A = B;
}
int n;
int a[N];
vector<int> pos[5];
int szC[5];
int pre[5][N];
int dp[2][N][N][5]; ///0, 1
int cost (int cur, int p0, int p1, int p2) {
return max(0, pre[0][cur] - pre[0][p0]) + max(0, pre[1][cur] - pre[1][p1]) + max(0, pre[2][cur] - pre[2][p2]);
}
void solution () {
cin >> n;
string S;
cin >> S;
rep (i, 0, 2) pos[i].pb(0);
rep (i, 1, n) {
a[i] = (S[i - 1] == 'R' ? 0 : (S[i - 1] == 'G' ? 1 : 2));
pos[a[i]].pb(i);
szC[a[i]]++;
}
rep (j, 0, 2)
rep (i, 1, n) {
pre[j][i] = pre[j][i - 1] + (a[i] == j);
}
rep (j, 0, szC[0])
rep (k, 0, szC[1])
rep (t, 0, 2) dp[0][j][k][t] = INF;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
dp[0][0][0][0] = 0;
rep (i, 0, n) {
rep (j, 0, szC[0])
rep (k, 0, szC[1])
rep (t, 0, 2) dp[i & 1 ^ 1][j][k][t] = INF;
rep (j, 0, szC[0])
rep (k, 0, szC[1]) {
int p = i - j - k;
// cout << i<<","<<j<<","<<k<<","<<p<<"\n";
if (j + k > i || p > szC[2])
continue;
rep (t, 0, 2) {
if (j < szC[0] && t != 0) {
minimize (dp[i & 1 ^ 1][j + 1][k][0], dp[i & 1][j][k][t] + cost (pos[0][j + 1], pos[0][j + 1], pos[1][k], pos[2][p]) );
}
if (k < szC[1] && t != 1) {
minimize (dp[i & 1 ^ 1][j][k + 1][1], dp[i & 1][j][k][t] + cost (pos[1][k + 1], pos[0][j], pos[1][k + 1], pos[2][p]) );
}
if (p < szC[2] && t != 2) {
minimize (dp[i & 1 ^ 1][j][k][2], dp[i & 1][j][k][t] + cost (pos[2][p + 1], pos[0][j], pos[1][k], pos[2][p + 1]) );
}
}
}
}
int res = INF;
rep (i, 0, 2) res = min(res, dp[n & 1][szC[0]][szC[1]][i]);
if (res > n * n) cout << -1;
else cout << res <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("c");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug challenge +10
5 5
01101
10001
00110
10101
00100
*/
Compilation message
joi2019_ho_t3.cpp: In function 'void solution()':
joi2019_ho_t3.cpp:67:28: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
67 | rep (t, 0, 2) dp[i & 1 ^ 1][j][k][t] = INF;
| ~~^~~
joi2019_ho_t3.cpp:76:36: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
76 | minimize (dp[i & 1 ^ 1][j + 1][k][0], dp[i & 1][j][k][t] + cost (pos[0][j + 1], pos[0][j + 1], pos[1][k], pos[2][p]) );
| ~~^~~
joi2019_ho_t3.cpp:79:36: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
79 | minimize (dp[i & 1 ^ 1][j][k + 1][1], dp[i & 1][j][k][t] + cost (pos[1][k + 1], pos[0][j], pos[1][k + 1], pos[2][p]) );
| ~~^~~
joi2019_ho_t3.cpp:82:36: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
82 | minimize (dp[i & 1 ^ 1][j][k][2], dp[i & 1][j][k][t] + cost (pos[2][p + 1], pos[0][j], pos[1][k], pos[2][p + 1]) );
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
4564 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2440 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
32 ms |
4188 KB |
Output is correct |
3 |
Correct |
27 ms |
4188 KB |
Output is correct |
4 |
Correct |
27 ms |
4436 KB |
Output is correct |
5 |
Correct |
26 ms |
4188 KB |
Output is correct |
6 |
Correct |
25 ms |
5212 KB |
Output is correct |
7 |
Correct |
26 ms |
5212 KB |
Output is correct |
8 |
Correct |
26 ms |
4296 KB |
Output is correct |
9 |
Correct |
25 ms |
5212 KB |
Output is correct |
10 |
Correct |
31 ms |
5208 KB |
Output is correct |
11 |
Correct |
27 ms |
4184 KB |
Output is correct |
12 |
Correct |
5 ms |
4440 KB |
Output is correct |
13 |
Correct |
9 ms |
4736 KB |
Output is correct |
14 |
Correct |
15 ms |
4956 KB |
Output is correct |
15 |
Correct |
25 ms |
5312 KB |
Output is correct |
16 |
Correct |
26 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
0 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2648 KB |
Output is correct |
27 |
Correct |
1 ms |
4444 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
4564 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
1 ms |
2440 KB |
Output is correct |
32 |
Correct |
1 ms |
2396 KB |
Output is correct |
33 |
Correct |
2 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
2652 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
1 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2392 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
32 ms |
4188 KB |
Output is correct |
44 |
Correct |
27 ms |
4188 KB |
Output is correct |
45 |
Correct |
27 ms |
4436 KB |
Output is correct |
46 |
Correct |
26 ms |
4188 KB |
Output is correct |
47 |
Correct |
25 ms |
5212 KB |
Output is correct |
48 |
Correct |
26 ms |
5212 KB |
Output is correct |
49 |
Correct |
26 ms |
4296 KB |
Output is correct |
50 |
Correct |
25 ms |
5212 KB |
Output is correct |
51 |
Correct |
31 ms |
5208 KB |
Output is correct |
52 |
Correct |
27 ms |
4184 KB |
Output is correct |
53 |
Correct |
5 ms |
4440 KB |
Output is correct |
54 |
Correct |
9 ms |
4736 KB |
Output is correct |
55 |
Correct |
15 ms |
4956 KB |
Output is correct |
56 |
Correct |
25 ms |
5312 KB |
Output is correct |
57 |
Correct |
26 ms |
4184 KB |
Output is correct |
58 |
Correct |
60 ms |
3416 KB |
Output is correct |
59 |
Correct |
64 ms |
3604 KB |
Output is correct |
60 |
Correct |
76 ms |
4952 KB |
Output is correct |
61 |
Correct |
66 ms |
4700 KB |
Output is correct |
62 |
Correct |
30 ms |
4188 KB |
Output is correct |
63 |
Correct |
33 ms |
5212 KB |
Output is correct |
64 |
Correct |
48 ms |
5140 KB |
Output is correct |
65 |
Correct |
55 ms |
4952 KB |
Output is correct |
66 |
Correct |
65 ms |
3416 KB |
Output is correct |
67 |
Correct |
84 ms |
3164 KB |
Output is correct |
68 |
Correct |
62 ms |
4700 KB |
Output is correct |
69 |
Correct |
62 ms |
4700 KB |
Output is correct |
70 |
Correct |
68 ms |
3420 KB |
Output is correct |
71 |
Correct |
68 ms |
3416 KB |
Output is correct |
72 |
Correct |
21 ms |
4088 KB |
Output is correct |
73 |
Correct |
4 ms |
2392 KB |
Output is correct |