#include "bits/stdc++.h"
#define int long long
#define all(v) v.begin() , v.end()
#define sz(a) (int)a.size()
using namespace std;
int n,m;
vector<int> a,b;
void _(){
cin >> n >> m;
for(int i=0;i<n;i++){
int x; cin >> x;
a.push_back(x);
}
for(int i=0;i<m;i++){
int x;cin >> x;
b.push_back(x);
}
auto check = [&](int pow)->pair<bool,vector<char>>{
int p=0;
vector<char> res(n,'L');
for(int i=0;i<m;){
if(p==n) return {0,res};
int lol=-1;
while(p<n && a[p]<=b[i]){
res[p]='R';
lol=max(lol,a[p]+pow);
p++;
}
if(lol>=b[i]){
while(i<m && lol>=b[i]) i++;
continue;
}
if(i==m) return {1,res};
if(p==n) return {0,res};
vector<int> f;
while(p<n && a[p]-pow<=b[i]) f.push_back(p++);
if(sz(f)==0) return {0,res};
if(sz(f)==1){
while(i<m && b[i]<=a[f[0]]) i++;
continue;
}
if(sz(f)>=3){
for(int j=0;j<sz(f);j++){
if(j==sz(f)-2) continue;
res[f[j]]='R';
}
while(i<m && b[i]<=a[f.back()]+pow) i++;
continue;
}
while(i<m && b[i]<=a[f[0]]) i++;
if(i==m) return {1,res};
if(b[i]>=a[f[1]]){
res[f[1]]='R';
while(i<m && b[i]<=a[f[1]]+pow) i++;
}
else{
res[f[0]]='R';
while(i<m && b[i]<=a[f[0]]+pow) i++;
}
}
return {1,res};
};
auto check2 = [&](int pow)->pair<bool,vector<char>>{
int p=n-1;
vector<char> res(n,'R');
for(int i=m-1;i>=0;){
if(p<0) return {0,res};
int lol=(int)1e18;
while(p>=0 && a[p]>=b[i]){
res[p]='L';
lol=min(lol,a[p]-pow);
p--;
}
if(lol<=b[i]){
while(i>=0 && lol<=b[i]) i--;
continue;
}
if(i<0) return {1,res};
if(p<0) return {0,res};
vector<int> f;
while(p>=0 && a[p]+pow>=b[i]) f.push_back(p--);
if(sz(f)==0) return {0,res};
if(sz(f)==1){
while(i>=0 && b[i]>=a[f[0]]) i--;
continue;
}
if(sz(f)>=3){
for(int j=0;j<sz(f);j++){
if(j==1) continue;
res[f[j]]='L';
}
while(i>=0 && b[i]>=a[f.back()]-pow) i--;
continue;
}
while(i>=0 && b[i]>=a[f[0]]) i--;
if(i<0) return {1,res};
if(b[i]<=a[f[1]]){
res[f[1]]='L';
while(i>=0 && b[i]>=a[f[1]]-pow) i--;
}
else{
res[f[0]]='L';
while(i>=0 && b[i]>=a[f[0]]-pow) i--;
}
}
return {1,res};
};
int l=0,r=(int)1e18;
while(l<r){
int m=(l+r)/2;
if(check(m).first || check2(m).first) r=m;
else l=m+1;
}
if(r==(int)1e18) cout << -1 << '\n';
else if(check2(m).first){
auto X = check2(l);
cout << l << '\n';
for(char x:X.second) cout << x;
cout << '\n';
}
else{
auto X = check(l);
cout << l << '\n';
for(char x:X.second) cout << x;
cout << '\n';
}
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int tc=1;//cin >> tc;
while(tc--) _();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
8 ms |
1500 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Correct |
9 ms |
1500 KB |
Correct |
5 |
Correct |
9 ms |
1496 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
2 ms |
736 KB |
Correct |
9 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
16 ms |
1504 KB |
Correct |
3 |
Correct |
5 ms |
844 KB |
Correct |
4 |
Correct |
64 ms |
3984 KB |
Correct |
5 |
Correct |
61 ms |
3992 KB |
Correct |
6 |
Correct |
0 ms |
344 KB |
Correct |
7 |
Correct |
0 ms |
344 KB |
Correct |
8 |
Correct |
81 ms |
4124 KB |
Correct |
9 |
Correct |
93 ms |
4076 KB |
Correct |
10 |
Correct |
91 ms |
4048 KB |
Correct |
11 |
Correct |
13 ms |
1556 KB |
Correct |
12 |
Correct |
31 ms |
2376 KB |
Correct |
13 |
Correct |
74 ms |
3760 KB |
Correct |
14 |
Correct |
65 ms |
3760 KB |
Correct |
15 |
Correct |
27 ms |
2772 KB |
Correct |
16 |
Correct |
66 ms |
3772 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
0 ms |
344 KB |
Correct |
4 |
Correct |
0 ms |
344 KB |
Correct |
5 |
Correct |
0 ms |
348 KB |
Correct |
6 |
Correct |
1 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 |
1 ms |
344 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 |
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 |
0 ms |
348 KB |
Correct |
19 |
Correct |
1 ms |
344 KB |
Correct |
20 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
16 ms |
1488 KB |
Correct |
3 |
Correct |
45 ms |
2776 KB |
Correct |
4 |
Correct |
44 ms |
2772 KB |
Correct |
5 |
Correct |
44 ms |
2776 KB |
Correct |
6 |
Correct |
45 ms |
2776 KB |
Correct |
7 |
Correct |
75 ms |
4020 KB |
Correct |
8 |
Correct |
45 ms |
2772 KB |
Correct |
9 |
Correct |
47 ms |
2772 KB |
Correct |
10 |
Correct |
78 ms |
4004 KB |
Correct |
11 |
Correct |
43 ms |
2776 KB |
Correct |
12 |
Correct |
0 ms |
344 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
14 ms |
1552 KB |
Correct |
15 |
Correct |
19 ms |
1500 KB |
Correct |
16 |
Correct |
13 ms |
1500 KB |
Correct |
17 |
Correct |
14 ms |
3676 KB |
Correct |
18 |
Correct |
15 ms |
3676 KB |
Correct |
19 |
Correct |
18 ms |
3672 KB |
Correct |
20 |
Correct |
32 ms |
2672 KB |
Correct |
21 |
Correct |
31 ms |
2776 KB |
Correct |
22 |
Correct |
25 ms |
2772 KB |
Correct |
23 |
Correct |
24 ms |
2772 KB |
Correct |
24 |
Correct |
65 ms |
4012 KB |
Correct |
25 |
Correct |
19 ms |
3412 KB |
Correct |
26 |
Correct |
14 ms |
1496 KB |
Correct |
27 |
Correct |
18 ms |
1244 KB |
Correct |
28 |
Correct |
19 ms |
2772 KB |
Correct |
29 |
Correct |
18 ms |
2772 KB |
Correct |
30 |
Correct |
18 ms |
2776 KB |
Correct |
31 |
Correct |
65 ms |
3672 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
8 ms |
1500 KB |
Correct |
4 |
Correct |
0 ms |
348 KB |
Correct |
5 |
Correct |
9 ms |
1500 KB |
Correct |
6 |
Correct |
9 ms |
1496 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
2 ms |
736 KB |
Correct |
10 |
Correct |
0 ms |
348 KB |
Correct |
11 |
Correct |
16 ms |
1504 KB |
Correct |
12 |
Correct |
5 ms |
844 KB |
Correct |
13 |
Correct |
64 ms |
3984 KB |
Correct |
14 |
Correct |
61 ms |
3992 KB |
Correct |
15 |
Correct |
0 ms |
344 KB |
Correct |
16 |
Correct |
0 ms |
344 KB |
Correct |
17 |
Correct |
81 ms |
4124 KB |
Correct |
18 |
Correct |
93 ms |
4076 KB |
Correct |
19 |
Correct |
91 ms |
4048 KB |
Correct |
20 |
Correct |
13 ms |
1556 KB |
Correct |
21 |
Correct |
31 ms |
2376 KB |
Correct |
22 |
Correct |
74 ms |
3760 KB |
Correct |
23 |
Correct |
65 ms |
3760 KB |
Correct |
24 |
Correct |
27 ms |
2772 KB |
Correct |
25 |
Correct |
66 ms |
3772 KB |
Correct |
26 |
Correct |
0 ms |
344 KB |
Correct |
27 |
Correct |
0 ms |
344 KB |
Correct |
28 |
Correct |
0 ms |
348 KB |
Correct |
29 |
Correct |
1 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 |
1 ms |
344 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 |
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 |
0 ms |
348 KB |
Correct |
42 |
Correct |
1 ms |
344 KB |
Correct |
43 |
Correct |
0 ms |
348 KB |
Correct |
44 |
Correct |
16 ms |
1488 KB |
Correct |
45 |
Correct |
45 ms |
2776 KB |
Correct |
46 |
Correct |
44 ms |
2772 KB |
Correct |
47 |
Correct |
44 ms |
2776 KB |
Correct |
48 |
Correct |
45 ms |
2776 KB |
Correct |
49 |
Correct |
75 ms |
4020 KB |
Correct |
50 |
Correct |
45 ms |
2772 KB |
Correct |
51 |
Correct |
47 ms |
2772 KB |
Correct |
52 |
Correct |
78 ms |
4004 KB |
Correct |
53 |
Correct |
43 ms |
2776 KB |
Correct |
54 |
Correct |
0 ms |
344 KB |
Correct |
55 |
Correct |
0 ms |
348 KB |
Correct |
56 |
Correct |
14 ms |
1552 KB |
Correct |
57 |
Correct |
19 ms |
1500 KB |
Correct |
58 |
Correct |
13 ms |
1500 KB |
Correct |
59 |
Correct |
14 ms |
3676 KB |
Correct |
60 |
Correct |
15 ms |
3676 KB |
Correct |
61 |
Correct |
18 ms |
3672 KB |
Correct |
62 |
Correct |
32 ms |
2672 KB |
Correct |
63 |
Correct |
31 ms |
2776 KB |
Correct |
64 |
Correct |
25 ms |
2772 KB |
Correct |
65 |
Correct |
24 ms |
2772 KB |
Correct |
66 |
Correct |
65 ms |
4012 KB |
Correct |
67 |
Correct |
19 ms |
3412 KB |
Correct |
68 |
Correct |
14 ms |
1496 KB |
Correct |
69 |
Correct |
18 ms |
1244 KB |
Correct |
70 |
Correct |
19 ms |
2772 KB |
Correct |
71 |
Correct |
18 ms |
2772 KB |
Correct |
72 |
Correct |
18 ms |
2776 KB |
Correct |
73 |
Correct |
65 ms |
3672 KB |
Correct |
74 |
Correct |
10 ms |
1508 KB |
Correct |
75 |
Correct |
65 ms |
4004 KB |
Correct |
76 |
Correct |
27 ms |
2776 KB |
Correct |
77 |
Correct |
0 ms |
344 KB |
Correct |
78 |
Correct |
90 ms |
4128 KB |
Correct |
79 |
Correct |
84 ms |
3996 KB |
Correct |
80 |
Correct |
74 ms |
4096 KB |
Correct |
81 |
Correct |
13 ms |
1496 KB |
Correct |
82 |
Correct |
13 ms |
1628 KB |
Correct |
83 |
Correct |
13 ms |
1500 KB |
Correct |
84 |
Correct |
6 ms |
980 KB |
Correct |
85 |
Incorrect |
23 ms |
2664 KB |
User solution is incorrect |
86 |
Halted |
0 ms |
0 KB |
- |