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>
#define x first
#define y second
#define all(v) v.begin(), v.end()
#define chkmin(a, b) a = min(a, b)
#define chkmax(a, b) a = max(a, b)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
const int MAX_N = 1e5 + 5;
ll s[MAX_N], f[MAX_N];
int ri[MAX_N], flip[MAX_N];
int n, m;
bool Solve(int k) {
int i = 0, j = 0;
vi leftest(n);
while (i < n && j < m) {
flip[i] = 0;
if (f[j] >= s[i]) {
leftest[i] = -1;
while (j < m && f[j] - s[i] <= k) j++;
ri[i] = 1;
i++;
continue;
}
if (s[i] - f[j] > k) return 0;
ri[i] = 0;
leftest[i] = j;
if (i && leftest[i - 1] != -1 && s[i] - f[leftest[i - 1]] <= k) {
flip[i] = 1;
while (j < m && f[j] - s[i - 1] <= k) j++;
ri[i - 1] = 1;
}
while (j < m && f[j] <= s[i] && s[i] - f[j] <= k) j++;
i++;
}
if (j != m) return 0;
for (i = n - 1; i >= 0; i--) {
if (flip[i]) {
for (j = i; flip[j - 1]; j--);
j--;
for (int a = i, b = 0; a >= j; a--, b ^= 1) ri[a] = b;
i = j + 1;
}
}
return 1;
}
int Solve2(int k) {
for (int i = 0; i < (1 << n); i++) {
vii ranges;
for (int j = 0; j < n; j++) {
if ((i >> j) & 1) ranges.push_back({s[j], s[j] + k});
else ranges.push_back({s[j] - k, s[j]});
}
sort(all(ranges));
int a = 0, b = 0;
while (a < n && b < m) {
if (ranges[a].x <= f[b] && f[b] <= ranges[a].y) b++;
else a++;
}
if (b == m) {
return i + 1;
}
}
return 0;
}
int Ans() {
int begin = 0, end = 1e9 + 1;
while (begin < end) {
int mid = (begin + end) >> 1;
// bool b1 = Solve(mid), b2 = Solve2(mid);
// if (b1 != b2) {
// cout << "BAD\n";
// cout << n << ' ' << m << '\n';
// for (int i = 0; i < n; i++) cout << s[i] << ' ';
// cout << '\n';
// for (int i = 0; i < m; i++) cout << f[i] << ' ';
// exit(0);
// }
if (Solve(mid)) end = mid;
else begin = mid + 1;
}
return end;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
// while (true) {
// for (int i = 0; i < n; i++) {
// s[i] = rand();
// }
// for (int i = 0; i < m; i++) {
// f[i] = rand();
// }
// sort(s, s + n);
// sort(f, f + m);
// Ans();
// }
for (int i = 0; i < n; i++) cin >> s[i];
for (int i = 0; i < m; i++) cin >> f[i];
int end = Ans();
// for (end = 0; end <= 8; end++) {
// if (Solve(end)) break;
// }
int x = Solve(end);
if (!x) {
cout << -1 << '\n';
return 0;
}
// x--;
// for (int i = 0; i < n; i++) {
// cout << ((x >> i) & 1 ? 'R' : 'L');
// }
cout << end << '\n';
for (int i = 0; i < n; i++) {
cout << (ri[i] ? 'R' : 'L');
}
}
/*
3 5
6 14 25
0 13 19 21 28
*/
# | 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... |