// #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;
}
if (n<=10 and m<=1000){
int l=0;
int h=b[m-1]+1;
int mi=(l+h)/2;
while (l<h){
bool f=0;
for (int x=0;x<pow(2,n);x++){
string s;
for (int i=0;i<n;i++){
if (x&(int)pow(2,i)){
s+='L';
}
else{
s+='R';
}
}
vector<int>vi(m);
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
if (s[j]=='L'){
if (b[i]>=a[j]-mi and b[i]<=a[j]){
vi[i]=1;
}
}
else{
if (b[i]<=a[j]+mi and b[i]>=a[j]){
vi[i]=1;
}
}
}
}
bool y=1;
for (int i:vi){
if (i==0){
y=0;
}
}
if (y){
f=1;
// cout << mi << " " << s << endl;
}
}
if (f){
h=mi;
}
else{
l=mi+1;
}
mi=(l+h)/2;
}
for (int x=0;x<pow(2,n);x++){
string s;
for (int i=0;i<n;i++){
if (x&(int)pow(2,i)){
s+='L';
}
else{
s+='R';
}
}
vector<int>vi(m);
for (int i=0;i<m;i++){
for (int j=0;j<n;j++){
if (s[j]=='L'){
if (b[i]>=a[j]-mi and b[i]<=a[j]){
vi[i]=1;
}
}
else{
if (b[i]<=a[j]+mi and b[i]>=a[j]){
vi[i]=1;
}
}
}
}
bool y=1;
for (int i:vi){
if (i==0){
y=0;
}
}
if (y){
cout << mi << endl;
cout << s << endl;
break;
}
}
return;
}
int l=0;
int h=b[m-1]+1;
int mi=(l+h)/2;
while (l<h){
queue<int>o;
for (int p:b){
o.push(p);
}
for (int i:a){
if (!o.empty()){
int he=o.front();
if (he<i){
while (!o.empty()){
he=o.front();
if (he>=i-mi and he<=i){
o.pop();
}
else{
break;
}
}
}
else{
while (!o.empty()){
he=o.front();
if (he<=i+mi and he>=i){
o.pop();
}
else{
break;
}
}
}
}
}
if (o.empty()){
h=mi;
}
else{
l=mi+1;
}
mi=(l+h)/2;
}
string s;
queue<int>o;
for (int p:b){
o.push(p);
}
for (int i:a){
if (!o.empty()){
int he=o.front();
if (he<i){
s+='L';
while (!o.empty()){
he=o.front();
if (he>=i-mi and he<=i){
o.pop();
}
else{
break;
}
}
}
else{
s+='R';
while (!o.empty()){
he=o.front();
if (he<=i+mi and he>=i){
o.pop();
}
else{
break;
}
}
}
}
else{
s+='R';
}
}
cout << mi << endl;
cout << s << endl;
}
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... |