#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9;
int main() {
int n, m;
cin >> n >> m;
vector<int> spr;
vector<int> fl;
map<int,int> fli;
for (int i = 0; i < n; i ++) {
int x;
cin >> x;
spr.push_back(x);
}
for (int i = 0; i < m; i ++) {
int x;
cin >> x;
fl.push_back(x);
fli[x] = i;
}
fli[2 * inf] = m;
int lower = 0, upper = inf + 2;
while (1) {
int kval = (lower + upper) / 2 - 1;
if (lower + 1 >= upper) {
if (lower == inf + 1) {
cout << "-1\n";
return 0;
} else {
kval = lower;
}
}
vector<pair<int, int>> dp(n + 1);
for (int i = 0; i < n; i ++) {
pair<int, int> curval = dp[i];
dp[i + 1] = max(dp[i + 1], {curval.first, 0});
if (curval.first < m) {
if (fl[curval.first] >= spr[i] - kval) {
dp[i + 1] = max(dp[i + 1], {fli.upper_bound(spr[i])->second, -1});
}
if (fl[curval.first] >= spr[i]) {
dp[i + 1] = max(dp[i + 1], {fli.upper_bound(spr[i] + kval)->second, 1});
}
if (i + 1 < n && spr[i] <= spr[i] + kval) {
if (fl[curval.first] >= spr[i + 1] - kval) {
dp[i + 2] = max(dp[i + 2], {fli.upper_bound(spr[i] + kval)->second, 2});
}
}
}
}
if (lower + 1 >= upper) {
cout << lower << '\n';
string rec(n, ' ');
int ind = n;
while (ind) {
if (dp[ind].second == -1) {
rec[ind - 1] = 'L';
ind --;
} else if (dp[ind].second == 1) {
rec[ind - 1] = 'R';
ind --;
} else if (dp[ind].second == 2) {
rec[ind - 1] = 'L';
rec[ind - 2] = 'R';
ind -= 2;
} else {
rec[ind - 1] = 'L';
ind --;
}
}
cout << rec << '\n';
return 0;
}
if (dp.back().first == m) {
upper = kval + 1;
} else {
lower = kval + 1;
}
}
}
# | 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... |