Submission #591272

# Submission time Handle Problem Language Result Execution time Memory
591272 2022-07-07T08:08:10 Z piOOE Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
100 / 100
312 ms 60068 KB
#include <bits/stdc++.h>

using namespace std;

using ll = long long;

vector<vector<vector<int>>> dp[3];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n;
    cin >> n;
    string S;
    cin >> S;
    vector<int> a(n);
    array<int, 3> cnt{};
    vector<vector<int>> g(3);
    for (int i = 0; i < n; ++i) {
        a[i] = S[i] == 'R' ? 0 : S[i] == 'G' ? 1 : 2;
        ++cnt[a[i]];
        g[a[i]].push_back(i);
    }
    if (max({cnt[0], cnt[1], cnt[2]}) > (n + 1) / 2) {
        cout << -1;
        return 0;
    }
    vector<vector<vector<array<int, 3>>>> pl(cnt[0] + 1, vector<vector<array<int, 3>>>(cnt[1] + 1,
                                                                                       vector<array<int, 3>>(
                                                                                               cnt[2] + 1)));
    pl[0][0][0] = {-1, -1, -1};
    for (int t = 0; t < 3; ++t) {
        reverse(g[t].begin(), g[t].end());
        dp[t] = vector<vector<vector<int>>>(cnt[0] + 1, vector<vector<int>>(cnt[1] + 1, vector<int>(cnt[2] + 1)));
    }
    for (int sum = 1; sum <= n; ++sum) {
        for (int f = 0; f <= cnt[0]; ++f) {
            for (int s = 0; s <= cnt[1]; ++s) {
                if (f + s > sum) {
                    break;
                }
                int t = sum - f - s;
                if (t > cnt[2]) {
                    continue;
                }
                array<int, 3> temp = {f == 0 ? n + 1 : g[0][f - 1], s == 0 ? n + 1 : g[1][s - 1],
                                      t == 0 ? n + 1 : g[2][t - 1]};
                int idx = min_element(temp.begin(), temp.end()) - temp.begin();
                if (idx == 0) {
                    pl[f][s][t] = pl[f - 1][s][t];
                } else if (idx == 1) {
                    pl[f][s][t] = pl[f][s - 1][t];
                } else {
                    pl[f][s][t] = pl[f][s][t - 1];
                }
                for (int k = 0; k < 3; ++k) if (pl[f][s][t][k] != -1) pl[f][s][t][k] += 1;
                pl[f][s][t][idx] = 0;
                array<int, 3> num = {f, s, t};
                if (sum != n) {
                    array<int, 3> k = pl[f][s][t];
                    for (int p = 0; p < 3; ++p) {
                        dp[p][f][s][t] = 1000'000'000;
                        for (int nxt = 0; nxt < 3; ++nxt) {
                            if (p == nxt || num[nxt] == 0) {
                                continue;
                            }
                            int ans = dp[nxt][f - (nxt == 0)][s - (nxt == 1)][t - (nxt == 2)] + pl[f][s][t][nxt];
                            if (dp[p][f][s][t] > ans) {
                                dp[p][f][s][t] = ans;
                            }
                        }
                    }
                }
            }
        }
    }
    int ans = 1000'000'000;
    int f = cnt[0], s = cnt[1], t = cnt[2];
    array<int, 3> k = pl[f][s][t];
    for (int nxt = 0; nxt < 3; ++nxt) {
        if (cnt[nxt] == 0) {
            continue;
        }
        int val = dp[nxt][f - (nxt == 0)][s - (nxt == 1)][t - (nxt == 2)] + pl[f][s][t][nxt];
        ans = min(ans, val);
    }
    cout << ans;
    return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int main()':
joi2019_ho_t3.cpp:60:35: warning: variable 'k' set but not used [-Wunused-but-set-variable]
   60 |                     array<int, 3> k = pl[f][s][t];
      |                                   ^
joi2019_ho_t3.cpp:79:19: warning: variable 'k' set but not used [-Wunused-but-set-variable]
   79 |     array<int, 3> k = pl[f][s][t];
      |                   ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 576 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 452 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 572 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 580 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 22 ms 9172 KB Output is correct
3 Correct 21 ms 9172 KB Output is correct
4 Correct 21 ms 9148 KB Output is correct
5 Correct 21 ms 9224 KB Output is correct
6 Correct 21 ms 9196 KB Output is correct
7 Correct 26 ms 9116 KB Output is correct
8 Correct 21 ms 9152 KB Output is correct
9 Correct 22 ms 9044 KB Output is correct
10 Correct 21 ms 9220 KB Output is correct
11 Correct 22 ms 9220 KB Output is correct
12 Correct 5 ms 2644 KB Output is correct
13 Correct 11 ms 4436 KB Output is correct
14 Correct 17 ms 6400 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 212 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 316 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 316 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 0 ms 212 KB Output is correct
18 Correct 1 ms 596 KB Output is correct
19 Correct 1 ms 596 KB Output is correct
20 Correct 1 ms 576 KB Output is correct
21 Correct 2 ms 596 KB Output is correct
22 Correct 1 ms 468 KB Output is correct
23 Correct 1 ms 468 KB Output is correct
24 Correct 1 ms 452 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 596 KB Output is correct
28 Correct 1 ms 596 KB Output is correct
29 Correct 1 ms 572 KB Output is correct
30 Correct 1 ms 596 KB Output is correct
31 Correct 1 ms 596 KB Output is correct
32 Correct 1 ms 596 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 212 KB Output is correct
35 Correct 1 ms 468 KB Output is correct
36 Correct 1 ms 468 KB Output is correct
37 Correct 1 ms 468 KB Output is correct
38 Correct 1 ms 468 KB Output is correct
39 Correct 1 ms 580 KB Output is correct
40 Correct 1 ms 212 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 1 ms 212 KB Output is correct
43 Correct 22 ms 9172 KB Output is correct
44 Correct 21 ms 9172 KB Output is correct
45 Correct 21 ms 9148 KB Output is correct
46 Correct 21 ms 9224 KB Output is correct
47 Correct 21 ms 9196 KB Output is correct
48 Correct 26 ms 9116 KB Output is correct
49 Correct 21 ms 9152 KB Output is correct
50 Correct 22 ms 9044 KB Output is correct
51 Correct 21 ms 9220 KB Output is correct
52 Correct 22 ms 9220 KB Output is correct
53 Correct 5 ms 2644 KB Output is correct
54 Correct 11 ms 4436 KB Output is correct
55 Correct 17 ms 6400 KB Output is correct
56 Correct 0 ms 212 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 270 ms 59300 KB Output is correct
59 Correct 266 ms 60032 KB Output is correct
60 Correct 279 ms 58552 KB Output is correct
61 Correct 274 ms 59680 KB Output is correct
62 Correct 1 ms 212 KB Output is correct
63 Correct 37 ms 14320 KB Output is correct
64 Correct 89 ms 33644 KB Output is correct
65 Correct 133 ms 44172 KB Output is correct
66 Correct 312 ms 58860 KB Output is correct
67 Correct 253 ms 58700 KB Output is correct
68 Correct 292 ms 59180 KB Output is correct
69 Correct 287 ms 60068 KB Output is correct
70 Correct 272 ms 59936 KB Output is correct
71 Correct 253 ms 58676 KB Output is correct
72 Correct 0 ms 212 KB Output is correct
73 Correct 1 ms 320 KB Output is correct