#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<cassert>
#include<unordered_map>
#include <queue>
#include <cstdint>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric>
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-lopps")
#define int long long
using namespace std;
const int mod=1e9+7,mxn=2e5+5,inf=1e18,minf=-1e18,lg=30;
//#undef int
int n,k,m,w,h,l;
void setIO(string name){
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((name+".in").c_str(),"r",stdin);
freopen((name+".out").c_str(),"w",stdout);
}
int mid;
int dp[mxn+10],from[mxn+10],di[mxn+10],d[mxn+10];
vector<int>F,S;
//max prefix flower we can get
int getR(int x){return ub(all(F),x)-F.begin()-1;}
int getL(int x){return lb(all(F),x)-F.begin();}
bool check(){
dp[0]=0;
for(int i=1;i<=n;i++){
dp[i]=dp[i-1];
from[i]=i-1;
if(dp[i-1]+1>=getL(S[i]-mid)){
int x=getR(S[i]);
if(x>dp[i])dp[i]=x,di[i]=0,from[i]=i-1;
}
if(dp[i-1]+1>=getL(S[i])){
int x=getR(S[i]+mid);
if(x>dp[i])dp[i]=x,di[i]=1,from[i]=i-1;
}
if(i>1&&dp[i-2]+1>=getL(S[i]-mid)){
int x=getR(S[i-1]+mid);
if(x>dp[i])dp[i]=x,di[i]=1,from[i]=i-2;
}
}
int cur=n;
while(cur>0){
if(cur-from[cur]==2)di[cur]=0,di[cur-1]=1;
cur=from[cur];
}
if(dp[n]==m)return 1;
return 0;
}
int32_t main(){
fastio
cin>>n>>m;
S.resize(n+1,-1);
F.resize(m+1,-1);
for(int i=1;i<=n;i++)cin>>S[i];
for(int i=1;i<=m;i++)cin>>F[i];
int l=0,r=1e9,ans=inf;
while(l<=r){
mid=l+(r-l)/2;
if(check())r=mid-1,ans=min(ans,mid);
else l=mid+1;
}
if(ans==inf)return (cout<<-1),0;
cout<<ans<<'\n';
mid=ans;
check();
for(int i=1;i<=n;i++)cout<<((di[i])?'R':'L');
}
/*
keep dp
we wanted to keep dp[i]-> max pref of F we can cover
but in a prefix of an optimal configuration it doesnt always cover prefix of F
ex. case RL
is this the only case?
can it be RRL?? no? because if the last L cover the first prefix we can turn the 2nd R to L to get RLR
why cant we greedy check tho???????
*/
Compilation message (stderr)
Main.cpp:32:40: warning: bad option '-funroll-lopps' to pragma 'optimize' [-Wpragmas]
32 | #pragma GCC optimize ("03,unroll-lopps")
| ^
Main.cpp:38:23: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
38 | void setIO(string name){
| ^
Main.cpp:47:15: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
47 | int getR(int x){return ub(all(F),x)-F.begin()-1;}
| ^
Main.cpp:48:15: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
48 | int getL(int x){return lb(all(F),x)-F.begin();}
| ^
Main.cpp:49:12: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
49 | bool check(){
| ^
Main.cpp:75:14: warning: bad option '-funroll-lopps' to attribute 'optimize' [-Wattributes]
75 | int32_t main(){
| ^
Main.cpp: In function 'void setIO(std::string)':
Main.cpp:40:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen((name+".in").c_str(),"r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:41:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | freopen((name+".out").c_str(),"w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |