// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n";
const int N=2e5+5;
const int M=1e9+7;
void solve()
{
int n,m;
cin >> n >> m;
vector<int>a(n),b(m);
for (int i=0;i<n;i++){
cin >> a[i];
}
for (int i=0;i<m;i++){
cin >> b[i];
}
if (n==1 and a[0]>b[0] and a[0]<b[m-1]){
cout << -1 << endl;
return;
}
else if (n==1){
if (a[0]>b[0]){
cout << a[0]-b[0] << endl;
cout << 'L' << endl;
}
else{
cout << b[m-1]-a[0] << endl;
cout << 'R' << endl;
}
return;
}
int l=0;
int h=b[m-1]+1;
int mi=(l+h)/2;
vector<pair<int,string>>q;
while (l<h){
int i=0;
queue<int>o;
for (int j:b){
o.push(j);
}
string s;
for (int j:a){
s+='W';
}
while (!o.empty()){
if (i>=n){
break;
}
int he=o.front();
while (!o.empty()){
he =o.front();
if (he<=a[i]+mi and he>=a[i]){
o.pop();
s[i]='R';
}
// else if (i!=n-1){
// s[i]='R';
// i++;
// }
else{
break;
}
}
if (o.empty()){
continue;
}
int f=he+mi;
while (i<n-1 and a[i+1]<=f){
s[i]='R';
i++;
}
if (s[i]=='R'){
break;
}
s[i]='L';
while (!o.empty()){
he =o.front();
if (he>=a[i]-mi and he<=a[i]){
o.pop();
}
else{
break;
}
}
i++;
}
// cout << mi << " " << s << endl;
for (int i=0;i<n;i++){
if (s[i]!='L'){
s[i]='R';
}
}
q.push_back({mi,s});
i=0;
while (!o.empty()){
int he=o.front();
if ((he>=a[i]-mi and he<=a[i]) or (he<=a[i]+mi and he>=a[i])){
o.pop();
}
else if (i!=n-1){
i++;
}
else{
break;
}
}
if (o.empty()){
h=mi;
}
else{
l=mi+1;
}
mi=(l+h)/2;
}
for (auto i:q){
if (i.first==mi){
cout << mi <<endl;
cout << i.second << endl;
break;
}
}
}
signed main()
{
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
cout << fixed<<setprecision(9);
int t=1;
// cin >> t;
for (int _=1;_<=t;_++){
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |