Submission #1045750

# Submission time Handle Problem Language Result Execution time Memory
1045750 2024-08-06T07:23:31 Z SamAnd Sprinklers (CEOI24_sprinklers) C++17
100 / 100
185 ms 24580 KB
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 100005;

int n, m;
int s[N], f[N];

int ul[N], ur[N];
vector<pair<int, pair<int, int> > > v[N];
vector<int> vv[N];

int dp[N];
pair<pair<int, int>, pair<int, int> > p[N];
bool stg(int k)
{
    for (int i = 0; i <= n + 1; ++i)
    {
        dp[i] = 0;
        v[i].clear();
        vv[i].clear();
    }
    for (int i = n; i >= 1; --i)
    {
        ur[i] = i;
        if (i + 1 <= n && s[i] + k >= s[i + 1])
            ur[i] = ur[i + 1];
    }
    for (int i = 1; i <= n; ++i)
    {
        ul[i] = i;
        if (i - 1 >= 1 && s[i] - k <= s[i - 1])
            ul[i] = ul[i - 1];
    }
    for (int i = 1; i < n; ++i)
    {
        if (s[i] + k >= s[i + 1] - k)
        {
            int l = s[i + 1] - k;
            int r = s[i] + k;

            int ri = i + 1;
            if (ri + 1 <= n && s[ri + 1] <= r)
            {
                ++ri;
                r = s[ri] + k;
            }
            if (ri != i + 1)
            {
                ri = ur[i + 1];
                r = s[ri] + k;
            }

            int li = i;
            if (li - 1 >= 1 && s[li - 1] >= l)
            {
                --li;
                l = s[li] - k;
            }
            if (li != i)
            {
                li = ul[i];
                l = s[li] - k;
            }

            v[li].push_back(m_p(ri, m_p(l, r)));
            vv[li].push_back(i);
        }
    }

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < sz(v[i + 1]); ++j)
        {
            int l = v[i + 1][j].se.fi;
            int r = v[i + 1][j].se.se;
            int x = dp[i] + 1;
            if (x <= m && l <= f[x] && f[x] <= r)
            {
                int tl = x, tr = m;
                x = m + 1;
                while (tl <= tr)
                {
                    int mid = (tl + tr) / 2;
                    if (f[mid] > r)
                    {
                        x = mid;
                        tr = mid - 1;
                    }
                    else
                        tl = mid + 1;
                }
            }
            if (x - 1 > dp[v[i + 1][j].fi])
            {
                dp[v[i + 1][j].fi] = x - 1;
                p[v[i + 1][j].fi] = m_p(m_p(i, vv[i + 1][j]), m_p(i + 1, v[i + 1][j].fi));
            }
        }
        {
            int l = s[i + 1];
            int r = s[i + 1] + k;
            int x = dp[i] + 1;
            if (x <= m && l <= f[x] && f[x] <= r)
            {
                int tl = x, tr = m;
                x = m + 1;
                while (tl <= tr)
                {
                    int mid = (tl + tr) / 2;
                    if (f[mid] > r)
                    {
                        x = mid;
                        tr = mid - 1;
                    }
                    else
                        tl = mid + 1;
                }
            }
            if (x - 1 > dp[i + 1])
            {
                dp[i + 1] = x - 1;
                p[i + 1] = m_p(m_p(i, i), m_p((i + 1), -(i + 1)));
            }
        }
        {
            int l = s[i + 1] - k;
            int r = s[i + 1];
            int x = dp[i] + 1;
            if (x <= m && l <= f[x] && f[x] <= r)
            {
                int tl = x, tr = m;
                x = m + 1;
                while (tl <= tr)
                {
                    int mid = (tl + tr) / 2;
                    if (f[mid] > r)
                    {
                        x = mid;
                        tr = mid - 1;
                    }
                    else
                        tl = mid + 1;
                }
            }
            if (x - 1 > dp[i + 1])
            {
                dp[i + 1] = x - 1;
                p[i + 1] = m_p(m_p(i, i), m_p(-(i + 1), (i + 1)));
            }
        }
    }

    return (dp[n] == m);
}

