#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define sc second
#define pb push_back
#define in insert
int main () {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n; cin>>n;
string s; cin>>s;
ll r = 0 , g = 0, y = 0;
ll rp[4001], rg[4001], ry[4001];
for (int i = 0; i < n; i++) {
if (s[i] == 'R') {
r++;
rp[r] = i + 1;
}
if (s[i] == 'G') {
g++;
rg[g] = i + 1;
}
if (s[i] == 'Y') {
y++;
ry[y] = i + 1;
}
}
ll d[r + 1][g + 1][y + 1][3];
//cout<<r<<" "<<g<<" "<<y;
for (int i = 0; i <= r; i++)
for (int j = 0; j <= g; j++)
for (int k = 0; k <= y; k++)
d[i][j][k][0] = d[i][j][k][1] = d[i][j][k][2] = INT_MAX;
d[0][0][0][0] = d[0][0][0][1] = d[0][0][0][2] = 0;
for (int i = 0; i <= r; i++)
for (int j = 0; j <= g; j++)
for (int k = 0; k <= y; k++) {
if (k != 0) {
d[i][j][k][2] = min(d[i][j][k - 1][1] , d[i][j][k - 1][0]);
d[i][j][k][2] += abs(ry[k] - i - j - k);
}
if (j != 0) {
d[i][j][k][1] = min(d[i][j - 1][k][2] , d[i][j - 1][k][0]);
d[i][j][k][1] += abs(rg[j] - i - j - k);
}
if (i != 0) {
d[i][j][k][0] = min(d[i - 1][j][k][1] , d[i - 1][j][k][2]);
d[i][j][k][0] += abs(rp[i] - i - j - k);
}
//cout<<i<<" "<<j<<" "<<k<<'\n';
//cout<<d[i][j][k][0]<<" "<<d[i][j][k][1]<<" "<<d[i][j][k][2]<<'\n';
//cout<<'\n';
}
ll mn = min( {d[r][g][y][1], d[r][g][y][0], d[r][g][y][2]});
if (mn != INT_MAX)cout<<mn / 2;
else cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
1236 KB |
Output is correct |
3 |
Correct |
1 ms |
1236 KB |
Output is correct |
4 |
Correct |
1 ms |
1236 KB |
Output is correct |
5 |
Correct |
1 ms |
1236 KB |
Output is correct |
6 |
Correct |
1 ms |
1236 KB |
Output is correct |
7 |
Correct |
1 ms |
1236 KB |
Output is correct |
8 |
Correct |
2 ms |
1236 KB |
Output is correct |
9 |
Correct |
1 ms |
1236 KB |
Output is correct |
10 |
Correct |
2 ms |
1236 KB |
Output is correct |
11 |
Correct |
2 ms |
1236 KB |
Output is correct |
12 |
Correct |
1 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
852 KB |
Output is correct |
14 |
Correct |
1 ms |
980 KB |
Output is correct |
15 |
Correct |
1 ms |
1236 KB |
Output is correct |
16 |
Correct |
1 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
12 |
Halted |
0 ms |
0 KB |
- |