답안 #1053172

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1053172 2024-08-11T09:23:02 Z elazarkoren Sprinklers (CEOI24_sprinklers) C++17
100 / 100
42 ms 5180 KB
#include <bits/stdc++.h>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;

const int MAX_N = 1e5 + 5;

ll s[MAX_N], f[MAX_N];
int ri[MAX_N], flip[MAX_N];
int n, m;

bool Solve(int k) {
    int i = 0, j = 0;
    vi leftest(n);
    while (i < n && j < m) {
        flip[i] = 0;
        if (f[j] >= s[i]) {
            leftest[i] = -1;
            while (j < m && f[j] - s[i] <= k) j++;
            ri[i] = 1;
            i++;
            continue;
        }
        if (s[i] - f[j] > k) return 0;
        ri[i] = 0;
        leftest[i] = j;
        if (i && leftest[i - 1] != -1 && s[i] - f[leftest[i - 1]] <= k) {
            flip[i] = 1;
            while (j < m && f[j] - s[i - 1] <= k) j++;
            ri[i - 1] = 1;
        }
        while (j < m && f[j] <= s[i] && s[i] - f[j] <= k) j++;
        i++;
    }
    if (j != m) return 0;
    for (i = n - 1; i >= 0; i--) {
        if (flip[i]) {
            for (j = i; flip[j - 1]; j--);
            j--;
            for (int a = i, b = 0; a >= j; a--, b ^= 1) ri[a] = b;
            i = j + 1;
        }
    }
    return 1;
}

int Solve2(int k) {
    for (int i = 0; i < (1 << n); i++) {
        vii ranges;
        for (int j = 0; j < n; j++) {
            if ((i >> j) & 1) ranges.push_back({s[j], s[j] + k});
            else ranges.push_back({s[j] - k, s[j]});
        }
        sort(all(ranges));
        int a = 0, b = 0;
        while (a < n && b < m) {
            if (ranges[a].x <= f[b] && f[b] <= ranges[a].y) b++;
            else a++;
        }
        if (b == m) {
            return i + 1;
        }
    }
    return 0;
}

