#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
vector<int> s(n);
for (int i = 0; i < n; i++) {
cin >> s[i];
}
vector<int> f(m);
for (int i = 0; i < m; i++) {
cin >> f[i];
}
auto Check = [&](int k) {
vector<int> dp(n + 1, -1);
for (int i = 0; i < n; i++) {
if (dp[i] == m - 1) {
break;
}
if (i + 1 < n) {
// take anything
dp[i + 1] = max(dp[i + 1], dp[i]);
}
if (f[dp[i] + 1] >= s[i] - k && f[dp[i] + 1] <= s[i]) {
// L
int from = (int) (lower_bound(f.begin(), f.end(), s[i] - k) - f.begin());
int to = (int) (lower_bound(f.begin(), f.end(), s[i] + 1) - f.begin()) - 1;
dp[i + 1] = max(dp[i + 1], to);
}
if (f[dp[i] + 1] >= s[i] && f[dp[i] + 1] <= s[i] + k) {
// R
int from = (int) (lower_bound(f.begin(), f.end(), s[i]) - f.begin());
int to = (int) (lower_bound(f.begin(), f.end(), s[i] + k + 1) - f.begin()) - 1;
dp[i + 1] = max(dp[i + 1], to);
}
if (i + 1 < n && f[dp[i] + 1] >= s[i + 1] - k && f[dp[i] + 1] <= s[i + 1]) {
// RL
int from = (int) (lower_bound(f.begin(), f.end(), s[i + 1] - k) - f.begin());
int to = (int) (lower_bound(f.begin(), f.end(), s[i + 1] + 1) - f.begin()) - 1;
if (to == m - 1) {
dp[i + 2] = max(dp[i + 2], m - 1);
continue;
}
if (f[to + 1] >= s[i] && f[to + 1] <= s[i] + k) {
to = (int) (lower_bound(f.begin(), f.end(), s[i] + k + 1) - f.begin()) - 1;
dp[i + 2] = max(dp[i + 2], to);
}
}
}
return *max_element(dp.begin(), dp.end()) == m - 1;
};
int low = 0, high = 1e9, k = -1;
while (low <= high) {
int mid = (low + high) >> 1;
if (Check(mid)) {
k = mid;
high = mid - 1;
} else {
low = mid + 1;
}
}
if (k == -1) {
cout << k << '\n';
return 0;
}
vector<int> dp(n + 1, -1);
vector<int> d(n + 1);
auto Update = [&](int p, int v, int t) {
if (dp[p] < v) {
dp[p] = v;
d[p] = t;
}
};
for (int i = 0; i < n; i++) {
if (dp[i] == m - 1) {
Update(i + 1, dp[i], 0);
continue;
}
if (i + 1 < n) {
// take anything
Update(i + 1, dp[i], 0);
}
if (f[dp[i] + 1] >= s[i] - k && f[dp[i] + 1] <= s[i]) {
// L
int from = (int) (lower_bound(f.begin(), f.end(), s[i] - k) - f.begin());
int to = (int) (lower_bound(f.begin(), f.end(), s[i] + 1) - f.begin()) - 1;
Update(i + 1, to, 0);
}
if (f[dp[i] + 1] >= s[i] && f[dp[i] + 1] <= s[i] + k) {
// R
int from = (int) (lower_bound(f.begin(), f.end(), s[i]) - f.begin());
int to = (int) (lower_bound(f.begin(), f.end(), s[i] + k + 1) - f.begin()) - 1;
Update(i + 1, to, 1);
}
if (i + 1 < n && f[dp[i] + 1] >= s[i + 1] - k && f[dp[i] + 1] <= s[i + 1]) {
// RL
int from = (int) (lower_bound(f.begin(), f.end(), s[i + 1] - k) - f.begin());
int to = (int) (lower_bound(f.begin(), f.end(), s[i + 1] + 1) - f.begin()) - 1;
if (to == m - 1) {
Update(i + 2, m - 1, 2);
continue;
}
if (f[to + 1] >= s[i] && f[to + 1] <= s[i] + k) {
to = (int) (lower_bound(f.begin(), f.end(), s[i] + k + 1) - f.begin()) - 1;
Update(i + 2, to, 2);
}
}
}
vector<char> dir(n);
int p = n;
while (p > 0) {
if (d[p] == 0) {
--p;
dir[p] = 'L';
} else if (d[p] == 1) {
--p;
dir[p] = 'R';
} else {
--p;
dir[p] = 'L';
--p;
dir[p] = 'R';
}
}
cout << k << '\n';
for (int i = 0; i < n; i++) {
cout << dir[i];
}
return 0;
}
Compilation message
Main.cpp: In lambda function:
Main.cpp:30:13: warning: unused variable 'from' [-Wunused-variable]
30 | int from = (int) (lower_bound(f.begin(), f.end(), s[i] - k) - f.begin());
| ^~~~
Main.cpp:36:13: warning: unused variable 'from' [-Wunused-variable]
36 | int from = (int) (lower_bound(f.begin(), f.end(), s[i]) - f.begin());
| ^~~~
Main.cpp:42:13: warning: unused variable 'from' [-Wunused-variable]
42 | int from = (int) (lower_bound(f.begin(), f.end(), s[i + 1] - k) - f.begin());
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:89:11: warning: unused variable 'from' [-Wunused-variable]
89 | int from = (int) (lower_bound(f.begin(), f.end(), s[i] - k) - f.begin());
| ^~~~
Main.cpp:95:11: warning: unused variable 'from' [-Wunused-variable]
95 | int from = (int) (lower_bound(f.begin(), f.end(), s[i]) - f.begin());
| ^~~~
Main.cpp:101:11: warning: unused variable 'from' [-Wunused-variable]
101 | int from = (int) (lower_bound(f.begin(), f.end(), s[i + 1] - k) - f.begin());
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
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 |
10 ms |
1416 KB |
Correct |
3 |
Correct |
1 ms |
348 KB |
Correct |
4 |
Correct |
10 ms |
1740 KB |
Correct |
5 |
Correct |
7 ms |
1628 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
2 ms |
604 KB |
Correct |
9 |
Correct |
0 ms |
456 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
8 ms |
1628 KB |
Correct |
3 |
Correct |
5 ms |
600 KB |
Correct |
4 |
Correct |
54 ms |
3944 KB |
Correct |
5 |
Correct |
57 ms |
4040 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
344 KB |
Correct |
8 |
Correct |
20 ms |
4052 KB |
Correct |
9 |
Correct |
21 ms |
4040 KB |
Correct |
10 |
Correct |
23 ms |
4040 KB |
Correct |
11 |
Correct |
18 ms |
2768 KB |
Correct |
12 |
Correct |
33 ms |
2492 KB |
Correct |
13 |
Correct |
39 ms |
3016 KB |
Correct |
14 |
Correct |
45 ms |
3292 KB |
Correct |
15 |
Correct |
57 ms |
3396 KB |
Correct |
16 |
Correct |
42 ms |
2992 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 |
456 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 |
344 KB |
Correct |
11 |
Correct |
0 ms |
452 KB |
Correct |
12 |
Correct |
0 ms |
348 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
1 ms |
348 KB |
Correct |
15 |
Correct |
0 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
0 ms |
348 KB |
Correct |
18 |
Correct |
1 ms |
344 KB |
Correct |
19 |
Correct |
0 ms |
348 KB |
Correct |
20 |
Correct |
0 ms |
348 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
26 ms |
1996 KB |
Correct |
3 |
Correct |
108 ms |
4068 KB |
Correct |
4 |
Correct |
106 ms |
4080 KB |
Correct |
5 |
Correct |
109 ms |
4040 KB |
Correct |
6 |
Correct |
100 ms |
4144 KB |
Correct |
7 |
Correct |
108 ms |
4064 KB |
Correct |
8 |
Correct |
103 ms |
4044 KB |
Correct |
9 |
Correct |
106 ms |
3904 KB |
Correct |
10 |
Correct |
118 ms |
4068 KB |
Correct |
11 |
Correct |
127 ms |
4024 KB |
Correct |
12 |
Correct |
0 ms |
348 KB |
Correct |
13 |
Correct |
1 ms |
348 KB |
Correct |
14 |
Correct |
22 ms |
2740 KB |
Correct |
15 |
Correct |
20 ms |
2752 KB |
Correct |
16 |
Correct |
20 ms |
2772 KB |
Correct |
17 |
Correct |
23 ms |
2772 KB |
Correct |
18 |
Correct |
21 ms |
2768 KB |
Correct |
19 |
Correct |
24 ms |
3028 KB |
Correct |
20 |
Correct |
83 ms |
3644 KB |
Correct |
21 |
Correct |
84 ms |
3528 KB |
Correct |
22 |
Correct |
68 ms |
3784 KB |
Correct |
23 |
Correct |
70 ms |
3528 KB |
Correct |
24 |
Correct |
54 ms |
3276 KB |
Correct |
25 |
Correct |
60 ms |
3128 KB |
Correct |
26 |
Correct |
31 ms |
2328 KB |
Correct |
27 |
Correct |
40 ms |
1548 KB |
Correct |
28 |
Correct |
53 ms |
3092 KB |
Correct |
29 |
Correct |
46 ms |
3012 KB |
Correct |
30 |
Correct |
56 ms |
3060 KB |
Correct |
31 |
Correct |
70 ms |
3096 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
10 ms |
1416 KB |
Correct |
4 |
Correct |
1 ms |
348 KB |
Correct |
5 |
Correct |
10 ms |
1740 KB |
Correct |
6 |
Correct |
7 ms |
1628 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
2 ms |
604 KB |
Correct |
10 |
Correct |
0 ms |
456 KB |
Correct |
11 |
Correct |
8 ms |
1628 KB |
Correct |
12 |
Correct |
5 ms |
600 KB |
Correct |
13 |
Correct |
54 ms |
3944 KB |
Correct |
14 |
Correct |
57 ms |
4040 KB |
Correct |
15 |
Correct |
0 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
344 KB |
Correct |
17 |
Correct |
20 ms |
4052 KB |
Correct |
18 |
Correct |
21 ms |
4040 KB |
Correct |
19 |
Correct |
23 ms |
4040 KB |
Correct |
20 |
Correct |
18 ms |
2768 KB |
Correct |
21 |
Correct |
33 ms |
2492 KB |
Correct |
22 |
Correct |
39 ms |
3016 KB |
Correct |
23 |
Correct |
45 ms |
3292 KB |
Correct |
24 |
Correct |
57 ms |
3396 KB |
Correct |
25 |
Correct |
42 ms |
2992 KB |
Correct |
26 |
Correct |
0 ms |
348 KB |
Correct |
27 |
Correct |
0 ms |
456 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 |
344 KB |
Correct |
34 |
Correct |
0 ms |
452 KB |
Correct |
35 |
Correct |
0 ms |
348 KB |
Correct |
36 |
Correct |
0 ms |
348 KB |
Correct |
37 |
Correct |
1 ms |
348 KB |
Correct |
38 |
Correct |
0 ms |
348 KB |
Correct |
39 |
Correct |
0 ms |
348 KB |
Correct |
40 |
Correct |
0 ms |
348 KB |
Correct |
41 |
Correct |
1 ms |
344 KB |
Correct |
42 |
Correct |
0 ms |
348 KB |
Correct |
43 |
Correct |
0 ms |
348 KB |
Correct |
44 |
Correct |
26 ms |
1996 KB |
Correct |
45 |
Correct |
108 ms |
4068 KB |
Correct |
46 |
Correct |
106 ms |
4080 KB |
Correct |
47 |
Correct |
109 ms |
4040 KB |
Correct |
48 |
Correct |
100 ms |
4144 KB |
Correct |
49 |
Correct |
108 ms |
4064 KB |
Correct |
50 |
Correct |
103 ms |
4044 KB |
Correct |
51 |
Correct |
106 ms |
3904 KB |
Correct |
52 |
Correct |
118 ms |
4068 KB |
Correct |
53 |
Correct |
127 ms |
4024 KB |
Correct |
54 |
Correct |
0 ms |
348 KB |
Correct |
55 |
Correct |
1 ms |
348 KB |
Correct |
56 |
Correct |
22 ms |
2740 KB |
Correct |
57 |
Correct |
20 ms |
2752 KB |
Correct |
58 |
Correct |
20 ms |
2772 KB |
Correct |
59 |
Correct |
23 ms |
2772 KB |
Correct |
60 |
Correct |
21 ms |
2768 KB |
Correct |
61 |
Correct |
24 ms |
3028 KB |
Correct |
62 |
Correct |
83 ms |
3644 KB |
Correct |
63 |
Correct |
84 ms |
3528 KB |
Correct |
64 |
Correct |
68 ms |
3784 KB |
Correct |
65 |
Correct |
70 ms |
3528 KB |
Correct |
66 |
Correct |
54 ms |
3276 KB |
Correct |
67 |
Correct |
60 ms |
3128 KB |
Correct |
68 |
Correct |
31 ms |
2328 KB |
Correct |
69 |
Correct |
40 ms |
1548 KB |
Correct |
70 |
Correct |
53 ms |
3092 KB |
Correct |
71 |
Correct |
46 ms |
3012 KB |
Correct |
72 |
Correct |
56 ms |
3060 KB |
Correct |
73 |
Correct |
70 ms |
3096 KB |
Correct |
74 |
Correct |
10 ms |
1628 KB |
Correct |
75 |
Correct |
82 ms |
4048 KB |
Correct |
76 |
Correct |
95 ms |
4040 KB |
Correct |
77 |
Correct |
0 ms |
348 KB |
Correct |
78 |
Correct |
22 ms |
4040 KB |
Correct |
79 |
Correct |
23 ms |
4040 KB |
Correct |
80 |
Correct |
22 ms |
4040 KB |
Correct |
81 |
Correct |
17 ms |
2740 KB |
Correct |
82 |
Correct |
18 ms |
2768 KB |
Correct |
83 |
Correct |
18 ms |
2772 KB |
Correct |
84 |
Correct |
11 ms |
1116 KB |
Correct |
85 |
Correct |
59 ms |
3300 KB |
Correct |
86 |
Correct |
76 ms |
3272 KB |
Correct |
87 |
Correct |
70 ms |
3096 KB |
Correct |
88 |
Correct |
9 ms |
1628 KB |
Correct |
89 |
Correct |
7 ms |
1624 KB |
Correct |