Submission #1088536

# Submission time Handle Problem Language Result Execution time Memory
1088536 2024-09-14T15:01:28 Z efishel Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
215 ms 230240 KB
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector <ll>;
using vi = vector <int>;

vi ou[3];
bool vis[403][403][403][3];
int dp[403][403][403][3]; // dp[a][b][c][last] = min operations to have a valid string\
rn having a prefix of length a+b+c having taken a as, b bs, and c cs, having the last color as last
bool vis2[403][403][403][3];
int dp2[403][403][403][3];

int fcost (int a, int b, int c, int wh) { // calculates the followisg sum:
/*
    for (ll j = 0; j < a; j++) if (ou[0][j] < ou[wh][t]) ans++;
    for (ll j = 0; j < b; j++) if (ou[1][j] < ou[wh][t]) ans++;
    for (ll j = 0; j < c; j++) if (ou[2][j] < ou[wh][t]) ans++;
*/
    if (vis2[a][b][c][wh]) return dp2[a][b][c][wh];
    vis2[a][b][c][wh] = true;
    int t = wh == 0 ? a : wh == 1 ? b : c;
    if ((a ? ou[0][a-1] : -1) >= ou[wh][t]) return dp2[a][b][c][wh] = fcost(a-1, b, c, wh);
    if ((b ? ou[1][b-1] : -1) >= ou[wh][t]) return dp2[a][b][c][wh] = fcost(a, b-1, c, wh);
    if ((c ? ou[2][c-1] : -1) >= ou[wh][t]) return dp2[a][b][c][wh] = fcost(a, b, c-1, wh);
    return dp2[a][b][c][wh] = a+b+c;
}

ll n;
int bey[3]; // bey[i] = how many taken are beyond the last taken of color i
int ptr[3][3];
// bool taken[403];
int solve (ll a, ll b, ll c, ll last) { // have taken the first a as b bs and c cs, the last rn is last
    if (a+b+c == n) return 0;
    if (vis[a][b][c][last]) return dp[a][b][c][last];
    vis[a][b][c][last] = true;
    int &ans = dp[a][b][c][last];
    ans = 1e9;
    // int i = a+b+c;
    // ll lsa = a < ou[0].size() ? ou[0][a] : n;
    // ll lsb = b < ou[1].size() ? ou[1][b] : n;
    // ll lsc = c < ou[2].size() ? ou[2][c] : n;
    // auto costFor = [&](ll col, ll t) {
    //     int ans = 0;
    //     ptr[col][0] = 0;
    //     // while (ptr[col][0] >= 0 && ou[0][ptr[col][0]] > ou[col][t]) ptr[col][0]--;
    //     while (ptr[col][0] < ou[0].size() && ou[0][ptr[col][0]] < ou[col][t]) ptr[col][0]++;
    //     ans += min((int)a, ptr[col][0]);
    //     // for (ll j = 0; j < a; j++) if (ou[0][j] < ou[col][t]) ans++;
    //     for (ll j = 0; j < a; j++) if (ou[0][j] < ou[col][t]) ans++;
    //     for (ll j = 0; j < b; j++) if (ou[1][j] < ou[col][t]) ans++;
    //     for (ll j = 0; j < c; j++) if (ou[2][j] < ou[col][t]) ans++;
    //     return ans;
    // };
    if (last != 0 && a < ou[0].size()) { ans = min(ans, solve(a+1, b, c, 0) + ou[0][a] - fcost(a, b, c, 0)); }
    if (last != 1 && b < ou[1].size()) { ans = min(ans, solve(a, b+1, c, 1) + ou[1][b] - fcost(a, b, c, 1)); }
    if (last != 2 && c < ou[2].size()) { ans = min(ans, solve(a, b, c+1, 2) + ou[2][c] - fcost(a, b, c, 2)); }
    return ans;
}

int main () {
    cin.tie(nullptr) -> sync_with_stdio(false);
    cin >> n;
    string str;
    cin >> str;
    // ou[0] = ou[1] = ou[2] = { -1 };
    for (ll i = 0; i < n; i++) {
        ou[(str[i] == 'R' ? 0 : str[i] == 'G' ? 1 : 2)].push_back(i);
    }
    ll ans = min({ solve(0, 0, 0, 0), solve(0, 0, 0, 1), solve(0, 0, 0, 2) });
    cout << (ans == 1e9 ? -1 : ans) << '\n';
    return 0;
}

Compilation message

joi2019_ho_t3.cpp:9:27: warning: multi-line comment [-Wcomment]
    9 | int dp[403][403][403][3]; // dp[a][b][c][last] = min operations to have a valid string\
      |                           ^
joi2019_ho_t3.cpp: In function 'int solve(ll, ll, ll, ll)':
joi2019_ho_t3.cpp:55:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   55 |     if (last != 0 && a < ou[0].size()) { ans = min(ans, solve(a+1, b, c, 0) + ou[0][a] - fcost(a, b, c, 0)); }
      |                      ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:56:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   56 |     if (last != 1 && b < ou[1].size()) { ans = min(ans, solve(a, b+1, c, 1) + ou[1][b] - fcost(a, b, c, 1)); }
      |                      ~~^~~~~~~~~~~~~~
joi2019_ho_t3.cpp:57:24: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   57 |     if (last != 2 && c < ou[2].size()) { ans = min(ans, solve(a, b, c+1, 2) + ou[2][c] - fcost(a, b, c, 2)); }
      |                      ~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 0 ms 860 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 0 ms 860 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 4 ms 5468 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5720 KB Output is correct