int Ans() {
    int begin = 0, end = 1e9 + 1;
    while (begin < end) {
        int mid = (begin + end) >> 1;
//        bool b1 = Solve(mid), b2 = Solve2(mid);
//        if (b1 != b2) {
//            cout << "BAD\n";
//            cout << n << ' ' << m << '\n';
//            for (int i = 0; i < n; i++) cout << s[i] << ' ';
//            cout << '\n';
//            for (int i = 0; i < m; i++) cout << f[i] << ' ';
//            exit(0);
//        }
        if (Solve(mid)) end = mid;
        else begin = mid + 1;
    }
    return end;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n >> m;
//    while (true) {
//        for (int i = 0; i < n; i++) {
//            s[i] = rand();
//        }
//        for (int i = 0; i < m; i++) {
//            f[i] = rand();
//        }
//        sort(s, s + n);
//        sort(f, f + m);
//        Ans();
//    }
    for (int i = 0; i < n; i++) cin >> s[i];
    for (int i = 0; i < m; i++) cin >> f[i];

    int end = Ans();
//    for (end = 0; end <= 8; end++) {
//        if (Solve(end)) break;
//    }
    int x = Solve(end);
    if (!x) {
        cout << -1 << '\n';
        return 0;
    }
//    x--;
//    for (int i = 0; i < n; i++) {
//        cout << ((x >> i) & 1 ? 'R' : 'L');
//    }
    cout << end << '\n';
    for (int i = 0; i < n; i++) {
        cout << (ri[i] ? 'R' : 'L');
    }
}
/*
3 5
6 14 25
0 13 19 21 28
 */
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 0 ms 2396 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 2396 KB Correct
2 Correct 6 ms 2396 KB Correct
3 Correct 0 ms 2396 KB Correct
4 Correct 6 ms 2516 KB Correct
5 Correct 6 ms 2520 KB Correct
6 Correct 0 ms 2396 KB Correct
7 Correct 0 ms 2436 KB Correct
8 Correct 2 ms 2396 KB Correct
9 Correct 0 ms 2396 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 7 ms 2396 KB Correct
3 Correct 2 ms 2396 KB Correct
4 Correct 24 ms 3284 KB Correct
5 Correct 25 ms 3272 KB Correct
6 Correct 0 ms 2392 KB Correct
7 Correct 0 ms 2392 KB Correct
8 Correct 14 ms 3024 KB Correct
9 Correct 15 ms 3024 KB Correct
10 Correct 22 ms 3284 KB Correct
11 Correct 13 ms 3280 KB Correct
12 Correct 14 ms 2760 KB Correct
13 Correct 19 ms 3148 KB Correct
14 Correct 22 ms 3272 KB Correct
15 Correct 21 ms 3284 KB Correct
16 Correct 18 ms 3280 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 0 ms 2396 KB Correct
3 Correct 0 ms 2396 KB Correct
4 Correct 1 ms 2392 KB Correct
5 Correct 0 ms 2396 KB Correct
6 Correct 1 ms 2396 KB Correct
7 Correct 0 ms 2396 KB Correct
8 Correct 0 ms 2396 KB Correct
9 Correct 0 ms 2396 KB Correct
10 Correct 0 ms 2396 KB Correct
11 Correct 0 ms 2508 KB Correct
12 Correct 0 ms 2396 KB Correct
13 Correct 1 ms 2392 KB Correct
14 Correct 0 ms 2396 KB Correct
15 Correct 0 ms 2396 KB Correct
16 Correct 1 ms 2396 KB Correct
17 Correct 0 ms 2396 KB Correct
18 Correct 0 ms 2392 KB Correct
19 Correct 0 ms 2396 KB Correct
20 Correct 0 ms 2396 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 10 ms 2396 KB Correct
3 Correct 38 ms 3260 KB Correct
4 Correct 38 ms 3276 KB Correct
5 Correct 36 ms 3256 KB Correct
6 Correct 34 ms 3284 KB Correct
7 Correct 42 ms 3264 KB Correct
8 Correct 39 ms 3256 KB Correct
9 Correct 38 ms 3256 KB Correct
10 Correct 36 ms 3188 KB Correct
11 Correct 36 ms 3284 KB Correct
12 Correct 1 ms 2392 KB Correct
13 Correct 0 ms 2396 KB Correct
14 Correct 14 ms 3280 KB Correct
15 Correct 14 ms 3148 KB Correct
16 Correct 14 ms 3280 KB Correct
17 Correct 13 ms 3284 KB Correct
18 Correct 13 ms 3284 KB Correct
19 Correct 17 ms 3284 KB Correct
20 Correct 31 ms 3272 KB Correct
21 Correct 30 ms 3264 KB Correct
22 Correct 28 ms 3260 KB Correct
23 Correct 34 ms 3276 KB Correct
24 Correct 26 ms 4304 KB Correct
25 Correct 29 ms 4308 KB Correct
26 Correct 15 ms 3868 KB Correct
27 Correct 14 ms 3520 KB Correct
28 Correct 27 ms 4292 KB Correct
29 Correct 21 ms 4304 KB Correct
30 Correct 23 ms 4308 KB Correct
31 Correct 21 ms 4128 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 0 ms 2396 KB Correct
3 Correct 6 ms 2396 KB Correct
4 Correct 0 ms 2396 KB Correct
5 Correct 6 ms 2516 KB Correct
6 Correct 6 ms 2520 KB Correct
7 Correct 0 ms 2396 KB Correct
8 Correct 0 ms 2436 KB Correct
9 Correct 2 ms 2396 KB Correct
10 Correct 0 ms 2396 KB Correct
11 Correct 7 ms 2396 KB Correct
12 Correct 2 ms 2396 KB Correct
13 Correct 24 ms 3284 KB Correct
14 Correct 25 ms 3272 KB Correct
15 Correct 0 ms 2392 KB Correct
16 Correct 0 ms 2392 KB Correct
17 Correct 14 ms 3024 KB Correct
18 Correct 15 ms 3024 KB Correct
19 Correct 22 ms 3284 KB Correct
20 Correct 13 ms 3280 KB Correct
21 Correct 14 ms 2760 KB Correct
22 Correct 19 ms 3148 KB Correct
23 Correct 22 ms 3272 KB Correct
24 Correct 21 ms 3284 KB Correct
25 Correct 18 ms 3280 KB Correct
26 Correct 0 ms 2396 KB Correct
27 Correct 1 ms 2392 KB Correct
28 Correct 0 ms 2396 KB Correct
29 Correct 1 ms 2396 KB Correct
30 Correct 0 ms 2396 KB Correct
31 Correct 0 ms 2396 KB Correct
32 Correct 0 ms 2396 KB Correct
33 Correct 0 ms 2396 KB Correct
34 Correct 0 ms 2508 KB Correct
35 Correct 0 ms 2396 KB Correct
36 Correct 1 ms 2392 KB Correct
37 Correct 0 ms 2396 KB Correct
38 Correct 0 ms 2396 KB Correct
39 Correct 1 ms 2396 KB Correct
40 Correct 0 ms 2396 KB Correct
41 Correct 0 ms 2392 KB Correct
42 Correct 0 ms 2396 KB Correct
43 Correct 0 ms 2396 KB Correct
44 Correct 10 ms 2396 KB Correct
45 Correct 38 ms 3260 KB Correct
46 Correct 38 ms 3276 KB Correct
47 Correct 36 ms 3256 KB Correct
48 Correct 34 ms 3284 KB Correct
49 Correct 42 ms 3264 KB Correct
50 Correct 39 ms 3256 KB Correct
51 Correct 38 ms 3256 KB Correct
52 Correct 36 ms 3188 KB Correct
53 Correct 36 ms 3284 KB Correct
54 Correct 1 ms 2392 KB Correct
55 Correct 0 ms 2396 KB Correct
56 Correct 14 ms 3280 KB Correct
57 Correct 14 ms 3148 KB Correct
58 Correct 14 ms 3280 KB Correct
59 Correct 13 ms 3284 KB Correct
60 Correct 13 ms 3284 KB Correct
61 Correct 17 ms 3284 KB Correct
62 Correct 31 ms 3272 KB Correct
63 Correct 30 ms 3264 KB Correct
64 Correct 28 ms 3260 KB Correct
65 Correct 34 ms 3276 KB Correct
66 Correct 26 ms 4304 KB Correct
67 Correct 29 ms 4308 KB Correct
68 Correct 15 ms 3868 KB Correct
69 Correct 14 ms 3520 KB Correct
70 Correct 27 ms 4292 KB Correct
71 Correct 21 ms 4304 KB Correct
72 Correct 23 ms 4308 KB Correct
73 Correct 21 ms 4128 KB Correct
74 Correct 8 ms 3416 KB Correct
75 Correct 32 ms 5068 KB Correct
76 Correct 36 ms 5180 KB Correct
77 Correct 0 ms 2392 KB Correct
78 Correct 14 ms 5076 KB Correct
79 Correct 21 ms 5064 KB Correct
80 Correct 15 ms 4920 KB Correct
81 Correct 17 ms 4048 KB Correct
82 Correct 14 ms 4188 KB Correct
83 Correct 27 ms 4172 KB Correct
84 Correct 5 ms 2908 KB Correct
85 Correct 25 ms 4564 KB Correct
86 Correct 27 ms 4620 KB Correct
87 Correct 23 ms 4100 KB Correct
88 Correct 8 ms 3420 KB Correct
89 Correct 7 ms 3392 KB Correct