답안 #1036544

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1036544 2024-07-27T13:45:40 Z model_code Sprinklers (CEOI24_sprinklers) C++17
100 / 100
97 ms 2764 KB
// Author: Robert Jaworski
#include <bits/stdc++.h>

using namespace std;

int dp(int N, int M, const vector<int>& s, const vector<int>& f, vector<int>& dir) {
    constexpr int DP_R = 0;
    constexpr int DP_LL = 1;
    constexpr int DP_RL = 2;
    constexpr int DP_SIZE = 3;

    vector<int> dp(DP_SIZE * (N + 1)); // min K to water all the plants until s[i]
    auto DP = [&] (int si, int conf) -> decltype(auto) {
        return dp[DP_SIZE + si * DP_SIZE + conf];
    };
    dp[DP_LL] = 0;
    dp[DP_R] = dp[DP_RL] = 0x7fffffff;

    auto WATER = [&] (int siR, int siL, int fi) {
        if (fi < 0) return 0;
        int R = 0 <= siR && siR < N ? f[fi] - s[siR] : 0x7fffffff;
        if (R < 0) R = 0x7fffffff;
        int L = 0 <= siL && siL < N ? s[siL] - f[fi] : 0x7fffffff;
        if (L < 0) L = 0x7fffffff;
        return min(R, L);
    };

    auto CHECK = [&] (int si, int dp1, int dp2, int fi, int pfi, int ppfi) {
        if (dp2 == DP_RL && dp1 != DP_R) return 0x7fffffff;
        if (dp2 == DP_LL && dp1 == DP_R) return 0x7fffffff;
        int k = DP(si-1, dp1);
        int r = dp1 == DP_R ? si-1 : dp1 == DP_RL ? si-2 : dp2 == DP_R ? si : -1;
        int l = dp2 != DP_R ? si : dp1 != DP_R ? si-1 : -1;
        for (int ffi = pfi+1; ffi <= fi; ffi++) {
            if (s[si] != f[ffi]) k = max(k, WATER(r, l, ffi));
        }
        if (dp2 == DP_RL && si-1 >= 0) {
            for (int dp3 = 0; dp3 < DP_SIZE; dp3++) {
                int k2 = DP(si-2, dp3);
                int r2 = dp3 == DP_R ? si-2 : dp3 == DP_RL ? si-3 : -1;
                for (int ffi = ppfi+1; ffi <= fi; ffi++) {
                    if (s[si] != f[ffi]) k2 = max(k2, WATER(r2, l, ffi));
                }
                k = min(k, k2);
            }
        }
        return k;
    };

    int si, fi, pfi, ppfi;
    for (si = 0, fi = pfi = ppfi = -1; si < N; si++) {
        while (fi + 1 < M && f[fi+1] <= s[si]) fi++;
        DP(si, DP_R) = DP(si, DP_LL) = DP(si, DP_RL) = 0x7fffffff;
        for (int dp1 = 0; dp1 < DP_SIZE; dp1++) {
            for (int dp2 = 0; dp2 < DP_SIZE; dp2++) {
                DP(si, dp2) = min(DP(si, dp2), CHECK(si, dp1, dp2, fi, pfi, ppfi));
            }
        }
        ppfi = pfi;
        pfi = fi;
    }

    fi = M - 1;
    int K = 0x7fffffff;
    int choice = -1;
    if (fi == pfi) {
        if (DP(si-1, DP_R) < K) {
            K = DP(si-1, DP_R);
            choice = DP_R;
        }
        if (DP(si-1, DP_RL) < K) {
            K = DP(si-1, DP_RL);
            choice = DP_RL;
        }
        if (DP(si-1, DP_LL) < K) {
            K = DP(si-1, DP_LL);
            choice = DP_LL;
        }
    }
    else {
        if (DP(si-1, DP_R) < K) {
            K = max(DP(si-1, DP_R), WATER(si-1, -1, fi));
            choice = DP_R;
        }
        if (DP(si-1, DP_RL) < K) {
            int k = max(DP(si-1, DP_RL), WATER(si-2, -1, fi));
            if (k < K) {
                K = k;
                choice = DP_RL;
            }
        }
    }

    if (K < 0x7fffffff) {
        dir.resize(N, 0);
        for (si = N; si-- > 0;) {
            switch (choice) {
            case DP_R:
                dir[si] = 1;
                if (DP(si-1, DP_R) <= K) choice = DP_R;
                else if (DP(si-1, DP_RL) <= K) choice = DP_RL;
                else if (DP(si-1, DP_LL) <= K) choice = DP_LL;
                else {
                    cerr << "Bad" << endl;
                }
                break;
            case DP_RL:
                dir[si] = 0;
                choice = DP_R;
                break;
            case DP_LL:
                dir[si] = 0;
                if (DP(si-1, DP_RL) <= K) choice = DP_RL;
                else if (DP(si-1, DP_LL) <= K) choice = DP_LL;
                else {
                    cerr << "Bad" << endl;
                }
                break;
            }
        }
    }

    return K;
}

