#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define pb push_back
#define en cout << '\n'
#define vi vector<int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) x.begin(),x.end()
const int N = 1e5+100;
int n, m, a[N], b[N];
int dp[N][2], par[N][2];
int get(int l, int r){
if(r < l) return 0;
return upper_bound(b+1, b+1+m, r) - lower_bound(b+1, b+1+m, l);
}
int get_first(int l){
int pos = lower_bound(b+1, b+1+m, l) - b;
if(pos == m+1) return 2e9;
return b[pos];
}
void solve(){
cin >> n >> m;
vector<pii> v;
for(int i = 1; i <= n; ++i){
cin >> a[i];
v.pb({a[i], 1});
}
for(int i = 1; i <= m; ++i){
cin >> b[i];
v.pb({b[i], 0});
}
sort(all(v));
string soll;
int l = 0, r = 1e9+2, ans = -1;
while(l <= r){
int k = l+r>>1;
string sol = "";
bool ok = 1;
int last_covered = -2e9;
dp[1][0] = get(-2e9, a[1] - k - 1) > 0 ? b[1] : get_first(a[1] + 1);
dp[1][1] = get(-2e9, a[1] - 1) > 0 ? b[1] : get_first(a[1] + k + 1);
for(int i = 2; i <= n ;++i){
int L = a[i] - k, R = a[i];
if(dp[i - 1][0] >= L){
dp[i][0] = get_first(a[i] + 1);
par[i][0] = 0;
}else{
dp[i][0] = -2e9;
}
if(dp[i - 1][0] >= a[i]){
par[i][1] = 0;
dp[i][1] = get_first(a[i] + k + 1);
}else{
dp[i][1] = dp[i - 1][0];
par[i][1] = 0;
}
if(dp[i-1][1] >= L){
if(dp[i][0] < get_first(max(a[i - 1] + k + 1, a[i] + 1))){
par[i][0] = 1;
}
dp[i][0] = max(dp[i][0], get_first(max(a[i - 1] + k + 1, a[i] + 1)));
}else{
dp[i][0] = max(dp[i][0], (int)(-2e9));
}
if(dp[i-1][1] >= a[i]){
if(dp[i][1] < get_first(a[i] + k + 1)){
par[i][1] = 1;
}
dp[i][1] = max(dp[i][1], get_first(a[i] + k + 1));
}else{
if(dp[i][1] < dp[i - 1][1]){
dp[i][1] = dp[i - 1][1];
par[i][1] = 1;
}
}
}
if(max(dp[n][0], dp[n][1]) == (int)2e9){
ok = 1;
int x = 0;
if(dp[n][0] == 2e9) x = 0;
else x = 1;
sol += (x==0?'L':'R');
for(int i = n - 1; i >= 1; --i){
x = par[i + 1][x];
sol += (x==0?'L':'R');
}
reverse(all(sol));
}
else ok = 0;
// cout << k << '\n';
// for(int i = 1; i <= n; ++i) cout << dp[i][0] << ' ' << dp[i][1] << '\n';
if(ok){
ans = k;
soll = sol;
r = k - 1;
}else{
l = k + 1;
}
}
cout << ans << '\n';
if(ans != -1){
cout << soll;
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
solve();
return 0;
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:40:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
40 | int k = l+r>>1;
| ~^~
Main.cpp:47:22: warning: unused variable 'R' [-Wunused-variable]
47 | int L = a[i] - k, R = a[i];
| ^
Main.cpp:43:7: warning: unused variable 'last_covered' [-Wunused-variable]
43 | int last_covered = -2e9;
| ^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
460 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
460 KB |
Correct |
2 |
Correct |
8 ms |
2524 KB |
Correct |
3 |
Correct |
1 ms |
348 KB |
Correct |
4 |
Correct |
9 ms |
2780 KB |
Correct |
5 |
Correct |
12 ms |
2776 KB |
Correct |
6 |
Correct |
0 ms |
464 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
2 ms |
1116 KB |
Correct |
9 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
19 ms |
2780 KB |
Correct |
3 |
Correct |
31 ms |
856 KB |
Correct |
4 |
Correct |
370 ms |
6596 KB |
Correct |
5 |
Correct |
421 ms |
6596 KB |
Correct |
6 |
Correct |
0 ms |
344 KB |
Correct |
7 |
Correct |
0 ms |
344 KB |
Correct |
8 |
Correct |
210 ms |
6636 KB |
Correct |
9 |
Correct |
204 ms |
6596 KB |
Correct |
10 |
Correct |
358 ms |
6596 KB |
Correct |
11 |
Correct |
171 ms |
4500 KB |
Correct |
12 |
Correct |
173 ms |
3936 KB |
Correct |
13 |
Correct |
363 ms |
5376 KB |
Correct |
14 |
Correct |
353 ms |
5576 KB |
Correct |
15 |
Correct |
370 ms |
5832 KB |
Correct |
16 |
Correct |
357 ms |
5320 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
460 KB |
Correct |
3 |
Correct |
0 ms |
600 KB |
Correct |
4 |
Correct |
1 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 |
500 KB |
Correct |
11 |
Correct |
0 ms |
348 KB |
Correct |
12 |
Correct |
0 ms |
348 KB |
Correct |
13 |
Correct |
1 ms |
348 KB |
Correct |
14 |
Correct |
1 ms |
348 KB |
Correct |
15 |
Correct |
1 ms |
344 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
0 ms |
348 KB |
Correct |
18 |
Correct |
1 ms |
348 KB |
Correct |
19 |
Correct |
0 ms |
348 KB |
Correct |
20 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
63 ms |
3032 KB |
Correct |
3 |
Correct |
455 ms |
6684 KB |
Correct |
4 |
Correct |
431 ms |
6600 KB |
Correct |
5 |
Correct |
439 ms |
6600 KB |
Correct |
6 |
Correct |
465 ms |
6780 KB |
Correct |
7 |
Correct |
489 ms |
6640 KB |
Correct |
8 |
Correct |
457 ms |
6592 KB |
Correct |
9 |
Correct |
457 ms |
6576 KB |
Correct |
10 |
Correct |
427 ms |
6600 KB |
Correct |
11 |
Correct |
476 ms |
6600 KB |
Correct |
12 |
Correct |
0 ms |
344 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
192 ms |
4368 KB |
Correct |
15 |
Correct |
235 ms |
4560 KB |
Correct |
16 |
Correct |
219 ms |
4552 KB |
Correct |
17 |
Correct |
319 ms |
5316 KB |
Correct |
18 |
Correct |
342 ms |
5320 KB |
Correct |
19 |
Correct |
367 ms |
5828 KB |
Correct |
20 |
Correct |
450 ms |
6092 KB |
Correct |
21 |
Correct |
434 ms |
6256 KB |
Correct |
22 |
Correct |
426 ms |
6084 KB |
Correct |
23 |
Correct |
446 ms |
6084 KB |
Correct |
24 |
Correct |
425 ms |
6008 KB |
Correct |
25 |
Correct |
430 ms |
5852 KB |
Correct |
26 |
Correct |
278 ms |
3796 KB |
Correct |
27 |
Correct |
153 ms |
2696 KB |
Correct |
28 |
Correct |
394 ms |
5380 KB |
Correct |
29 |
Correct |
376 ms |
5368 KB |
Correct |
30 |
Correct |
409 ms |
5352 KB |
Correct |
31 |
Correct |
367 ms |
5332 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
460 KB |
Correct |
3 |
Correct |
8 ms |
2524 KB |
Correct |
4 |
Correct |
1 ms |
348 KB |
Correct |
5 |
Correct |
9 ms |
2780 KB |
Correct |
6 |
Correct |
12 ms |
2776 KB |
Correct |
7 |
Correct |
0 ms |
464 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
2 ms |
1116 KB |
Correct |
10 |
Correct |
0 ms |
348 KB |
Correct |
11 |
Correct |
19 ms |
2780 KB |
Correct |
12 |
Correct |
31 ms |
856 KB |
Correct |
13 |
Correct |
370 ms |
6596 KB |
Correct |
14 |
Correct |
421 ms |
6596 KB |
Correct |
15 |
Correct |
0 ms |
344 KB |
Correct |
16 |
Correct |
0 ms |
344 KB |
Correct |
17 |
Correct |
210 ms |
6636 KB |
Correct |
18 |
Correct |
204 ms |
6596 KB |
Correct |
19 |
Correct |
358 ms |
6596 KB |
Correct |
20 |
Correct |
171 ms |
4500 KB |
Correct |
21 |
Correct |
173 ms |
3936 KB |
Correct |
22 |
Correct |
363 ms |
5376 KB |
Correct |
23 |
Correct |
353 ms |
5576 KB |
Correct |
24 |
Correct |
370 ms |
5832 KB |
Correct |
25 |
Correct |
357 ms |
5320 KB |
Correct |
26 |
Correct |
0 ms |
600 KB |
Correct |
27 |
Correct |
1 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 |
500 KB |
Correct |
34 |
Correct |
0 ms |
348 KB |
Correct |
35 |
Correct |
0 ms |
348 KB |
Correct |
36 |
Correct |
1 ms |
348 KB |
Correct |
37 |
Correct |
1 ms |
348 KB |
Correct |
38 |
Correct |
1 ms |
344 KB |
Correct |
39 |
Correct |
0 ms |
348 KB |
Correct |
40 |
Correct |
0 ms |
348 KB |
Correct |
41 |
Correct |
1 ms |
348 KB |
Correct |
42 |
Correct |
0 ms |
348 KB |
Correct |
43 |
Correct |
0 ms |
348 KB |
Correct |
44 |
Correct |
63 ms |
3032 KB |
Correct |
45 |
Correct |
455 ms |
6684 KB |
Correct |
46 |
Correct |
431 ms |
6600 KB |
Correct |
47 |
Correct |
439 ms |
6600 KB |
Correct |
48 |
Correct |
465 ms |
6780 KB |
Correct |
49 |
Correct |
489 ms |
6640 KB |
Correct |
50 |
Correct |
457 ms |
6592 KB |
Correct |
51 |
Correct |
457 ms |
6576 KB |
Correct |
52 |
Correct |
427 ms |
6600 KB |
Correct |
53 |
Correct |
476 ms |
6600 KB |
Correct |
54 |
Correct |
0 ms |
344 KB |
Correct |
55 |
Correct |
0 ms |
348 KB |
Correct |
56 |
Correct |
192 ms |
4368 KB |
Correct |
57 |
Correct |
235 ms |
4560 KB |
Correct |
58 |
Correct |
219 ms |
4552 KB |
Correct |
59 |
Correct |
319 ms |
5316 KB |
Correct |
60 |
Correct |
342 ms |
5320 KB |
Correct |
61 |
Correct |
367 ms |
5828 KB |
Correct |
62 |
Correct |
450 ms |
6092 KB |
Correct |
63 |
Correct |
434 ms |
6256 KB |
Correct |
64 |
Correct |
426 ms |
6084 KB |
Correct |
65 |
Correct |
446 ms |
6084 KB |
Correct |
66 |
Correct |
425 ms |
6008 KB |
Correct |
67 |
Correct |
430 ms |
5852 KB |
Correct |
68 |
Correct |
278 ms |
3796 KB |
Correct |
69 |
Correct |
153 ms |
2696 KB |
Correct |
70 |
Correct |
394 ms |
5380 KB |
Correct |
71 |
Correct |
376 ms |
5368 KB |
Correct |
72 |
Correct |
409 ms |
5352 KB |
Correct |
73 |
Correct |
367 ms |
5332 KB |
Correct |
74 |
Correct |
32 ms |
2800 KB |
Correct |
75 |
Correct |
413 ms |
6596 KB |
Correct |
76 |
Correct |
558 ms |
6616 KB |
Correct |
77 |
Correct |
0 ms |
348 KB |
Correct |
78 |
Correct |
144 ms |
6548 KB |
Correct |
79 |
Correct |
369 ms |
6728 KB |
Correct |
80 |
Correct |
175 ms |
6684 KB |
Correct |
81 |
Correct |
218 ms |
4520 KB |
Correct |
82 |
Correct |
254 ms |
4552 KB |
Correct |
83 |
Correct |
232 ms |
4592 KB |
Correct |
84 |
Correct |
42 ms |
1760 KB |
Correct |
85 |
Correct |
378 ms |
5588 KB |
Correct |
86 |
Correct |
441 ms |
5712 KB |
Correct |
87 |
Correct |
361 ms |
5312 KB |
Correct |
88 |
Correct |
12 ms |
2780 KB |
Correct |
89 |
Correct |
11 ms |
2780 KB |
Correct |