Submission #671391

# Submission time Handle Problem Language Result Execution time Memory
671391 2022-12-13T03:24:56 Z GrandTiger1729 Growing Vegetable is Fun 3 (JOI19_ho_t3) C++17
60 / 100
500 ms 17888 KB
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int key(char c){
    if (c == 'R') return 0;
    if (c == 'G') return 1;
    if (c == 'Y') return 2;
}

const int INF = 1e9 + 9;
int main(){
    cin.tie(0)->sync_with_stdio(0);
    int n; cin >> n;
    string s; cin >> s;
    vector<int> pos[3];
    for (int i = 0; i < n; i++){
        pos[key(s[i])].push_back(i);
    }
    vector<vector<int>> pref(n + 1, vector<int>(3));
    for (int i = 0; i < n; i++){
        for (int j = 0; j < 3; j++){
            pref[i + 1][j] += pref[i][j];
        }
        pref[i + 1][key(s[i])]++;
    }
    int R = pos[0].size(), G = pos[1].size(), Y = pos[2].size();
    auto val = [&](int t, int x, int k){
        return max(0, pref[x][t] - k);
    };
    vector<vector<vector<int>>> dp(n + 1, vector<vector<int>>(n + 1, vector<int>(3, INF)));
    dp[0][0][0] = dp[0][0][1] = dp[0][0][2] = 0;
    for (int t = 1; t <= n; t++){
        vector<vector<vector<int>>> tmp(n + 1, vector<vector<int>>(n + 1, vector<int>(3, INF)));
        for (int i = 1; i <= t && i <= R; i++){
            for (int j = 0; i + j <= t && j <= G; j++){
                int k = t - i - j;
                if (k <= Y)
                    tmp[i][j][0] = min(tmp[i][j][0], min(dp[i - 1][j][1], dp[i - 1][j][2]) + val(1, pos[0][i - 1], j) + val(2, pos[0][i - 1], k));
            }
        }
        for (int i = 0; i <= t && i <= R; i++){
            for (int j = 1; i + j <= t && j <= G; j++){
                int k = t - i - j;
                if (k <= Y)
                    tmp[i][j][1] = min(tmp[i][j][1], min(dp[i][j - 1][0], dp[i][j - 1][2]) + val(0, pos[1][j - 1], i) + val(2, pos[1][j - 1], k));
            }
        }
        for (int i = 0; i <= t && i <= R; i++){
            for (int j = 0; i + j <= t && j <= G; j++){
                int k = t - i - j;
                if (0 < k && k <= Y)
                    tmp[i][j][2] = min(tmp[i][j][2], min(dp[i][j][0], dp[i][j][1]) + val(0, pos[2][k - 1], i) + val(1, pos[2][k - 1], j));
            }
        }
        swap(dp, tmp);
    }
    int ans = min({dp[R][G][0], dp[R][G][1], dp[R][G][2]});
    cout << (ans == INF? -1: ans);
    return 0;
}

Compilation message

joi2019_ho_t3.cpp: In function 'int key(char)':
joi2019_ho_t3.cpp:10:1: warning: control reaches end of non-void function [-Wreturn-type]
   10 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 356 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 356 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 10 ms 724 KB Output is correct
19 Correct 10 ms 732 KB Output is correct
20 Correct 10 ms 632 KB Output is correct
21 Correct 10 ms 736 KB Output is correct
22 Correct 11 ms 732 KB Output is correct
23 Correct 10 ms 732 KB Output is correct
24 Correct 10 ms 744 KB Output is correct
25 Correct 10 ms 724 KB Output is correct
26 Correct 10 ms 736 KB Output is correct
27 Correct 11 ms 736 KB Output is correct
28 Correct 11 ms 736 KB Output is correct
29 Correct 10 ms 736 KB Output is correct
30 Correct 10 ms 724 KB Output is correct
31 Correct 10 ms 736 KB Output is correct
32 Correct 10 ms 724 KB Output is correct
33 Correct 10 ms 720 KB Output is correct
34 Correct 11 ms 720 KB Output is correct
35 Correct 9 ms 596 KB Output is correct
36 Correct 9 ms 724 KB Output is correct
37 Correct 9 ms 596 KB Output is correct
38 Correct 10 ms 736 KB Output is correct
39 Correct 10 ms 732 KB Output is correct
40 Correct 10 ms 716 KB Output is correct
41 Correct 10 ms 736 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Execution timed out 1081 ms 17888 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 0 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 1 ms 292 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 1 ms 356 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 10 ms 724 KB Output is correct
19 Correct 10 ms 732 KB Output is correct
20 Correct 10 ms 632 KB Output is correct
21 Correct 10 ms 736 KB Output is correct
22 Correct 11 ms 732 KB Output is correct
23 Correct 10 ms 732 KB Output is correct
24 Correct 10 ms 744 KB Output is correct
25 Correct 10 ms 724 KB Output is correct
26 Correct 10 ms 736 KB Output is correct
27 Correct 11 ms 736 KB Output is correct
28 Correct 11 ms 736 KB Output is correct
29 Correct 10 ms 736 KB Output is correct
30 Correct 10 ms 724 KB Output is correct
31 Correct 10 ms 736 KB Output is correct
32 Correct 10 ms 724 KB Output is correct
33 Correct 10 ms 720 KB Output is correct
34 Correct 11 ms 720 KB Output is correct
35 Correct 9 ms 596 KB Output is correct
36 Correct 9 ms 724 KB Output is correct
37 Correct 9 ms 596 KB Output is correct
38 Correct 10 ms 736 KB Output is correct
39 Correct 10 ms 732 KB Output is correct
40 Correct 10 ms 716 KB Output is correct
41 Correct 10 ms 736 KB Output is correct
42 Correct 0 ms 212 KB Output is correct
43 Execution timed out 1081 ms 17888 KB Time limit exceeded
44 Halted 0 ms 0 KB -