22 Correct 2 ms 3932 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 1 ms 3164 KB Output is correct
26 Correct 2 ms 4956 KB Output is correct
27 Correct 3 ms 5976 KB Output is correct
28 Correct 3 ms 5468 KB Output is correct
29 Correct 3 ms 5468 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 5468 KB Output is correct
33 Correct 2 ms 4188 KB Output is correct
34 Correct 2 ms 4444 KB Output is correct
35 Correct 3 ms 5808 KB Output is correct
36 Correct 3 ms 4440 KB Output is correct
37 Correct 2 ms 3932 KB Output is correct
38 Correct 2 ms 4700 KB Output is correct
39 Correct 3 ms 5468 KB Output is correct
40 Correct 0 ms 604 KB Output is correct
41 Correct 1 ms 1884 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 38 ms 111676 KB Output is correct
3 Correct 40 ms 111496 KB Output is correct
4 Correct 6 ms 12636 KB Output is correct
5 Correct 7 ms 16732 KB Output is correct
6 Correct 6 ms 15056 KB Output is correct
7 Correct 5 ms 12380 KB Output is correct
8 Correct 8 ms 19036 KB Output is correct
9 Correct 7 ms 14788 KB Output is correct
10 Correct 5 ms 12732 KB Output is correct
11 Correct 6 ms 14396 KB Output is correct
12 Correct 2 ms 6060 KB Output is correct
13 Correct 4 ms 9564 KB Output is correct
14 Correct 6 ms 14432 KB Output is correct
15 Correct 5 ms 11356 KB Output is correct
16 Correct 5 ms 12052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 604 KB Output is correct
5 Correct 0 ms 604 KB Output is correct
6 Correct 1 ms 860 KB Output is correct
7 Correct 1 ms 860 KB Output is correct
8 Correct 1 ms 856 KB Output is correct
9 Correct 1 ms 860 KB Output is correct
10 Correct 0 ms 604 KB Output is correct
11 Correct 1 ms 856 KB Output is correct
12 Correct 0 ms 860 KB Output is correct
13 Correct 0 ms 604 KB Output is correct
14 Correct 1 ms 860 KB Output is correct
15 Correct 1 ms 604 KB Output is correct
16 Correct 0 ms 604 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 4 ms 5468 KB Output is correct
19 Correct 3 ms 4444 KB Output is correct
20 Correct 3 ms 5212 KB Output is correct
21 Correct 3 ms 5720 KB Output is correct
22 Correct 2 ms 3932 KB Output is correct
23 Correct 3 ms 4952 KB Output is correct
24 Correct 2 ms 3676 KB Output is correct
25 Correct 1 ms 3164 KB Output is correct
26 Correct 2 ms 4956 KB Output is correct
27 Correct 3 ms 5976 KB Output is correct
28 Correct 3 ms 5468 KB Output is correct
29 Correct 3 ms 5468 KB Output is correct
30 Correct 3 ms 5212 KB Output is correct
31 Correct 2 ms 4700 KB Output is correct
32 Correct 2 ms 5468 KB Output is correct
33 Correct 2 ms 4188 KB Output is correct
34 Correct 2 ms 4444 KB Output is correct
35 Correct 3 ms 5808 KB Output is correct
36 Correct 3 ms 4440 KB Output is correct
37 Correct 2 ms 3932 KB Output is correct
38 Correct 2 ms 4700 KB Output is correct
39 Correct 3 ms 5468 KB Output is correct
40 Correct 0 ms 604 KB Output is correct
41 Correct 1 ms 1884 KB Output is correct
42 Correct 0 ms 348 KB Output is correct
43 Correct 38 ms 111676 KB Output is correct
44 Correct 40 ms 111496 KB Output is correct
45 Correct 6 ms 12636 KB Output is correct
46 Correct 7 ms 16732 KB Output is correct
47 Correct 6 ms 15056 KB Output is correct
48 Correct 5 ms 12380 KB Output is correct
49 Correct 8 ms 19036 KB Output is correct
50 Correct 7 ms 14788 KB Output is correct
51 Correct 5 ms 12732 KB Output is correct
52 Correct 6 ms 14396 KB Output is correct
53 Correct 2 ms 6060 KB Output is correct
54 Correct 4 ms 9564 KB Output is correct
55 Correct 6 ms 14432 KB Output is correct
56 Correct 5 ms 11356 KB Output is correct
57 Correct 5 ms 12052 KB Output is correct
58 Correct 176 ms 186960 KB Output is correct
59 Correct 215 ms 230240 KB Output is correct
60 Correct 193 ms 213968 KB Output is correct
61 Correct 201 ms 208000 KB Output is correct
62 Correct 39 ms 103256 KB Output is correct
63 Correct 66 ms 160992 KB Output is correct
64 Correct 94 ms 189008 KB Output is correct
65 Correct 124 ms 220412 KB Output is correct
66 Correct 209 ms 211620 KB Output is correct
67 Correct 205 ms 202628 KB Output is correct
68 Correct 197 ms 217636 KB Output is correct
69 Correct 202 ms 209748 KB Output is correct
70 Correct 209 ms 225104 KB Output is correct
71 Correct 209 ms 204532 KB Output is correct
72 Correct 28 ms 29780 KB Output is correct
73 Correct 3 ms 6488 KB Output is correct