#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
const int INF = 2e9+10;
void solve() {
int n, m; cin >> n >> m;
vector<int> a(n); for (auto& x : a) cin >> x;
vector<int> b(m); for (auto& x : b) cin >> x;
vector<string> build(n + 1);
auto can = [&](int x) {
vector<int> dp(n+1);
auto upd = [&](int i, int me, string c) {
if (dp[i] <= me) {
dp[i] = me;
build[i] = c;
}
};
for (int i = 0; i < n; i++) {
// dp[i+1] = max(dp[i+1], dp[i]);
upd(i+1, dp[i], "L");
if (dp[i] >= m) continue;
int need = b[dp[i]];
if (a[i] - x <= need && need <= a[i]) {
int cur = upper_bound(b.begin(), b.end(), a[i]) - b.begin();
upd(i+1, cur, "L");
// dp[i+1] = max(dp[i+1], cur);
}
if (a[i] <= need && need <= a[i] + x) {
int cur = upper_bound(b.begin(), b.end(), a[i] + x) - b.begin();
upd(i+1, cur, "R");
// dp[i+1] = max(dp[i+1], cur);
}
if (i < n-1) {
if (a[i+1] - x <= need && need <= a[i] + x) {
int cur = upper_bound(b.begin(), b.end(), a[i] + x) - b.begin();
upd(i+2, cur, "RL");
// dp[i+2] = max(dp[i+2], cur);
}
}
}
return dp[n] >= m;
};
int lo = -1, hi = MOD, mid = (lo + hi) / 2;
while (lo < mid && mid < hi) {
if (can(mid)) hi = mid;
else lo = mid;
mid = (lo + hi) / 2;
}
if (!can(hi)) {
assert(n == 1);
cout << -1 << '\n';
return;
}
vector<char> ans(n, '?');
int c = n;
while (c > 0) {
string p = build[c];
if (sz(p) == 1) {
ans[c-1] = p[0];
c--;
} else if (sz(p) == 2) {
ans[c-1] = p[1];
ans[c-2] = p[0];
c -= 2;
} else assert(false);
}
vector<int> ps(m + 1);
for (int i = 0; i < n; i++) {
int l = -1, r = -1;
if (ans[i] == 'L') {
l = a[i] - hi, r = a[i];
} else if (ans[i] == 'R') {
l = a[i], r = a[i] + hi;
} else assert(false);
int L = lower_bound(b.begin(), b.end(), l) - b.begin();
int R = upper_bound(b.begin(), b.end(), r) - b.begin() - 1;
if (L <= R) {
ps[L]++;
ps[R+1]--;
}
}
for (int i = 1; i <= m; i++)
ps[i] += ps[i-1];
for (int i = 0; i < m; i++) {
assert(ps[i]);
}
cout << hi << '\n';
for (char c : ans) cout << c;
cout << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (T--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
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 |
6 ms |
1116 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Correct |
5 ms |
604 KB |
Correct |
5 |
Correct |
6 ms |
604 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 |
0 ms |
344 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
7 ms |
1124 KB |
Correct |
3 |
Correct |
8 ms |
872 KB |
Correct |
4 |
Correct |
93 ms |
4896 KB |
Correct |
5 |
Correct |
100 ms |
4808 KB |
Correct |
6 |
Correct |
0 ms |
344 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
35 ms |
4812 KB |
Correct |
9 |
Correct |
36 ms |
4812 KB |
Correct |
10 |
Correct |
38 ms |
5064 KB |
Correct |
11 |
Correct |
32 ms |
4304 KB |
Correct |
12 |
Correct |
45 ms |
2496 KB |
Correct |
13 |
Correct |
66 ms |
4632 KB |
Correct |
14 |
Correct |
69 ms |
4640 KB |
Correct |
15 |
Correct |
80 ms |
4560 KB |
Correct |
16 |
Correct |
58 ms |
4640 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Correct |
0 ms |
348 KB |
Correct |
5 |
Correct |
0 ms |
348 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
0 ms |
348 KB |
Correct |
10 |
Correct |
0 ms |
348 KB |
Correct |
11 |
Correct |
0 ms |
348 KB |
Correct |
12 |
Correct |
0 ms |
348 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
0 ms |
344 KB |
Correct |
15 |
Correct |
1 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
0 ms |
344 KB |
Correct |
18 |
Correct |
0 ms |
344 KB |
Correct |
19 |
Correct |
0 ms |
344 KB |
Correct |
20 |
Correct |
0 ms |
348 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
28 ms |
1624 KB |
Correct |
3 |
Correct |
136 ms |
4812 KB |
Correct |
4 |
Correct |
143 ms |
5068 KB |
Correct |
5 |
Correct |
141 ms |
4808 KB |
Correct |
6 |
Correct |
136 ms |
4780 KB |
Correct |
7 |
Correct |
138 ms |
4808 KB |
Correct |
8 |
Correct |
137 ms |
4808 KB |
Correct |
9 |
Correct |
137 ms |
4756 KB |
Correct |
10 |
Correct |
160 ms |
4808 KB |
Correct |
11 |
Correct |
144 ms |
4812 KB |
Correct |
12 |
Correct |
0 ms |
600 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
31 ms |
4388 KB |
Correct |
15 |
Correct |
34 ms |
5328 KB |
Correct |
16 |
Correct |
33 ms |
5332 KB |
Correct |
17 |
Correct |
34 ms |
5592 KB |
Correct |
18 |
Correct |
33 ms |
5584 KB |
Correct |
19 |
Correct |
47 ms |
5724 KB |
Correct |
20 |
Correct |
115 ms |
6348 KB |
Correct |
21 |
Correct |
113 ms |
6312 KB |
Correct |
22 |
Correct |
105 ms |
6100 KB |
Correct |
23 |
Correct |
109 ms |
6100 KB |
Correct |
24 |
Correct |
97 ms |
6088 KB |
Correct |
25 |
Correct |
96 ms |
5908 KB |
Correct |
26 |
Correct |
54 ms |
4700 KB |
Correct |
27 |
Correct |
49 ms |
2764 KB |
Correct |
28 |
Correct |
88 ms |
5724 KB |
Correct |
29 |
Correct |
80 ms |
5632 KB |
Correct |
30 |
Correct |
89 ms |
5668 KB |
Correct |
31 |
Correct |
103 ms |
5648 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
6 ms |
1116 KB |
Correct |
4 |
Correct |
0 ms |
348 KB |
Correct |
5 |
Correct |
5 ms |
604 KB |
Correct |
6 |
Correct |
6 ms |
604 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
1 ms |
344 KB |
Correct |
10 |
Correct |
0 ms |
344 KB |
Correct |
11 |
Correct |
7 ms |
1124 KB |
Correct |
12 |
Correct |
8 ms |
872 KB |
Correct |
13 |
Correct |
93 ms |
4896 KB |
Correct |
14 |
Correct |
100 ms |
4808 KB |
Correct |
15 |
Correct |
0 ms |
344 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
35 ms |
4812 KB |
Correct |
18 |
Correct |
36 ms |
4812 KB |
Correct |
19 |
Correct |
38 ms |
5064 KB |
Correct |
20 |
Correct |
32 ms |
4304 KB |
Correct |
21 |
Correct |
45 ms |
2496 KB |
Correct |
22 |
Correct |
66 ms |
4632 KB |
Correct |
23 |
Correct |
69 ms |
4640 KB |
Correct |
24 |
Correct |
80 ms |
4560 KB |
Correct |
25 |
Correct |
58 ms |
4640 KB |
Correct |
26 |
Correct |
0 ms |
348 KB |
Correct |
27 |
Correct |
0 ms |
348 KB |
Correct |
28 |
Correct |
0 ms |
348 KB |
Correct |
29 |
Correct |
0 ms |
348 KB |
Correct |
30 |
Correct |
0 ms |
348 KB |
Correct |
31 |
Correct |
0 ms |
348 KB |
Correct |
32 |
Correct |
0 ms |
348 KB |
Correct |
33 |
Correct |
0 ms |
348 KB |
Correct |
34 |
Correct |
0 ms |
348 KB |
Correct |
35 |
Correct |
0 ms |
348 KB |
Correct |
36 |
Correct |
0 ms |
348 KB |
Correct |
37 |
Correct |
0 ms |
344 KB |
Correct |
38 |
Correct |
1 ms |
348 KB |
Correct |
39 |
Correct |
0 ms |
348 KB |
Correct |
40 |
Correct |
0 ms |
344 KB |
Correct |
41 |
Correct |
0 ms |
344 KB |
Correct |
42 |
Correct |
0 ms |
344 KB |
Correct |
43 |
Correct |
0 ms |
348 KB |
Correct |
44 |
Correct |
28 ms |
1624 KB |
Correct |
45 |
Correct |
136 ms |
4812 KB |
Correct |
46 |
Correct |
143 ms |
5068 KB |
Correct |
47 |
Correct |
141 ms |
4808 KB |
Correct |
48 |
Correct |
136 ms |
4780 KB |
Correct |
49 |
Correct |
138 ms |
4808 KB |
Correct |
50 |
Correct |
137 ms |
4808 KB |
Correct |
51 |
Correct |
137 ms |
4756 KB |
Correct |
52 |
Correct |
160 ms |
4808 KB |
Correct |
53 |
Correct |
144 ms |
4812 KB |
Correct |
54 |
Correct |
0 ms |
600 KB |
Correct |
55 |
Correct |
0 ms |
348 KB |
Correct |
56 |
Correct |
31 ms |
4388 KB |
Correct |
57 |
Correct |
34 ms |
5328 KB |
Correct |
58 |
Correct |
33 ms |
5332 KB |
Correct |
59 |
Correct |
34 ms |
5592 KB |
Correct |
60 |
Correct |
33 ms |
5584 KB |
Correct |
61 |
Correct |
47 ms |
5724 KB |
Correct |
62 |
Correct |
115 ms |
6348 KB |
Correct |
63 |
Correct |
113 ms |
6312 KB |
Correct |
64 |
Correct |
105 ms |
6100 KB |
Correct |
65 |
Correct |
109 ms |
6100 KB |
Correct |
66 |
Correct |
97 ms |
6088 KB |
Correct |
67 |
Correct |
96 ms |
5908 KB |
Correct |
68 |
Correct |
54 ms |
4700 KB |
Correct |
69 |
Correct |
49 ms |
2764 KB |
Correct |
70 |
Correct |
88 ms |
5724 KB |
Correct |
71 |
Correct |
80 ms |
5632 KB |
Correct |
72 |
Correct |
89 ms |
5668 KB |
Correct |
73 |
Correct |
103 ms |
5648 KB |
Correct |
74 |
Correct |
10 ms |
2136 KB |
Correct |
75 |
Correct |
134 ms |
6684 KB |
Correct |
76 |
Correct |
159 ms |
6684 KB |
Correct |
77 |
Correct |
0 ms |
348 KB |
Correct |
78 |
Correct |
36 ms |
6852 KB |
Correct |
79 |
Correct |
38 ms |
6856 KB |
Correct |
80 |
Correct |
37 ms |
6860 KB |
Correct |
81 |
Correct |
32 ms |
5328 KB |
Correct |
82 |
Correct |
34 ms |
5328 KB |
Correct |
83 |
Correct |
34 ms |
5576 KB |
Correct |
84 |
Correct |
16 ms |
1628 KB |
Correct |
85 |
Correct |
101 ms |
5928 KB |
Correct |
86 |
Correct |
111 ms |
6088 KB |
Correct |
87 |
Correct |
106 ms |
5396 KB |
Correct |
88 |
Correct |
7 ms |
2136 KB |
Correct |
89 |
Correct |
6 ms |
2140 KB |
Correct |