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;
typedef long long ll;
const int MX = 407;
int N;
int dp[MX][MX][MX][3], lastR[MX], lastG[MX], lastY[MX];
vector<int> R, G, Y;
int f(int r, int g, int y, int c) {
int pos = r + g + y, res = 1e9;
if(dp[r][g][y][c] != -1) return dp[r][g][y][c];
if(pos == N) return dp[r][g][y][c] = 0;
if(c != 0 && r + 1 < R.size()) {
int cost = max(0, lastG[R[r + 1]] - g) + max(0, lastY[R[r + 1]] - y);
res = min(res, f(r + 1, g, y, 0) + cost);
}
if(c != 1 && g + 1 < G.size()) {
int cost = max(0, lastR[G[g + 1]] - r) + max(0, lastY[G[g + 1]] - y);
res = min(res, f(r, g + 1, y, 1) + cost);
}
if(c != 2 && y + 1 < Y.size()) {
int cost = max(0, lastR[Y[y + 1]] - r) + max(0, lastG[Y[y + 1]] - g);
res = min(res, f(r, g, y + 1, 2) + cost);
}
return dp[r][g][y][c] = res;
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
memset(dp, -1, sizeof dp);
cin >> N;
R = {0}, G = {0}, Y = {0};
for(int i = 1; i <= N; i++) {
char c;
cin >> c;
if(c == 'R') R.push_back(i);
if(c == 'G') G.push_back(i);
if(c == 'Y') Y.push_back(i);
lastR[i] = R.size() - 1;
lastG[i] = G.size() - 1;
lastY[i] = Y.size() - 1;
}
for(int r = 1; r < R.size(); r++) {
for(int g = 1; g < G.size(); g++) {
for(int y = 1; y < Y.size(); y++) {
}
}
}
int k = min({f(0, 0, 0, 0), f(0, 0, 0, 1), f(0, 0, 0, 2)});
if(k == 1e9) k = -1;
cout << k << '\n';
}
Compilation message (stderr)
joi2019_ho_t3.cpp: In function 'int f(int, int, int, int)':
joi2019_ho_t3.cpp:18:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | if(c != 0 && r + 1 < R.size()) {
| ~~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:23:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | if(c != 1 && g + 1 < G.size()) {
| ~~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp:28:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
28 | if(c != 2 && y + 1 < Y.size()) {
| ~~~~~~^~~~~~~~~~
joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:58:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int r = 1; r < R.size(); r++) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:59:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int g = 1; g < G.size(); g++) {
| ~~^~~~~~~~~~
joi2019_ho_t3.cpp:60:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
60 | for(int y = 1; y < Y.size(); y++) {
| ~~^~~~~~~~~~
# | 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... |