This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define sz(v) int(v.size())
#define ar array
typedef long long ll;
const int N = 1e5+10, MOD = 1e9+7;
const int INF = 2e9+10;
void solve() {
int n, m; cin >> n >> m;
vector<int> a(n); for (auto& x : a) cin >> x;
vector<int> b(m); for (auto& x : b) cin >> x;
vector<string> build(n + 1);
auto can = [&](int x) {
vector<int> dp(n+1);
auto upd = [&](int i, int me, string c) {
if (dp[i] <= me) {
dp[i] = me;
build[i] = c;
}
};
for (int i = 0; i < n; i++) {
// dp[i+1] = max(dp[i+1], dp[i]);
upd(i+1, dp[i], "L");
if (dp[i] >= m) continue;
int need = b[dp[i]];
if (a[i] - x <= need && need <= a[i]) {
int cur = upper_bound(b.begin(), b.end(), a[i]) - b.begin();
upd(i+1, cur, "L");
// dp[i+1] = max(dp[i+1], cur);
}
if (a[i] <= need && need <= a[i] + x) {
int cur = upper_bound(b.begin(), b.end(), a[i] + x) - b.begin();
upd(i+1, cur, "R");
// dp[i+1] = max(dp[i+1], cur);
}
if (i < n-1 && a[i+1] <= a[i] + x) {
if (a[i+1] - x <= need && need <= a[i] + x) {
int cur = upper_bound(b.begin(), b.end(), a[i] + x) - b.begin();
upd(i+2, cur, "RL");
// dp[i+2] = max(dp[i+2], cur);
}
}
}
return *max_element(dp.begin(), dp.end()) >= m;
};
int lo = 0, hi = MOD, mid = (lo + hi) / 2;
while (lo < mid && mid < hi) {
if (can(mid)) hi = mid;
else lo = mid;
mid = (lo + hi) / 2;
}
if (!can(hi)) {
cout << -1 << '\n';
return;
}
vector<char> ans(n, '?');
int c = n;
while (c > 0) {
string p = build[c];
if (sz(p) == 1) {
ans[c-1] = p[0];
c--;
} else if (sz(p) == 2) {
ans[c-1] = p[1];
ans[c-2] = p[0];
c -= 2;
} else assert(false);
}
vector<int> ps(m + 1);
for (int i = 0; i < n; i++) {
int l = -1, r = -1;
if (ans[i] == 'L') {
l = a[i] - hi, r = a[i];
} else if (ans[i] == 'R') {
l = a[i], r = a[i] + hi;
} else assert(false);
int L = lower_bound(b.begin(), b.end(), l) - b.begin();
int R = upper_bound(b.begin(), b.end(), r) - b.begin() - 1;
if (L <= R) {
ps[L]++;
ps[R+1]--;
}
}
for (int i = 1; i <= m; i++)
ps[i] += ps[i-1];
for (int i = 0; i < m; i++) {
assert(ps[i]);
}
cout << hi << '\n';
for (char c : ans) cout << c;
cout << '\n';
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int T = 1;
// cin >> T;
while (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... |