# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
272958 | ChrisT | Growing Vegetable is Fun 3 (JOI19_ho_t3) | C++17 | 531 ms | 133368 KiB |
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;
void uin (int &a, int b) {
if (b < a) a = b;
}
int main() {
int n;
scanf ("%d",&n);
vector<vector<int>> pos(3); vector<int> done(3);
for (int i = 1; i <= n; i++) {
char ch; scanf (" %c",&ch);
int a = (ch == 'R' ? 0 : (ch == 'G' ? 1 : 2));
pos[a].push_back(i);
}
int a = (int)pos[0].size(), b = (int)pos[1].size(), c = (int)pos[2].size();
vector<vector<vector<vector<int>>>> dp(a+1,vector<vector<vector<int>>>(b+1,vector<vector<int>>(c+1,vector<int>(3,1e9))));
dp[0][0][0][0] = dp[0][0][0][1] = dp[0][0][0][2] = 0;
for (done[0] = 0; done[0] <= a; done[0]++) {
for (done[1] = 0; done[1] <= b; done[1]++) {
for (done[2] = 0; done[2] <= c; done[2]++) if (done[0] || done[1] || done[2]){
for (int take = 0; take <= 2; take++) if (done[take]) {
int idx = pos[take][done[take]-1], add = 0;
for (int j = 0; j <= 2; j++) if (j != take) {
int big = upper_bound(pos[j].begin(),pos[j].end(),idx) - pos[j].begin() + 1;
add += max(0,done[j] - big + 1);
}
for (int lst = 0; lst <= 2; lst++) if (lst != take) {
uin(dp[done[0]][done[1]][done[2]][take],dp[done[0]-(take==0)][done[1]-(take==1)][done[2]-(take==2)][lst] + add);
}
}
}
}
}
int ret = 1e9;
for (int i = 0; i < 3; i++) uin(ret,dp[a][b][c][i]);
printf ("%d\n",ret>1e8?-1:ret);
return 0;
}
Compilation message (stderr)
# | 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... |