This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |