이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define fi first
#define se second
typedef long long ll;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
mt19937 rnf(2106);
const int N = 100005;
int n, m;
int s[N], f[N];
vector<pair<int, pair<int, int> > > v[N];
vector<int> vv[N];
int dp[N];
pair<pair<int, int>, pair<int, int> > p[N];
bool stg(int k)
{
for (int i = 0; i <= n + 1; ++i)
{
dp[i] = 0;
v[i].clear();
vv[i].clear();
}
for (int i = 1; i < n; ++i)
{
if (s[i] + k <= s[i + 1] - k)
{
int l = s[i + 1] - k;
int r = s[i] + k;
int ri = i + 1;
while (ri + 1 <= n && s[ri + 1] <= r)
{
++ri;
r = s[ri] + k;
}
int li = i;
while (li - 1 >= 1 && s[li - 1] >= l)
{
--li;
l = s[li] - k;
}
v[li].push_back(m_p(ri, m_p(l, r)));
vv[li].push_back(i);
}
}
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < sz(v[i + 1]); ++j)
{
int l = v[i + 1][j].se.fi;
int r = v[i + 1][j].se.se;
int x = dp[i] + 1;
while (x <= m && l <= f[x] && f[x] <= r)
++x;
if (x - 1 > dp[v[i + 1][j].fi])
{
dp[v[i + 1][j].fi] = x - 1;
p[v[i + 1][j].fi] = m_p(m_p(i, vv[i + 1][j]), m_p(i + 1, v[i + 1][j].fi));
}
}
{
int l = s[i + 1];
int r = s[i + 1] + k;
int x = dp[i] + 1;
while (x <= m && l <= f[x] && f[x] <= r)
++x;
if (x - 1 > dp[i + 1])
{
dp[i + 1] = x - 1;
p[i + 1] = m_p(m_p(i, i), m_p((i + 1), -(i + 1)));
}
}
{
int l = s[i + 1] - k;
int r = s[i + 1];
int x = dp[i] + 1;
while (x <= m && l <= f[x] && f[x] <= r)
++x;
if (x - 1 > dp[i + 1])
{
dp[i + 1] = x - 1;
p[i + 1] = m_p(m_p(i, i), m_p(-(i + 1), (i + 1)));
}
}
}
return (dp[n] == m);
}
void solv()
{
cin >> n >> m;
for (int i = 1; i <= n; ++i)
cin >> s[i];
for (int i = 1; i <= m; ++i)
cin >> f[i];
int l = 0, r = 1000000009;
int ans = -1;
while (l <= r)
{
int m = (l + r) / 2;
if (stg(m))
{
ans = m;
r = m - 1;
}
else
l = m + 1;
}
cout << ans << "\n";
if (ans != -1)
{
stg(ans);
string s;
s.assign(n + 1, '?');
int i = n;
while (i >= 1)
{
auto t = p[i];
if (t.se.fi > 0 && t.se.se > 0)
{
int j = t.fi.se;
s[j] = 'R';
s[j + 1] = 'L';
for (int k = j + 2; k <= t.se.se; ++k)
s[k] = 'R';
for (int k = j - 1; k >= t.se.fi; --k)
s[k] = 'L';
}
else
{
if (t.se.fi < 0)
{
s[t.se.se] = 'L';
}
else
{
s[t.se.fi] = 'R';
}
}
i = t.fi.fi;
}
reverse(all(s));
s.pop_back();
reverse(all(s));
cout << s << "\n";
}
}
int main()
{
#ifdef SOMETHING
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif // SOMETHING
ios_base::sync_with_stdio(false), cin.tie(0);
int tt = 1;
//cin >> tt;
while (tt--)
{
solv();
}
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... |