#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5, mx=1e9+5, kx=10, bx=3;
int n, m, s[nx], f[nx], dr[nx], rv[nx][kx], l=0, r=1e9+5;
pair<int, int> dp[nx][kx];
bool solve(int x)
{
for (int i=1; i<=n; i++) dr[i]=0;
vector<int> vs(n+1), p, w;
vector<pair<int, int>> range;
p.push_back(0);
for (int i=4; i<=n; i++)
{
if (!range.empty()&&range.back().second>=s[i]-1) vs[i]=1, range.back().second=s[i]+x;
else if (s[i]-s[i-3]<=x) dr[i-3]=dr[i-1]=1, vs[i-3]=vs[i-2]=vs[i-1]=vs[i]=1, range.push_back({s[i-3]-x, s[i]+x});
}
for (int i=1; i<=n; i++) if (!vs[i]) p.push_back(i);
int idx=0;
for (int i=1; i<=m; i++)
{
while (idx<range.size()&&f[i]>range[idx].second) idx++;
if (range.empty()||f[i]<range[idx].first||f[i]>range[idx].second) w.push_back(i);
}
w.push_back(m+1);
vector<int> nxt(p.size()), nxtl(p.size());
idx=0;
for (int i=0; i<p.size(); i++)
{
while (f[w[idx]]<=s[p[i]]) idx++;
nxt[i]=idx;
}
idx=0;
for (int i=0; i<p.size(); i++)
{
while (f[w[idx]]<=s[p[i]]+x) idx++;
nxtl[i]=idx;
}
if (p.size()==1) return (w.size()==1);
for (int i=0; i<(1<<bx); i++) dp[0][i]={0, 0};
for (int i=1; i<p.size(); i++)
{
for (int msk=0; msk<(1<<bx); msk++)
{
int a=msk/2, b=msk/2+(1<<(bx-1));
if (dp[i-1][a].first<dp[i-1][b].first) swap(a, b);
int loc=f[w[dp[i-1][a].first]];
rv[i][msk]=a;
if (msk&1)
{
if (loc>=s[p[i]]-x&&loc<=s[p[i]]) dp[i][msk]={max(dp[i-1][a].second, nxt[i]), max(dp[i-1][a].second, nxt[i])};
else dp[i][msk]=dp[i-1][a];
}
else
{
if (loc>=s[p[i]]) dp[i][msk]={nxtl[i], nxtl[i]};
else dp[i][msk]={dp[i-1][a].first, nxtl[i]};
}
}
}
for (int i=0; i<(1<<bx); i++)
{
if (dp[p.size()-1][i].first==w.size()-1)
{
int cur=i;
for (int j=p.size()-1; j>0; j--) dr[p[j]]=cur%2, cur=rv[j][cur];
return 1;
}
}
return 0;
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>m;
for (int i=1; i<=n; i++) cin>>s[i];
for (int i=1; i<=m; i++) cin>>f[i];
s[0]=-2e9;
f[m+1]=INT_MAX;
while (l<r)
{
int md=(l+r)/2;
if (solve(md)) r=md;
else l=md+1;
}
if (l==mx) return cout<<-1, 0;
solve(l);
cout<<l<<'\n';
for (int i=1; i<=n; i++) cout<<(dr[i]?'L':'R');
}
Compilation message
Main.cpp: In function 'bool solve(int)':
Main.cpp:25:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while (idx<range.size()&&f[i]>range[idx].second) idx++;
| ~~~^~~~~~~~~~~~~
Main.cpp:31:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (int i=0; i<p.size(); i++)
| ~^~~~~~~~~
Main.cpp:37:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i=0; i<p.size(); i++)
| ~^~~~~~~~~
Main.cpp:44:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
44 | for (int i=1; i<p.size(); i++)
| ~^~~~~~~~~
Main.cpp:66:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | if (dp[p.size()-1][i].first==w.size()-1)
| ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Correct |
2 |
Correct |
0 ms |
2508 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2508 KB |
Correct |
2 |
Correct |
28 ms |
4180 KB |
Correct |
3 |
Correct |
1 ms |
2392 KB |
Correct |
4 |
Correct |
25 ms |
4524 KB |
Correct |
5 |
Correct |
25 ms |
4528 KB |
Correct |
6 |
Correct |
1 ms |
2396 KB |
Correct |
7 |
Correct |
0 ms |
2396 KB |
Correct |
8 |
Correct |
5 ms |
2936 KB |
Correct |
9 |
Correct |
0 ms |
2396 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Correct |
2 |
Correct |
24 ms |
4484 KB |
Correct |
3 |
Correct |
3 ms |
2652 KB |
Correct |
4 |
Correct |
28 ms |
5284 KB |
Correct |
5 |
Correct |
28 ms |
5320 KB |
Correct |
6 |
Correct |
1 ms |
2392 KB |
Correct |
7 |
Correct |
0 ms |
2392 KB |
Correct |
8 |
Correct |
27 ms |
5312 KB |
Correct |
9 |
Correct |
28 ms |
5320 KB |
Correct |
10 |
Correct |
28 ms |
5468 KB |
Correct |
11 |
Correct |
16 ms |
4564 KB |
Correct |
12 |
Correct |
17 ms |
4040 KB |
Correct |
13 |
Correct |
29 ms |
9788 KB |
Correct |
14 |
Correct |
37 ms |
12288 KB |
Correct |
15 |
Correct |
39 ms |
12208 KB |
Correct |
16 |
Correct |
27 ms |
11732 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Correct |
2 |
Correct |
0 ms |
2508 KB |
Correct |
3 |
Correct |
1 ms |
2396 KB |
Correct |
4 |
Correct |
0 ms |
2396 KB |
Correct |
5 |
Correct |
0 ms |
2396 KB |
Correct |
6 |
Correct |
1 ms |
2396 KB |
Correct |
7 |
Correct |
1 ms |
2396 KB |
Correct |
8 |
Correct |
1 ms |
2396 KB |
Correct |
9 |
Correct |
0 ms |
2396 KB |
Correct |
10 |
Correct |
1 ms |
2396 KB |
Correct |
11 |
Correct |
1 ms |
2392 KB |
Correct |
12 |
Correct |
1 ms |
2396 KB |
Correct |
13 |
Correct |
1 ms |
2396 KB |
Correct |
14 |
Correct |
1 ms |
2396 KB |
Correct |
15 |
Correct |
1 ms |
2396 KB |
Correct |
16 |
Correct |
0 ms |
2396 KB |
Correct |
17 |
Correct |
1 ms |
2396 KB |
Correct |
18 |
Correct |
0 ms |
2396 KB |
Correct |
19 |
Correct |
1 ms |
2396 KB |
Correct |
20 |
Correct |
1 ms |
2396 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Correct |
2 |
Correct |
34 ms |
7336 KB |
Correct |
3 |
Correct |
113 ms |
17380 KB |
Correct |
4 |
Correct |
117 ms |
17368 KB |
Correct |
5 |
Correct |
113 ms |
17416 KB |
Correct |
6 |
Correct |
112 ms |
17380 KB |
Correct |
7 |
Correct |
130 ms |
17372 KB |
Correct |
8 |
Correct |
115 ms |
17380 KB |
Correct |
9 |
Correct |
127 ms |
17368 KB |
Correct |
10 |
Correct |
116 ms |
17348 KB |
Correct |
11 |
Correct |
115 ms |
17384 KB |
Correct |
12 |
Correct |
1 ms |
2392 KB |
Correct |
13 |
Correct |
1 ms |
2648 KB |
Correct |
14 |
Correct |
56 ms |
15924 KB |
Correct |
15 |
Correct |
58 ms |
16052 KB |
Correct |
16 |
Correct |
57 ms |
15984 KB |
Correct |
17 |
Correct |
22 ms |
3796 KB |
Correct |
18 |
Correct |
24 ms |
4048 KB |
Correct |
19 |
Correct |
24 ms |
4304 KB |
Correct |
20 |
Correct |
76 ms |
16924 KB |
Correct |
21 |
Correct |
76 ms |
16940 KB |
Correct |
22 |
Correct |
49 ms |
14976 KB |
Correct |
23 |
Correct |
52 ms |
15076 KB |
Correct |
24 |
Correct |
30 ms |
4560 KB |
Correct |
25 |
Correct |
25 ms |
4560 KB |
Correct |
26 |
Correct |
61 ms |
14100 KB |
Correct |
27 |
Correct |
45 ms |
11112 KB |
Correct |
28 |
Correct |
34 ms |
11988 KB |
Correct |
29 |
Correct |
29 ms |
11984 KB |
Correct |
30 |
Correct |
32 ms |
11960 KB |
Correct |
31 |
Correct |
41 ms |
14816 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Correct |
2 |
Correct |
0 ms |
2508 KB |
Correct |
3 |
Correct |
28 ms |
4180 KB |
Correct |
4 |
Correct |
1 ms |
2392 KB |
Correct |
5 |
Correct |
25 ms |
4524 KB |
Correct |
6 |
Correct |
25 ms |
4528 KB |
Correct |
7 |
Correct |
1 ms |
2396 KB |
Correct |
8 |
Correct |
0 ms |
2396 KB |
Correct |
9 |
Correct |
5 ms |
2936 KB |
Correct |
10 |
Correct |
0 ms |
2396 KB |
Correct |
11 |
Correct |
24 ms |
4484 KB |
Correct |
12 |
Correct |
3 ms |
2652 KB |
Correct |
13 |
Correct |
28 ms |
5284 KB |
Correct |
14 |
Correct |
28 ms |
5320 KB |
Correct |
15 |
Correct |
1 ms |
2392 KB |
Correct |
16 |
Correct |
0 ms |
2392 KB |
Correct |
17 |
Correct |
27 ms |
5312 KB |
Correct |
18 |
Correct |
28 ms |
5320 KB |
Correct |
19 |
Correct |
28 ms |
5468 KB |
Correct |
20 |
Correct |
16 ms |
4564 KB |
Correct |
21 |
Correct |
17 ms |
4040 KB |
Correct |
22 |
Correct |
29 ms |
9788 KB |
Correct |
23 |
Correct |
37 ms |
12288 KB |
Correct |
24 |
Correct |
39 ms |
12208 KB |
Correct |
25 |
Correct |
27 ms |
11732 KB |
Correct |
26 |
Correct |
1 ms |
2396 KB |
Correct |
27 |
Correct |
0 ms |
2396 KB |
Correct |
28 |
Correct |
0 ms |
2396 KB |
Correct |
29 |
Correct |
1 ms |
2396 KB |
Correct |
30 |
Correct |
1 ms |
2396 KB |
Correct |
31 |
Correct |
1 ms |
2396 KB |
Correct |
32 |
Correct |
0 ms |
2396 KB |
Correct |
33 |
Correct |
1 ms |
2396 KB |
Correct |
34 |
Correct |
1 ms |
2392 KB |
Correct |
35 |
Correct |
1 ms |
2396 KB |
Correct |
36 |
Correct |
1 ms |
2396 KB |
Correct |
37 |
Correct |
1 ms |
2396 KB |
Correct |
38 |
Correct |
1 ms |
2396 KB |
Correct |
39 |
Correct |
0 ms |
2396 KB |
Correct |
40 |
Correct |
1 ms |
2396 KB |
Correct |
41 |
Correct |
0 ms |
2396 KB |
Correct |
42 |
Correct |
1 ms |
2396 KB |
Correct |
43 |
Correct |
1 ms |
2396 KB |
Correct |
44 |
Correct |
34 ms |
7336 KB |
Correct |
45 |
Correct |
113 ms |
17380 KB |
Correct |
46 |
Correct |
117 ms |
17368 KB |
Correct |
47 |
Correct |
113 ms |
17416 KB |
Correct |
48 |
Correct |
112 ms |
17380 KB |
Correct |
49 |
Correct |
130 ms |
17372 KB |
Correct |
50 |
Correct |
115 ms |
17380 KB |
Correct |
51 |
Correct |
127 ms |
17368 KB |
Correct |
52 |
Correct |
116 ms |
17348 KB |
Correct |
53 |
Correct |
115 ms |
17384 KB |
Correct |
54 |
Correct |
1 ms |
2392 KB |
Correct |
55 |
Correct |
1 ms |
2648 KB |
Correct |
56 |
Correct |
56 ms |
15924 KB |
Correct |
57 |
Correct |
58 ms |
16052 KB |
Correct |
58 |
Correct |
57 ms |
15984 KB |
Correct |
59 |
Correct |
22 ms |
3796 KB |
Correct |
60 |
Correct |
24 ms |
4048 KB |
Correct |
61 |
Correct |
24 ms |
4304 KB |
Correct |
62 |
Correct |
76 ms |
16924 KB |
Correct |
63 |
Correct |
76 ms |
16940 KB |
Correct |
64 |
Correct |
49 ms |
14976 KB |
Correct |
65 |
Correct |
52 ms |
15076 KB |
Correct |
66 |
Correct |
30 ms |
4560 KB |
Correct |
67 |
Correct |
25 ms |
4560 KB |
Correct |
68 |
Correct |
61 ms |
14100 KB |
Correct |
69 |
Correct |
45 ms |
11112 KB |
Correct |
70 |
Correct |
34 ms |
11988 KB |
Correct |
71 |
Correct |
29 ms |
11984 KB |
Correct |
72 |
Correct |
32 ms |
11960 KB |
Correct |
73 |
Correct |
41 ms |
14816 KB |
Correct |
74 |
Correct |
25 ms |
4712 KB |
Correct |
75 |
Correct |
29 ms |
7860 KB |
Correct |
76 |
Correct |
30 ms |
7708 KB |
Correct |
77 |
Correct |
1 ms |
2392 KB |
Correct |
78 |
Correct |
28 ms |
5332 KB |
Correct |
79 |
Correct |
28 ms |
5328 KB |
Correct |
80 |
Correct |
29 ms |
5468 KB |
Correct |
81 |
Correct |
25 ms |
11728 KB |
Correct |
82 |
Correct |
17 ms |
6864 KB |
Correct |
83 |
Correct |
24 ms |
6856 KB |
Correct |
84 |
Correct |
8 ms |
2908 KB |
Correct |
85 |
Correct |
45 ms |
12268 KB |
Correct |
86 |
Correct |
51 ms |
12388 KB |
Correct |
87 |
Correct |
42 ms |
14852 KB |
Correct |
88 |
Correct |
27 ms |
4432 KB |
Correct |
89 |
Correct |
30 ms |
4528 KB |
Correct |