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 minim(X, Y) (X = min(X, Y))
#define F(X, Y) for(int X = 0; X < Y; X++)
#define M(X) max(X, 0)
const int Z = 202, INF = 1e9;
int N, A[Z*2], DP[Z][Z][Z][3], p[3][Z*2], h[3][Z*2], c[3], x, y, z;
int main() {
cin >> N, getchar();
F(i, N) {
char v = getchar();
A[i] = (v < 82) + (v < 89);
}
F(i, Z) F(j, Z) F(k, Z) F(l, 3)
DP[i][j][k][l] = (i || j || k) ? INF : 0;
F(i, N) {
if(++c[A[i]] >= Z-1) break;
p[A[i]][c[A[i]]] = i;
F(j, 3) h[j][i] = c[j];
F(j, i+1) F(k, i+1) {
if(A[i] == 0) x = c[0], y = j, z = k;
if(A[i] == 1) x = j, y = c[1], z = k;
if(A[i] == 2) x = j, y = k, z = c[2];
if(x > c[0] || y > c[1] || z > c[2]) continue;
int s = x + y + z;
if(x) minim(DP[x][y][z][0], min(DP[x-1][y][z][1], DP[x-1][y][z][2]) + M(y-h[1][p[0][x]]) + M(z-h[2][p[0][x]]));
if(y) minim(DP[x][y][z][1], min(DP[x][y-1][z][0], DP[x][y-1][z][2]) + M(x-h[0][p[1][y]]) + M(z-h[2][p[1][y]]));
if(z) minim(DP[x][y][z][2], min(DP[x][y][z-1][0], DP[x][y][z-1][1]) + M(x-h[0][p[2][z]]) + M(y-h[1][p[2][z]]));
}
}
int res = INF;
F(i, 3) minim(res, DP[c[0]][c[1]][c[2]][i]);
cout << (res == INF ? -1 : res);
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:31:8: warning: unused variable 's' [-Wunused-variable]
31 | int s = x + y + z;
| ^
# | 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... |