int main() {
    // freopen("data/inputs/01_right_closed.in", "r", stdin);

    int N, M;
    cin >> N >> M;

    vector<int> s(N, 0);
    vector<int> f(M, 0);
    vector<int> conf;
    for (auto&& x : s) {
        cin >> x;
    }
    for (auto&& x : f) {
        cin >> x;
    }

    if (N == 1) {
        if (s.back() <= f[0]) {
            cout << (f.back() - s.back()) << endl << "R" << endl;
        }
        else if (s[0] >= f.back()) {
            cout << (s[0] - f[0]) << endl << "L" << endl;
        }
        else {
            cout << -1 << endl;
        }
        return 0;
    }

    int K = dp(N, M, s, f, conf);
    if (K == 0x7fffffff) {
        cout << -1 << endl;
        return 0;
    }

    cout << K << endl;
    for (auto&& x : conf) {
        cout << (x ? 'R' : 'L');
    }
    cout << endl;

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 37 ms 604 KB Correct
3 Correct 1 ms 344 KB Correct
4 Correct 26 ms 604 KB Correct
5 Correct 25 ms 604 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 9 ms 348 KB Correct
9 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 28 ms 840 KB Correct
3 Correct 5 ms 604 KB Correct
4 Correct 60 ms 2640 KB Correct
5 Correct 54 ms 2756 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 424 KB Correct
8 Correct 97 ms 2752 KB Correct
9 Correct 63 ms 2740 KB Correct
10 Correct 49 ms 2644 KB Correct
11 Correct 52 ms 2248 KB Correct
12 Correct 34 ms 1448 KB Correct
13 Correct 35 ms 2524 KB Correct
14 Correct 40 ms 2520 KB Correct
15 Correct 55 ms 2644 KB Correct
16 Correct 33 ms 2652 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 1 ms 344 KB Correct
4 Correct 0 ms 348 KB Correct
5 Correct 1 ms 348 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 1 ms 344 KB Correct
9 Correct 1 ms 348 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 0 ms 344 KB Correct
12 Correct 1 ms 348 KB Correct
13 Correct 0 ms 348 KB Correct
14 Correct 0 ms 348 KB Correct
15 Correct 0 ms 344 KB Correct
16 Correct 1 ms 600 KB Correct
17 Correct 0 ms 348 KB Correct
18 Correct 0 ms 348 KB Correct
19 Correct 1 ms 348 KB Correct
20 Correct 1 ms 360 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 33 ms 868 KB Correct
3 Correct 83 ms 2764 KB Correct
4 Correct 59 ms 2644 KB Correct
5 Correct 61 ms 2764 KB Correct
6 Correct 56 ms 2644 KB Correct
7 Correct 56 ms 2760 KB Correct
8 Correct 70 ms 2640 KB Correct
9 Correct 55 ms 2644 KB Correct
10 Correct 71 ms 2640 KB Correct
11 Correct 56 ms 2760 KB Correct
12 Correct 0 ms 348 KB Correct
13 Correct 0 ms 348 KB Correct
14 Correct 36 ms 2364 KB Correct
15 Correct 38 ms 2384 KB Correct
16 Correct 29 ms 2248 KB Correct
17 Correct 35 ms 2652 KB Correct
18 Correct 30 ms 2644 KB Correct
19 Correct 33 ms 2640 KB Correct
20 Correct 48 ms 2756 KB Correct
21 Correct 80 ms 2644 KB Correct
22 Correct 46 ms 2760 KB Correct
23 Correct 42 ms 2644 KB Correct
24 Correct 41 ms 2760 KB Correct
25 Correct 38 ms 2640 KB Correct
26 Correct 26 ms 1876 KB Correct
27 Correct 19 ms 1116 KB Correct
28 Correct 35 ms 2504 KB Correct
29 Correct 33 ms 2388 KB Correct
30 Correct 35 ms 2628 KB Correct
31 Correct 45 ms 2328 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 37 ms 604 KB Correct
4 Correct 1 ms 344 KB Correct
5 Correct 26 ms 604 KB Correct
6 Correct 25 ms 604 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 0 ms 348 KB Correct
9 Correct 9 ms 348 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 28 ms 840 KB Correct
12 Correct 5 ms 604 KB Correct
13 Correct 60 ms 2640 KB Correct
14 Correct 54 ms 2756 KB Correct
15 Correct 0 ms 348 KB Correct
16 Correct 0 ms 424 KB Correct
17 Correct 97 ms 2752 KB Correct
18 Correct 63 ms 2740 KB Correct
19 Correct 49 ms 2644 KB Correct
20 Correct 52 ms 2248 KB Correct
21 Correct 34 ms 1448 KB Correct
22 Correct 35 ms 2524 KB Correct
23 Correct 40 ms 2520 KB Correct
24 Correct 55 ms 2644 KB Correct
25 Correct 33 ms 2652 KB Correct
26 Correct 1 ms 344 KB Correct
27 Correct 0 ms 348 KB Correct
28 Correct 1 ms 348 KB Correct
29 Correct 0 ms 348 KB Correct
30 Correct 0 ms 348 KB Correct
31 Correct 1 ms 344 KB Correct
32 Correct 1 ms 348 KB Correct
33 Correct 0 ms 348 KB Correct
34 Correct 0 ms 344 KB Correct
35 Correct 1 ms 348 KB Correct
36 Correct 0 ms 348 KB Correct
37 Correct 0 ms 348 KB Correct
38 Correct 0 ms 344 KB Correct
39 Correct 1 ms 600 KB Correct
40 Correct 0 ms 348 KB Correct
41 Correct 0 ms 348 KB Correct
42 Correct 1 ms 348 KB Correct
43 Correct 1 ms 360 KB Correct
44 Correct 33 ms 868 KB Correct
45 Correct 83 ms 2764 KB Correct
46 Correct 59 ms 2644 KB Correct
47 Correct 61 ms 2764 KB Correct
48 Correct 56 ms 2644 KB Correct
49 Correct 56 ms 2760 KB Correct
50 Correct 70 ms 2640 KB Correct
51 Correct 55 ms 2644 KB Correct
52 Correct 71 ms 2640 KB Correct
53 Correct 56 ms 2760 KB Correct
54 Correct 0 ms 348 KB Correct
55 Correct 0 ms 348 KB Correct
56 Correct 36 ms 2364 KB Correct
57 Correct 38 ms 2384 KB Correct
58 Correct 29 ms 2248 KB Correct
59 Correct 35 ms 2652 KB Correct
60 Correct 30 ms 2644 KB Correct
61 Correct 33 ms 2640 KB Correct
62 Correct 48 ms 2756 KB Correct
63 Correct 80 ms 2644 KB Correct
64 Correct 46 ms 2760 KB Correct
65 Correct 42 ms 2644 KB Correct
66 Correct 41 ms 2760 KB Correct
67 Correct 38 ms 2640 KB Correct
68 Correct 26 ms 1876 KB Correct
69 Correct 19 ms 1116 KB Correct
70 Correct 35 ms 2504 KB Correct
71 Correct 33 ms 2388 KB Correct
72 Correct 35 ms 2628 KB Correct
73 Correct 45 ms 2328 KB Correct
74 Correct 26 ms 860 KB Correct
75 Correct 56 ms 2748 KB Correct
76 Correct 86 ms 2640 KB Correct
77 Correct 0 ms 348 KB Correct
78 Correct 53 ms 2752 KB Correct
79 Correct 52 ms 2644 KB Correct
80 Correct 91 ms 2744 KB Correct
81 Correct 29 ms 2132 KB Correct
82 Correct 38 ms 2264 KB Correct
83 Correct 29 ms 2380 KB Correct
84 Correct 15 ms 604 KB Correct
85 Correct 40 ms 2652 KB Correct
86 Correct 43 ms 2640 KB Correct
87 Correct 38 ms 2384 KB Correct
88 Correct 47 ms 604 KB Correct
89 Correct 45 ms 604 KB Correct