void solv()
{
    cin >> n >> m;
    for (int i = 1; i <= n; ++i)
        cin >> s[i];
    for (int i = 1; i <= m; ++i)
        cin >> f[i];

    int l = 0, r = 1000000009;
    int ans = -1;
    while (l <= r)
    {
        int m = (l + r) / 2;
        if (stg(m))
        {
            ans = m;
            r = m - 1;
        }
        else
            l = m + 1;
    }

    cout << ans << "\n";
    if (ans != -1)
    {
        stg(ans);
        string s;
        s.assign(n + 1, '?');
        int i = n;
        while (i >= 1)
        {
            auto t = p[i];
            if (t.se.fi > 0 && t.se.se > 0)
            {
                int j = t.fi.se;
                s[j] = 'R';
                s[j + 1] = 'L';
                for (int k = j + 2; k <= t.se.se; ++k)
                    s[k] = 'R';
                for (int k = j - 1; k >= t.se.fi; --k)
                    s[k] = 'L';
            }
            else
            {
                if (t.se.fi < 0)
                {
                    s[t.se.se] = 'L';
                }
                else
                {
                    s[t.se.fi] = 'R';
                }
            }
            i = t.fi.fi;
        }
        reverse(all(s));
        s.pop_back();
        reverse(all(s));
        cout << s << "\n";
    }
}

