#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;
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;
}
int dp[n+1][n+1][n+1][3];
fill(&dp[0][0][0][0], &dp[0][0][0][0] + 3*(n+1)*(n+1)*(n+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++) {
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 cnt = 0;
for (int z = 0; z < 3; z++) {
for (int idx = 0; idx < cur[z]; idx++) {
cnt += where[z][idx] > pos;
}
}
ckmin(dp[i+(nw==0)][j+(nw==1)][s-i-j+(nw==2)][nw], dp[i][j][s-i-j][k] + cnt);
}
}
}
}
}
}
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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
428 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
428 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
2908 KB |
Output is correct |
19 |
Correct |
3 ms |
2908 KB |
Output is correct |
20 |
Correct |
3 ms |
2908 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
2892 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
4 ms |
2908 KB |
Output is correct |
28 |
Correct |
4 ms |
2908 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
4 ms |
3120 KB |
Output is correct |
32 |
Correct |
3 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
3 ms |
2652 KB |
Output is correct |
36 |
Correct |
3 ms |
2908 KB |
Output is correct |
37 |
Correct |
3 ms |
2504 KB |
Output is correct |
38 |
Correct |
3 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2908 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
652 ms |
757508 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
352 KB |
Output is correct |
6 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
428 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
4 ms |
2908 KB |
Output is correct |
19 |
Correct |
3 ms |
2908 KB |
Output is correct |
20 |
Correct |
3 ms |
2908 KB |
Output is correct |
21 |
Correct |
3 ms |
2908 KB |
Output is correct |
22 |
Correct |
3 ms |
2892 KB |
Output is correct |
23 |
Correct |
3 ms |
2908 KB |
Output is correct |
24 |
Correct |
3 ms |
2908 KB |
Output is correct |
25 |
Correct |
2 ms |
2908 KB |
Output is correct |
26 |
Correct |
2 ms |
2908 KB |
Output is correct |
27 |
Correct |
4 ms |
2908 KB |
Output is correct |
28 |
Correct |
4 ms |
2908 KB |
Output is correct |
29 |
Correct |
4 ms |
2908 KB |
Output is correct |
30 |
Correct |
3 ms |
2908 KB |
Output is correct |
31 |
Correct |
4 ms |
3120 KB |
Output is correct |
32 |
Correct |
3 ms |
2908 KB |
Output is correct |
33 |
Correct |
2 ms |
2908 KB |
Output is correct |
34 |
Correct |
2 ms |
2908 KB |
Output is correct |
35 |
Correct |
3 ms |
2652 KB |
Output is correct |
36 |
Correct |
3 ms |
2908 KB |
Output is correct |
37 |
Correct |
3 ms |
2504 KB |
Output is correct |
38 |
Correct |
3 ms |
2908 KB |
Output is correct |
39 |
Correct |
3 ms |
2908 KB |
Output is correct |
40 |
Correct |
2 ms |
2908 KB |
Output is correct |
41 |
Correct |
2 ms |
2908 KB |
Output is correct |
42 |
Correct |
0 ms |
348 KB |
Output is correct |
43 |
Execution timed out |
652 ms |
757508 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |