#include <bits/stdc++.h>
#define ll long long
#define ar array
#define db double
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rint(l, r) uniform_int_distribution<int>(l, r)(rng)
template<typename T> bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
const int INF = 1e9;
int pref[3][3][401][401];
void test_case() {
int n; string s;
cin >> n >> s;
vector<int> a(n);
for (int i = 0; i < n; i++) {
a[i] = (s[i] == 'R' ? 0 : s[i] == 'G' ? 1 : 2);
}
ar<int, 3> on{};
int where[3][n+1];
memset(where, -1, sizeof(where));
for (int i = 0; i < n; i++) {
where[a[i]][on[a[i]]++] = i;
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
// those pos[j] > pos[i]
for (int k = 0; k < on[i]; k++) {
for (int l = 0; l <= on[j]; l++) {
for (int r = 0; r < l; r++) {
pref[i][j][k][l] += where[j][r] > where[i][k];
}
}
}
}
}
int dp[on[0]+1][on[1]+1][on[2]+1][3];
fill(&dp[0][0][0][0], &dp[0][0][0][0] + 3*(on[0]+1)*(on[1]+1)*(on[2]+1), INF);
memset(dp[0][0][0], 0, sizeof(dp[0][0][0]));
for (int s = 0; s < n; s++) {
for (int i = 0; i <= min(on[0], s); i++) {
for (int j = 0; j <= on[1] && i+j <= s; j++) {
if (s-i-j > on[2]) continue;
ar<int, 3> cur{i, j, s-i-j};
for (int k = 0; k < 3; k++) {
if (dp[i][j][s-i-j][k] == INF) continue;
for (int nw = 0; nw < 3; nw++) if (nw != k) {
int pos = where[nw][cur[nw]];
if (~pos) {
int cnt2 = 0;
for (int z = 0; z < 3; z++) if (z != nw) {
cnt2 += pref[nw][z][cur[nw]][cur[z]];
}
ckmin(dp[i+(nw==0)][j+(nw==1)][s-i-j+(nw==2)][nw], dp[i][j][s-i-j][k] + cnt2);
}
}
}
}
}
}
int ans = INF;
for (int k = 0; k < 3; k++) {
ckmin(ans, dp[on[0]][on[1]][on[2]][k]);
}
cout << (ans == INF ? -1 : ans) << '\n';
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
// cin >> t;
while (t--) test_case();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
656 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
21 ms |
2396 KB |
Output is correct |
3 |
Correct |
19 ms |
2216 KB |
Output is correct |
4 |
Correct |
19 ms |
2140 KB |
Output is correct |
5 |
Correct |
19 ms |
2140 KB |
Output is correct |
6 |
Correct |
20 ms |
2140 KB |
Output is correct |
7 |
Correct |
19 ms |
2140 KB |
Output is correct |
8 |
Correct |
19 ms |
2140 KB |
Output is correct |
9 |
Correct |
19 ms |
2116 KB |
Output is correct |
10 |
Correct |
20 ms |
2136 KB |
Output is correct |
11 |
Correct |
19 ms |
2140 KB |
Output is correct |
12 |
Correct |
3 ms |
1116 KB |
Output is correct |
13 |
Correct |
7 ms |
1372 KB |
Output is correct |
14 |
Correct |
12 ms |
1624 KB |
Output is correct |
15 |
Correct |
19 ms |
2220 KB |
Output is correct |
16 |
Correct |
20 ms |
2140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
600 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
860 KB |
Output is correct |
19 |
Correct |
1 ms |
860 KB |
Output is correct |
20 |
Correct |
1 ms |
860 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
1 ms |
856 KB |
Output is correct |
23 |
Correct |
1 ms |
860 KB |
Output is correct |
24 |
Correct |
1 ms |
604 KB |
Output is correct |
25 |
Correct |
1 ms |
604 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
604 KB |
Output is correct |
28 |
Correct |
1 ms |
860 KB |
Output is correct |
29 |
Correct |
1 ms |
656 KB |
Output is correct |
30 |
Correct |
1 ms |
860 KB |
Output is correct |
31 |
Correct |
1 ms |
856 KB |
Output is correct |
32 |
Correct |
1 ms |
860 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
600 KB |
Output is correct |
36 |
Correct |
1 ms |
860 KB |
Output is correct |
37 |
Correct |
1 ms |
604 KB |
Output is correct |
38 |
Correct |
1 ms |
860 KB |
Output is correct |
39 |
Correct |
1 ms |
860 KB |
Output is correct |
40 |
Correct |
0 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Correct |
21 ms |
2396 KB |
Output is correct |
44 |
Correct |
19 ms |
2216 KB |
Output is correct |
45 |
Correct |
19 ms |
2140 KB |
Output is correct |
46 |
Correct |
19 ms |
2140 KB |
Output is correct |
47 |
Correct |
20 ms |
2140 KB |
Output is correct |
48 |
Correct |
19 ms |
2140 KB |
Output is correct |
49 |
Correct |
19 ms |
2140 KB |
Output is correct |
50 |
Correct |
19 ms |
2116 KB |
Output is correct |
51 |
Correct |
20 ms |
2136 KB |
Output is correct |
52 |
Correct |
19 ms |
2140 KB |
Output is correct |
53 |
Correct |
3 ms |
1116 KB |
Output is correct |
54 |
Correct |
7 ms |
1372 KB |
Output is correct |
55 |
Correct |
12 ms |
1624 KB |
Output is correct |
56 |
Correct |
19 ms |
2220 KB |
Output is correct |
57 |
Correct |
20 ms |
2140 KB |
Output is correct |
58 |
Correct |
103 ms |
30300 KB |
Output is correct |
59 |
Correct |
106 ms |
30524 KB |
Output is correct |
60 |
Correct |
99 ms |
30276 KB |
Output is correct |
61 |
Correct |
102 ms |
30496 KB |
Output is correct |
62 |
Correct |
28 ms |
4444 KB |
Output is correct |
63 |
Correct |
22 ms |
6236 KB |
Output is correct |
64 |
Correct |
37 ms |
16216 KB |
Output is correct |
65 |
Correct |
64 ms |
22352 KB |
Output is correct |
66 |
Correct |
101 ms |
30044 KB |
Output is correct |
67 |
Correct |
102 ms |
30172 KB |
Output is correct |
68 |
Correct |
106 ms |
30544 KB |
Output is correct |
69 |
Correct |
104 ms |
30804 KB |
Output is correct |
70 |
Correct |
105 ms |
30544 KB |
Output is correct |
71 |
Correct |
96 ms |
30056 KB |
Output is correct |
72 |
Correct |
24 ms |
9308 KB |
Output is correct |
73 |
Correct |
15 ms |
4188 KB |
Output is correct |