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 pb push_back
#define mk make_pair
#define fr first
#define sc second
#define vi vector < int >
#define vl vector < ll >
#define pi pair < int, int >
#define pii pair < int, pi >
#define vii vector < pi >
const int N = 1507;
const int INF = 1e9 + 7;
int posr[N], posg[N], posy[N];
int pr[N], pg[N], py[N];
int r, g, y;
int n;
string s;
main(){
cin >> n >> s;
s = ' ' + s;
for (int i = 1; i <= n; i++){
pr[i] = pr[i - 1];
pg[i] = pg[i - 1];
py[i] = py[i - 1];
if (s[i] == 'R'){
r++;
posr[r] = i;
pr[i]++;
}
else if (s[i] == 'Y'){
y++;
posy[y] = i;
py[i]++;
}
else {
g++;
posg[g] = i;
pg[i]++;
}
}
int dp[r + 1][y + 1][g + 1][3];
dp[0][0][0][0] = 0;
dp[0][0][0][1] = 0;
dp[0][0][0][2] = 0;
for (int i = 0; i <= r; i++){
for (int j = 0; j <= y; j++){
for (int c = 0; c <= g; c++){
if (i == 0 && j == 0 && c == 0)
continue;
dp[i][j][c][0] =
dp[i][j][c][1] =
dp[i][j][c][2] = INF;
if (i){
int p = posr[i];
dp[i][j][c][1] = min(dp[i][j][c][1], dp[i - 1][j][c][0] + abs(i + j + c - (p + max(0, j - py[p]) + max(0, c - pg[p]))));
dp[i][j][c][2] = min(dp[i][j][c][2], dp[i - 1][j][c][0] + abs(i + j + c - (p + max(0, j - py[p]) + max(0, c - pg[p]))));
}
if (j){
int p = posy[j];
dp[i][j][c][0] = min(dp[i][j][c][0], dp[i][j - 1][c][1] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, c - pg[p]))));
dp[i][j][c][2] = min(dp[i][j][c][2], dp[i][j - 1][c][1] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, c - pg[p]))));
}
if (c){
int p = posg[c];
dp[i][j][c][0] = min(dp[i][j][c][0], dp[i][j][c - 1][2] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, j - py[p]))));
dp[i][j][c][1] = min(dp[i][j][c][1], dp[i][j][c - 1][2] + abs(i + j + c - (p + max(0, i - pr[p]) + max(0, j - py[p]))));
}
}
}
}
int ans = min(dp[r][y][g][0], min(dp[r][y][g][1], dp[r][y][g][2]));
if (ans == INF)
puts("-1");
else
cout << ans;
}
Compilation message (stderr)
joi2019_ho_t3.cpp:24:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
main(){
^
# | 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... |