int main()
{
    #ifdef SOMETHING
    freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    #endif // SOMETHING
    ios_base::sync_with_stdio(false), cin.tie(0);

    int tt = 1;
    //cin >> tt;
    while (tt--)
    {
        solv();
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Correct
2 Correct 2 ms 4956 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4956 KB Correct
2 Correct 10 ms 5468 KB Correct
3 Correct 2 ms 4956 KB Correct
4 Correct 7 ms 5468 KB Correct
5 Correct 8 ms 5468 KB Correct
6 Correct 1 ms 6748 KB Correct
7 Correct 2 ms 6748 KB Correct
8 Correct 4 ms 5212 KB Correct
9 Correct 2 ms 4952 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Correct
2 Correct 9 ms 5720 KB Correct
3 Correct 12 ms 6236 KB Correct
4 Correct 122 ms 17128 KB Correct
5 Correct 131 ms 19228 KB Correct
6 Correct 2 ms 4968 KB Correct
7 Correct 2 ms 4956 KB Correct
8 Correct 90 ms 12564 KB Correct
9 Correct 87 ms 12548 KB Correct
10 Correct 111 ms 12640 KB Correct
11 Correct 68 ms 18532 KB Correct
12 Correct 55 ms 11092 KB Correct
13 Correct 123 ms 20492 KB Correct
14 Correct 138 ms 20896 KB Correct
15 Correct 167 ms 20372 KB Correct
16 Correct 145 ms 20488 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Correct
2 Correct 2 ms 4956 KB Correct
3 Correct 1 ms 4956 KB Correct
4 Correct 1 ms 4956 KB Correct
5 Correct 2 ms 4956 KB Correct
6 Correct 2 ms 5028 KB Correct
7 Correct 2 ms 4956 KB Correct
8 Correct 2 ms 4956 KB Correct
9 Correct 2 ms 4956 KB Correct
10 Correct 2 ms 4956 KB Correct
11 Correct 2 ms 4956 KB Correct
12 Correct 2 ms 4956 KB Correct
13 Correct 2 ms 4956 KB Correct
14 Correct 2 ms 4956 KB Correct
15 Correct 2 ms 4956 KB Correct
16 Correct 2 ms 4956 KB Correct
17 Correct 2 ms 4956 KB Correct
18 Correct 1 ms 5076 KB Correct
19 Correct 2 ms 4952 KB Correct
20 Correct 2 ms 4956 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Correct
2 Correct 23 ms 7084 KB Correct
3 Correct 154 ms 24068 KB Correct
4 Correct 173 ms 24580 KB Correct
5 Correct 150 ms 22944 KB Correct
6 Correct 157 ms 23808 KB Correct
7 Correct 152 ms 23044 KB Correct
8 Correct 166 ms 24568 KB Correct
9 Correct 151 ms 24068 KB Correct
10 Correct 161 ms 23816 KB Correct
11 Correct 149 ms 23720 KB Correct
12 Correct 1 ms 6748 KB Correct
13 Correct 1 ms 6952 KB Correct
14 Correct 76 ms 22452 KB Correct
15 Correct 76 ms 21668 KB Correct
16 Correct 82 ms 22120 KB Correct
17 Correct 110 ms 11280 KB Correct
18 Correct 113 ms 11392 KB Correct
19 Correct 118 ms 11776 KB Correct
20 Correct 170 ms 23572 KB Correct
21 Correct 157 ms 23552 KB Correct
22 Correct 164 ms 22872 KB Correct
23 Correct 161 ms 21764 KB Correct
24 Correct 140 ms 19500 KB Correct
25 Correct 158 ms 16540 KB Correct
26 Correct 78 ms 19676 KB Correct
27 Correct 56 ms 13324 KB Correct
28 Correct 153 ms 20740 KB Correct
29 Correct 144 ms 20844 KB Correct
30 Correct 166 ms 21512 KB Correct
31 Correct 135 ms 17412 KB Correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 4952 KB Correct
2 Correct 2 ms 4956 KB Correct
3 Correct 10 ms 5468 KB Correct
4 Correct 2 ms 4956 KB Correct
5 Correct 7 ms 5468 KB Correct
6 Correct 8 ms 5468 KB Correct
7 Correct 1 ms 6748 KB Correct
8 Correct 2 ms 6748 KB Correct
9 Correct 4 ms 5212 KB Correct
10 Correct 2 ms 4952 KB Correct
11 Correct 9 ms 5720 KB Correct
12 Correct 12 ms 6236 KB Correct
13 Correct 122 ms 17128 KB Correct
14 Correct 131 ms 19228 KB Correct
15 Correct 2 ms 4968 KB Correct
16 Correct 2 ms 4956 KB Correct
17 Correct 90 ms 12564 KB Correct
18 Correct 87 ms 12548 KB Correct
19 Correct 111 ms 12640 KB Correct
20 Correct 68 ms 18532 KB Correct
21 Correct 55 ms 11092 KB Correct
22 Correct 123 ms 20492 KB Correct
23 Correct 138 ms 20896 KB Correct
24 Correct 167 ms 20372 KB Correct
25 Correct 145 ms 20488 KB Correct
26 Correct 1 ms 4956 KB Correct
27 Correct 1 ms 4956 KB Correct
28 Correct 2 ms 4956 KB Correct
29 Correct 2 ms 5028 KB Correct
30 Correct 2 ms 4956 KB Correct
31 Correct 2 ms 4956 KB Correct
32 Correct 2 ms 4956 KB Correct
33 Correct 2 ms 4956 KB Correct
34 Correct 2 ms 4956 KB Correct
35 Correct 2 ms 4956 KB Correct
36 Correct 2 ms 4956 KB Correct
37 Correct 2 ms 4956 KB Correct
38 Correct 2 ms 4956 KB Correct
39 Correct 2 ms 4956 KB Correct
40 Correct 2 ms 4956 KB Correct
41 Correct 1 ms 5076 KB Correct
42 Correct 2 ms 4952 KB Correct
43 Correct 2 ms 4956 KB Correct
44 Correct 23 ms 7084 KB Correct
45 Correct 154 ms 24068 KB Correct
46 Correct 173 ms 24580 KB Correct
47 Correct 150 ms 22944 KB Correct
48 Correct 157 ms 23808 KB Correct
49 Correct 152 ms 23044 KB Correct
50 Correct 166 ms 24568 KB Correct
51 Correct 151 ms 24068 KB Correct
52 Correct 161 ms 23816 KB Correct
53 Correct 149 ms 23720 KB Correct
54 Correct 1 ms 6748 KB Correct
55 Correct 1 ms 6952 KB Correct
56 Correct 76 ms 22452 KB Correct
57 Correct 76 ms 21668 KB Correct
58 Correct 82 ms 22120 KB Correct
59 Correct 110 ms 11280 KB Correct
60 Correct 113 ms 11392 KB Correct
61 Correct 118 ms 11776 KB Correct
62 Correct 170 ms 23572 KB Correct
63 Correct 157 ms 23552 KB Correct
64 Correct 164 ms 22872 KB Correct
65 Correct 161 ms 21764 KB Correct
66 Correct 140 ms 19500 KB Correct
67 Correct 158 ms 16540 KB Correct
68 Correct 78 ms 19676 KB Correct
69 Correct 56 ms 13324 KB Correct
70 Correct 153 ms 20740 KB Correct
71 Correct 144 ms 20844 KB Correct
72 Correct 166 ms 21512 KB Correct
73 Correct 135 ms 17412 KB Correct
74 Correct 11 ms 8764 KB Correct
75 Correct 160 ms 20032 KB Correct
76 Correct 185 ms 19716 KB Correct
77 Correct 1 ms 6748 KB Correct
78 Correct 75 ms 12772 KB Correct
79 Correct 113 ms 12804 KB Correct
80 Correct 74 ms 12812 KB Correct
81 Correct 94 ms 20224 KB Correct
82 Correct 87 ms 18536 KB Correct
83 Correct 89 ms 18796 KB Correct
84 Correct 14 ms 8028 KB Correct
85 Correct 143 ms 20532 KB Correct
86 Correct 170 ms 20740 KB Correct
87 Correct 128 ms 17996 KB Correct
88 Correct 9 ms 6424 KB Correct
89 Correct 7 ms 8284 KB Correct