답안 #1036537

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

using namespace std;

constexpr int DP_RANGE = 3;
constexpr int DP_SIZE = 1 << (2 * DP_RANGE);

void dp(int N, int M, const vector<int>& s, const vector<int>& f) {
    vector<int> dir(N, -1);
    int K = 0;

    vector<int> dp(DP_SIZE + M*DP_SIZE, -1);
    auto DP = [&] (int fi, int conf) -> int& {
        return dp[DP_SIZE + fi * DP_SIZE + conf];
    };

    auto COVER = [&] (int fi, int si, int conf) {
        int k = 0x7fffffff;
        if (si >= 0 && s[si] == f[fi]) return 0;
        for (int j = 0; j < DP_RANGE; j++) {
            int i = si-DP_RANGE+1 + j;
            if (i >= 0 && (conf & (1 << j)) != 0) {
                k = min(k, f[fi] - s[i]);
            }
        }
        for (int j = DP_RANGE; j < 2*DP_RANGE; j++) {
            int i = si-DP_RANGE+1 + j;
            if (i < N && (conf & (1 << j)) == 0) {
                k = min(k, s[i] - f[fi]);
            }
        }
        return k;
    };

    auto DO_COVER = [&] (int fi, int si, int conf) {
        for (int j = 0; j < 2*DP_RANGE; j++) {
            int i = si-DP_RANGE+1 + j;
            if (0 <= i && i < N) {
                if (dir[i] != -1 && (dir[i] == 0) != ((conf & (1 << j)) == 0)) {
                    cerr << "Bad cover " << i << " " << s[i] << endl;
                }
                dir[i] = conf & (1 << j);
            }
        }
    };

    auto PREV = [&] (int fi, int ds, int conf) {
        if (!ds) return DP(fi-1, conf);
        int k = 0x7fffffff;
        for (int c = 0; c < DP_SIZE; c++) {
            if (ds > 2*DP_RANGE || (c >> ds) == (conf & ((DP_SIZE-1) >> ds))) {
                k = min(k, DP(fi-1, c));
            }
        }
        return k;
    };

    for (int fi = 0, si = -1, psi = -1; fi < M; fi++) {
        while (si + 1 < N && s[si+1] <= f[fi]) si++;
        for (int conf = 0; conf < DP_SIZE; conf++) {
            DP(fi, conf) = COVER(fi, si, conf);
            DP(fi, conf) = max(DP(fi, conf), PREV(fi, si - psi, conf));
        }
        psi = si;
    }

    for (int fi = M, si = N-1, c=0, cm=0; fi-- > 0;) {
        while (si >= 0 && s[si] > f[fi]) {
            si--;
            c <<= 1;
            cm &= cm - 1;
        }
        int minK = 0x7fffffff, minC = 0;
        for (int conf = 0; conf < DP_SIZE; conf++) {
            if ((conf & cm) == (c & cm)) {
                if (DP(fi, conf) < minK) {
                    minK = DP(fi, conf);
                    minC = conf;
                }
            }
        }
        DO_COVER(fi, si, minC);
        c = minC;
        cm = DP_SIZE-1;
        K = max(K, minK);
    }

    if (K == 0x7fffffff) {
        cout << -1 << endl;
        return;
    }

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

int main() {
    int N, M;
    cin >> N >> M;

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

    dp(N, M, s, f);

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 54 ms 21536 KB Correct
3 Correct 1 ms 348 KB Correct
4 Correct 75 ms 25692 KB Correct
5 Correct 67 ms 25692 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 14 ms 5212 KB Correct
9 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 102 ms 25680 KB Correct
3 Correct 16 ms 1628 KB Correct
4 Correct 226 ms 26708 KB Correct
5 Correct 231 ms 26568 KB Correct
6 Correct 0 ms 344 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 77 ms 26560 KB Correct
9 Correct 108 ms 26704 KB Correct
10 Correct 198 ms 26708 KB Correct
11 Correct 25 ms 1116 KB Correct
12 Correct 132 ms 23376 KB Correct
13 Correct 200 ms 18176 KB Correct
14 Correct 186 ms 18244 KB Correct
15 Correct 191 ms 18260 KB Correct
16 Correct 170 ms 18256 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 1 ms 344 KB Correct
4 Correct 1 ms 348 KB Correct
5 Correct 1 ms 600 KB Correct
6 Correct 1 ms 604 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 1 ms 604 KB Correct
9 Correct 1 ms 604 KB Correct
10 Correct 1 ms 604 KB Correct
11 Correct 0 ms 348 KB Correct
12 Correct 1 ms 604 KB Correct
13 Correct 1 ms 604 KB Correct
14 Correct 1 ms 348 KB Correct
15 Correct 1 ms 604 KB Correct
16 Correct 1 ms 604 KB Correct
17 Correct 1 ms 604 KB Correct
18 Correct 1 ms 604 KB Correct
19 Correct 1 ms 604 KB Correct
20 Correct 2 ms 600 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 110 ms 25904 KB Correct
3 Correct 357 ms 26708 KB Correct
4 Correct 358 ms 26568 KB Correct
5 Correct 342 ms 26708 KB Correct
6 Correct 330 ms 26576 KB Correct
7 Correct 342 ms 26708 KB Correct
8 Correct 339 ms 26564 KB Correct
9 Correct 358 ms 26648 KB Correct
10 Correct 337 ms 26568 KB Correct
11 Correct 368 ms 26656 KB Correct
12 Correct 0 ms 348 KB Correct
13 Correct 0 ms 348 KB Correct
14 Correct 26 ms 1116 KB Correct
15 Correct 37 ms 1544 KB Correct
16 Correct 39 ms 1364 KB Correct
17 Correct 51 ms 26668 KB Correct
18 Correct 83 ms 26712 KB Correct
19 Correct 85 ms 26568 KB Correct
20 Correct 359 ms 26704 KB Correct
21 Correct 383 ms 26704 KB Correct
22 Correct 365 ms 26768 KB Correct
23 Correct 385 ms 26660 KB Correct
24 Correct 288 ms 26704 KB Correct
25 Correct 297 ms 26572 KB Correct
26 Correct 77 ms 3464 KB Correct
27 Correct 138 ms 8132 KB Correct
28 Correct 304 ms 18192 KB Correct
29 Correct 207 ms 18236 KB Correct
30 Correct 277 ms 18172 KB Correct
31 Correct 345 ms 26452 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 54 ms 21536 KB Correct
4 Correct 1 ms 348 KB Correct
5 Correct 75 ms 25692 KB Correct
6 Correct 67 ms 25692 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 0 ms 348 KB Correct
9 Correct 14 ms 5212 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 102 ms 25680 KB Correct
12 Correct 16 ms 1628 KB Correct
13 Correct 226 ms 26708 KB Correct
14 Correct 231 ms 26568 KB Correct
15 Correct 0 ms 344 KB Correct
16 Correct 0 ms 348 KB Correct
17 Correct 77 ms 26560 KB Correct
18 Correct 108 ms 26704 KB Correct
19 Correct 198 ms 26708 KB Correct
20 Correct 25 ms 1116 KB Correct
21 Correct 132 ms 23376 KB Correct
22 Correct 200 ms 18176 KB Correct
23 Correct 186 ms 18244 KB Correct
24 Correct 191 ms 18260 KB Correct
25 Correct 170 ms 18256 KB Correct
26 Correct 1 ms 344 KB Correct
27 Correct 1 ms 348 KB Correct
28 Correct 1 ms 600 KB Correct
29 Correct 1 ms 604 KB Correct
30 Correct 0 ms 348 KB Correct
31 Correct 1 ms 604 KB Correct
32 Correct 1 ms 604 KB Correct
33 Correct 1 ms 604 KB Correct
34 Correct 0 ms 348 KB Correct
35 Correct 1 ms 604 KB Correct
36 Correct 1 ms 604 KB Correct
37 Correct 1 ms 348 KB Correct
38 Correct 1 ms 604 KB Correct
39 Correct 1 ms 604 KB Correct
40 Correct 1 ms 604 KB Correct
41 Correct 1 ms 604 KB Correct
42 Correct 1 ms 604 KB Correct
43 Correct 2 ms 600 KB Correct
44 Correct 110 ms 25904 KB Correct
45 Correct 357 ms 26708 KB Correct
46 Correct 358 ms 26568 KB Correct
47 Correct 342 ms 26708 KB Correct
48 Correct 330 ms 26576 KB Correct
49 Correct 342 ms 26708 KB Correct
50 Correct 339 ms 26564 KB Correct
51 Correct 358 ms 26648 KB Correct
52 Correct 337 ms 26568 KB Correct
53 Correct 368 ms 26656 KB Correct
54 Correct 0 ms 348 KB Correct
55 Correct 0 ms 348 KB Correct
56 Correct 26 ms 1116 KB Correct
57 Correct 37 ms 1544 KB Correct
58 Correct 39 ms 1364 KB Correct
59 Correct 51 ms 26668 KB Correct
60 Correct 83 ms 26712 KB Correct
61 Correct 85 ms 26568 KB Correct
62 Correct 359 ms 26704 KB Correct
63 Correct 383 ms 26704 KB Correct
64 Correct 365 ms 26768 KB Correct
65 Correct 385 ms 26660 KB Correct
66 Correct 288 ms 26704 KB Correct
67 Correct 297 ms 26572 KB Correct
68 Correct 77 ms 3464 KB Correct
69 Correct 138 ms 8132 KB Correct
70 Correct 304 ms 18192 KB Correct
71 Correct 207 ms 18236 KB Correct
72 Correct 277 ms 18172 KB Correct
73 Correct 345 ms 26452 KB Correct
74 Correct 92 ms 25880 KB Correct
75 Correct 337 ms 26652 KB Correct
76 Correct 341 ms 26644 KB Correct
77 Correct 0 ms 348 KB Correct
78 Correct 96 ms 26652 KB Correct
79 Correct 121 ms 26648 KB Correct
80 Correct 268 ms 26560 KB Correct
81 Correct 48 ms 1080 KB Correct
82 Correct 31 ms 1372 KB Correct
83 Correct 31 ms 1372 KB Correct
84 Correct 64 ms 14168 KB Correct
85 Correct 264 ms 18244 KB Correct
86 Correct 274 ms 18260 KB Correct
87 Correct 366 ms 26448 KB Correct
88 Correct 81 ms 25668 KB Correct
89 Correct 74 ms 25684 KB Correct