#include <bits/stdc++.h>
using namespace std;
int n;
string s;
int r, g, b;
const int MXN = 405;
int dp[MXN][MXN][MXN][4];
vector<int> rs, gs, bs;
signed main() {
cin >> n >> s;
int rc = 0, bc = 0, gc = 0;
for (int i=0; i<n; i++) {
if (s[i] == 'R') {
rs.push_back(i+1);
r++;
rc++;
}
if (s[i] == 'G') {
gs.push_back(i+1);
g++;
gc++;
}
if (s[i] == 'Y') {
bs.push_back(i+1);
b++;
bc++;
}
}
s = ' ' + s;
for (int ind=1; ind<=n; ind++) {
for (int r=0; r<=rc; r++) {
for (int g=0; g<=gc; g++) {
for (int b=0; b<=bc; b++) {
if (r + g + b == ind) {
for (int whi=0; whi<=3; whi++) {
///
int bt = INT_MAX / 2;
if (r > 0 && whi != 1) {
bt = min(bt, (dp[ind-1][r-1][g][1] + abs(rs[r-1] - ind)));
}
if (g > 0 && whi != 2) {
bt = min(bt, (dp[ind-1][r][g-1][2] + abs(gs[g-1] - ind)));
}
if (b > 0 && whi != 3) {
bt = min(bt, (dp[ind-1][r][g][3] + abs(bs[b-1] - ind)));
}
dp[ind][r][g][whi] = bt;
///
}
}
}
}
}
}
int re = dp[n][r][g][0];
if (re > n * n * 2) {
cout << -1 << endl;
}
else {
cout << re / 2 << endl;
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
126 ms |
163832 KB |
Output is correct |
3 |
Correct |
127 ms |
163064 KB |
Output is correct |
4 |
Correct |
125 ms |
163780 KB |
Output is correct |
5 |
Correct |
124 ms |
163832 KB |
Output is correct |
6 |
Correct |
128 ms |
163960 KB |
Output is correct |
7 |
Correct |
124 ms |
163064 KB |
Output is correct |
8 |
Correct |
124 ms |
163192 KB |
Output is correct |
9 |
Correct |
125 ms |
162168 KB |
Output is correct |
10 |
Correct |
125 ms |
163832 KB |
Output is correct |
11 |
Correct |
124 ms |
163832 KB |
Output is correct |
12 |
Correct |
28 ms |
44544 KB |
Output is correct |
13 |
Correct |
53 ms |
77816 KB |
Output is correct |
14 |
Correct |
81 ms |
112120 KB |
Output is correct |
15 |
Correct |
125 ms |
163832 KB |
Output is correct |
16 |
Correct |
124 ms |
163832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
640 KB |
Output is correct |
6 |
Correct |
1 ms |
640 KB |
Output is correct |
7 |
Correct |
1 ms |
640 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
640 KB |
Output is correct |
10 |
Correct |
1 ms |
640 KB |
Output is correct |
11 |
Incorrect |
1 ms |
640 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |