#include <bits/stdc++.h>
//#include "includeall.h"
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
#define endl '\n'
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define show4(x,y,z, a) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << " " << #a << " is " << a << endl;
#define all(x) x.begin(), x.end()
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
#define FOR(i, x, n) for (ll i =x; i<=n; ++i)
#define RFOR(i, x, n) for (ll i =x; i>=n; --i)
using namespace std;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> pii;
typedef pair<pi, pi> piii;
ll n, m;
vector<ll> xx, yy;
string solve(ll x)
{
vector<ll> dp(n + 5, 0);
vector<ll> bt(n+5, 0);
for (ll i=1; i<=n; ++i)
{
dp[i] = dp[i-1], bt[i] = 0;
// take L
ll from = lb(yy.begin()+1, yy.begin() + m +1, xx[i] - x)-yy.begin();
ll to = ub(yy.begin()+1, yy.begin() + m +1, xx[i])-yy.begin() - 1;
if (from <= dp[i-1] + 1) if (to >= dp[i]) dp[i] = to, bt[i] = 0;
// take R
from = lb(yy.begin()+1, yy.begin() + m +1, xx[i])-yy.begin();
to = ub(yy.begin()+1, yy.begin() + m +1, xx[i] + x)-yy.begin() - 1;
if (from <= dp[i-1] + 1) if (to >= dp[i]) dp[i] = to, bt[i] = 1;
// take RL
if (i > 1 && xx[i-1] + x >= xx[i] - x)
{
from = lb(yy.begin()+1, yy.begin() + m +1, min(xx[i-1], xx[i] - x))-yy.begin();
to = ub(yy.begin()+1, yy.begin() + m +1, xx[i-1] + x)-yy.begin() - 1;
if (from <= dp[i-2] + 1) if (to >= dp[i]) dp[i] = to, bt[i] = 2;
}
}
if (dp[n] < m) return "";
string ans;
ll i = n;
while (i > 0)
{
if (bt[i] == 0) ans += 'L', i--;
else if (bt[i]==1) ans += 'R', i--;
else ans += "LR", i-=2;
}
reverse(all(ans));
return ans;
}
int main()
{
cin >> n >> m;
xx.resize(n+5), yy.resize(m+5);
for (ll i=1; i<=n; ++i) cin >> xx[i];
for (ll i=1; i<=m; ++i) cin >> yy[i];
ll lo = 1, hi = 3e9;
while (lo!=hi)
{
ll mid = (lo+hi) >> 1;
string ret = solve(mid);
if (ret == "") lo = mid+1;
else hi = mid;
}
if (solve(lo)=="") cout << -1 << endl;
else cout << lo << endl << solve(lo) << endl;
return 0;
}
# | 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... |