#include <bits/stdc++.h>
using namespace std;
bool solve(vector<int> &S, vector<int> &F, int K, vector<int> &ans)
{
int N = S.size(), M = F.size();
vector<pair<int, int>> dp(M + 1, {-1, -1});
vector<pair<vector<pair<int, int>>, vector<pair<int, int>>>> changes(M + 1);
dp[0].first = -2;
if (S[0] < F[0])
dp[0].second = -2;
for (int i = 0; i < M; i++)
{
if (dp[i].first != -1)
{
int x = lower_bound(S.begin(), S.end(), F[i]) - S.begin();
if (x != N && S[x] - K <= F[i])
{
int y = lower_bound(F.begin(), F.end(), S[x]) - F.begin();
dp[y].first = i;
changes[y].first = {{x, -1}};
if (x < N - 1 && y < M && S[x + 1] < F[y])
{
dp[y].second = i;
changes[y].second = {{x, -1}};
}
if (x < N - 1 && S[x + 1] - K <= F[i])
{
y = upper_bound(F.begin(), F.end(), S[x] + K) - F.begin();
dp[y].first = i;
changes[y].first = {{x, 1}, {x + 1, -1}};
if (x < N - 2 && y < M && S[x + 2] < F[y])
{
dp[y].second = i;
changes[y].second = {{x, 1}, {x + 1, -1}};
}
}
}
}
if (dp[i].second != -1)
{
int x = lower_bound(S.begin(), S.end(), F[i]) - S.begin() - 1;
if (x == -1)
continue;
if (S[x] + K < F[i])
continue;
int y = upper_bound(F.begin(), F.end(), S[x] + K) - F.begin();
dp[y].first = i + M;
changes[y].first = {{x, 1}};
if (x < N - 1 && y < M && S[x + 1] < F[y])
{
dp[y].second = i + M;
changes[y].second = {{x, 1}};
}
}
}
if (dp[M].first != -1 || dp[M].second != -1)
{
return 1;
int x = 0;
if (dp[M].first != -1)
x = M;
else
x = M + M;
while (x != 0)
{
if (x > M)
{
for (int i = 0; i < changes[x - M].second.size(); i++)
ans[changes[x - M].second[i].first] = changes[x - M].second[i].second;
x = dp[x - M].second;
}
else
{
for (int i = 0; i < changes[x].first.size(); i++)
ans[changes[x].first[i].first] = changes[x].first[i].second;
x = dp[x].first;
}
}
return 1;
}
else
return 0;
}
int main()
{
int N, M;
cin >> N >> M;
vector<int> S(N), F(M), F2;
for (int i = 0; i < N; i++)
{
cin >> S[i];
}
for (int i = 0; i < M; i++)
{
cin >> F[i];
}
int buff = 0;
for (int i = 0; i < N; i++)
{
while (buff < M && F[buff] <= S[i])
{
if (F[buff] != S[i])
F2.push_back(F[buff]);
buff++;
}
}
while (buff < M)
F2.push_back(F[buff++]);
swap(F, F2);
if (N == 1 && F[0] < S[0] && F[M - 1] > S[0])
{
cout << -1;
return 0;
}
int L = 0, R = 1000000000;
vector<int> ans(N + 1);
while (L != R)
{
int m = (L + R) / 2;
if (solve(S, F, m, ans))
R = m;
else
L = m + 1;
}
bool b = solve(S, F, L, ans);
cout << L << endl;
for (int i = 0; i < N; i++)
cout << ((ans[i] == 1) ? 'R' : 'L');
}
Compilation message
Main.cpp: In function 'bool solve(std::vector<int>&, std::vector<int>&, int, std::vector<int>&)':
Main.cpp:69:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < changes[x - M].second.size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:75:35: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int i = 0; i < changes[x].first.size(); i++)
| ~~^~~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp: In function 'int main()':
Main.cpp:126:10: warning: unused variable 'b' [-Wunused-variable]
126 | bool b = solve(S, F, L, ans);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
User solution is incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
35 ms |
5672 KB |
Correct |
3 |
Incorrect |
0 ms |
344 KB |
User solution is incorrect |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
User solution is incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
User solution is incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
User solution is incorrect |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
User solution is incorrect |
2 |
Halted |
0 ms |
0 KB |
- |