#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
using namespace std;
ll dp[100005];
string prevs[100005];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,m;
cin >> n >> m;
FOR(i,0,n+1) dp[i] = -1;
vector<ll> water(n);
vector<ll> flower(m);
FOR(i,0,n) cin >> water[i];
FOR(i,0,m) cin >> flower[i];
ll lo = 0, hi = 10000000000000000;
while (lo < hi){
FOR(i,0,n+1) dp[i] = -1;
ll k = (lo+hi)/2;
bool flag = false;
FOR(i,0,n){
ll sus = upper_bound(flower.begin(), flower.end(), dp[i]) - flower.begin();
if (sus >= m || flower[sus] >= water[i]){
if (water[i] + k > dp[i+1]){
dp[i+1] = water[i] + k;
prevs[i+1] = "R";
}
}else{
if (abs(flower[sus] - water[i]) > k){
flag = true;
}
if (i==n-1){
if (water[i] > dp[i+1]){
dp[i+1] = water[i];
prevs[i+1] = "L";
}
}
else{
if (abs(flower[sus] - water[i+1]) > k){
if (water[i] > dp[i+1]){
dp[i+1] = water[i];
prevs[i+1] = "L";
}
}else{
if (water[i] > dp[i+1]){
dp[i+1] = water[i];
prevs[i+1] = "L";
}
ll maxi = max(water[i]+k, water[i+1]);
if (maxi > dp[i+2]){
dp[i+2] = maxi;
prevs[i+2] = "LR";
}
}
}
}
}
if (dp[n] < flower[m-1]) flag = 1;
if (flag==0) hi = k;
else lo = k+1;
}
ll k = lo;
FOR(i,0,n+1) dp[i] = -1;
FOR(i,0,n){
ll sus = upper_bound(flower.begin(), flower.end(), dp[i]) - flower.begin();
//cout << dp[i] << " "<< sus << endl;
if (sus >= m || flower[sus] >= water[i]){
if (water[i] + k > dp[i+1]){
dp[i+1] = water[i] + k;
prevs[i+1] = "R";
}
}else{
if (i==n-1){
if (water[i] > dp[i+1]){
dp[i+1] = water[i];
prevs[i+1] = "L";
}
}
else{
if (abs(flower[sus] - water[i+1]) > k){
if (water[i] > dp[i+1]){
dp[i+1] = water[i];
prevs[i+1] = "L";
}
}else{
if (water[i] > dp[i+1]){
dp[i+1] = water[i];
prevs[i+1] = "L";
}
ll maxi = max(water[i]+k, water[i+1]);
if (maxi > dp[i+2]){
dp[i+2] = maxi;
prevs[i+2] = "LR";
}
}
}
}
}
ll cur = n;
string ans = "";
while (cur != 0){
ans += prevs[cur];
cur -= prevs[cur].size();
}
reverse(ans.begin(), ans.end());
if (lo == 10000000000000000) cout << -1;
else{
cout << lo << endl;
cout << ans << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Correct |
2 |
Correct |
1 ms |
3420 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Correct |
2 |
Correct |
6 ms |
4188 KB |
Correct |
3 |
Correct |
1 ms |
3420 KB |
Correct |
4 |
Correct |
7 ms |
4188 KB |
Correct |
5 |
Correct |
7 ms |
4184 KB |
Correct |
6 |
Correct |
1 ms |
3416 KB |
Correct |
7 |
Correct |
1 ms |
3416 KB |
Correct |
8 |
Correct |
2 ms |
3672 KB |
Correct |
9 |
Correct |
1 ms |
3420 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Correct |
2 |
Correct |
9 ms |
4412 KB |
Correct |
3 |
Correct |
14 ms |
3932 KB |
Correct |
4 |
Correct |
153 ms |
8132 KB |
Correct |
5 |
Correct |
153 ms |
8120 KB |
Correct |
6 |
Correct |
1 ms |
3420 KB |
Correct |
7 |
Correct |
1 ms |
3420 KB |
Correct |
8 |
Correct |
126 ms |
8136 KB |
Correct |
9 |
Correct |
119 ms |
8020 KB |
Correct |
10 |
Correct |
125 ms |
8016 KB |
Correct |
11 |
Correct |
70 ms |
6372 KB |
Correct |
12 |
Correct |
71 ms |
6224 KB |
Correct |
13 |
Correct |
131 ms |
6992 KB |
Correct |
14 |
Correct |
142 ms |
7252 KB |
Correct |
15 |
Correct |
148 ms |
7352 KB |
Correct |
16 |
Correct |
128 ms |
6740 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Correct |
2 |
Correct |
1 ms |
3420 KB |
Correct |
3 |
Correct |
1 ms |
3420 KB |
Correct |
4 |
Correct |
1 ms |
3420 KB |
Correct |
5 |
Correct |
1 ms |
3420 KB |
Correct |
6 |
Correct |
1 ms |
3420 KB |
Correct |
7 |
Correct |
1 ms |
3420 KB |
Correct |
8 |
Correct |
1 ms |
3420 KB |
Correct |
9 |
Correct |
1 ms |
3416 KB |
Correct |
10 |
Correct |
1 ms |
3420 KB |
Correct |
11 |
Correct |
1 ms |
3424 KB |
Correct |
12 |
Correct |
1 ms |
3420 KB |
Correct |
13 |
Correct |
1 ms |
3420 KB |
Correct |
14 |
Correct |
1 ms |
3420 KB |
Correct |
15 |
Correct |
1 ms |
3424 KB |
Correct |
16 |
Correct |
1 ms |
3420 KB |
Correct |
17 |
Correct |
1 ms |
3420 KB |
Correct |
18 |
Correct |
1 ms |
3420 KB |
Correct |
19 |
Correct |
1 ms |
3420 KB |
Correct |
20 |
Correct |
1 ms |
3420 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Correct |
2 |
Correct |
30 ms |
4544 KB |
Correct |
3 |
Correct |
182 ms |
8020 KB |
Correct |
4 |
Correct |
177 ms |
8020 KB |
Correct |
5 |
Correct |
175 ms |
8020 KB |
Correct |
6 |
Correct |
178 ms |
8020 KB |
Correct |
7 |
Correct |
181 ms |
8020 KB |
Correct |
8 |
Correct |
177 ms |
8016 KB |
Correct |
9 |
Correct |
181 ms |
8136 KB |
Correct |
10 |
Correct |
175 ms |
8020 KB |
Correct |
11 |
Correct |
176 ms |
8128 KB |
Correct |
12 |
Correct |
1 ms |
3420 KB |
Correct |
13 |
Correct |
1 ms |
3420 KB |
Correct |
14 |
Correct |
72 ms |
6372 KB |
Correct |
15 |
Correct |
81 ms |
6392 KB |
Correct |
16 |
Correct |
81 ms |
6228 KB |
Correct |
17 |
Correct |
115 ms |
6736 KB |
Correct |
18 |
Correct |
113 ms |
6968 KB |
Correct |
19 |
Correct |
121 ms |
7156 KB |
Correct |
20 |
Correct |
161 ms |
7764 KB |
Correct |
21 |
Correct |
158 ms |
7764 KB |
Correct |
22 |
Correct |
148 ms |
7508 KB |
Correct |
23 |
Correct |
150 ms |
7552 KB |
Correct |
24 |
Correct |
142 ms |
7252 KB |
Correct |
25 |
Correct |
143 ms |
7248 KB |
Correct |
26 |
Correct |
89 ms |
6044 KB |
Correct |
27 |
Correct |
64 ms |
5100 KB |
Correct |
28 |
Correct |
141 ms |
7000 KB |
Correct |
29 |
Correct |
135 ms |
6740 KB |
Correct |
30 |
Correct |
140 ms |
6960 KB |
Correct |
31 |
Correct |
136 ms |
7036 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3420 KB |
Correct |
2 |
Correct |
1 ms |
3420 KB |
Correct |
3 |
Correct |
6 ms |
4188 KB |
Correct |
4 |
Correct |
1 ms |
3420 KB |
Correct |
5 |
Correct |
7 ms |
4188 KB |
Correct |
6 |
Correct |
7 ms |
4184 KB |
Correct |
7 |
Correct |
1 ms |
3416 KB |
Correct |
8 |
Correct |
1 ms |
3416 KB |
Correct |
9 |
Correct |
2 ms |
3672 KB |
Correct |
10 |
Correct |
1 ms |
3420 KB |
Correct |
11 |
Correct |
9 ms |
4412 KB |
Correct |
12 |
Correct |
14 ms |
3932 KB |
Correct |
13 |
Correct |
153 ms |
8132 KB |
Correct |
14 |
Correct |
153 ms |
8120 KB |
Correct |
15 |
Correct |
1 ms |
3420 KB |
Correct |
16 |
Correct |
1 ms |
3420 KB |
Correct |
17 |
Correct |
126 ms |
8136 KB |
Correct |
18 |
Correct |
119 ms |
8020 KB |
Correct |
19 |
Correct |
125 ms |
8016 KB |
Correct |
20 |
Correct |
70 ms |
6372 KB |
Correct |
21 |
Correct |
71 ms |
6224 KB |
Correct |
22 |
Correct |
131 ms |
6992 KB |
Correct |
23 |
Correct |
142 ms |
7252 KB |
Correct |
24 |
Correct |
148 ms |
7352 KB |
Correct |
25 |
Correct |
128 ms |
6740 KB |
Correct |
26 |
Correct |
1 ms |
3420 KB |
Correct |
27 |
Correct |
1 ms |
3420 KB |
Correct |
28 |
Correct |
1 ms |
3420 KB |
Correct |
29 |
Correct |
1 ms |
3420 KB |
Correct |
30 |
Correct |
1 ms |
3420 KB |
Correct |
31 |
Correct |
1 ms |
3420 KB |
Correct |
32 |
Correct |
1 ms |
3416 KB |
Correct |
33 |
Correct |
1 ms |
3420 KB |
Correct |
34 |
Correct |
1 ms |
3424 KB |
Correct |
35 |
Correct |
1 ms |
3420 KB |
Correct |
36 |
Correct |
1 ms |
3420 KB |
Correct |
37 |
Correct |
1 ms |
3420 KB |
Correct |
38 |
Correct |
1 ms |
3424 KB |
Correct |
39 |
Correct |
1 ms |
3420 KB |
Correct |
40 |
Correct |
1 ms |
3420 KB |
Correct |
41 |
Correct |
1 ms |
3420 KB |
Correct |
42 |
Correct |
1 ms |
3420 KB |
Correct |
43 |
Correct |
1 ms |
3420 KB |
Correct |
44 |
Correct |
30 ms |
4544 KB |
Correct |
45 |
Correct |
182 ms |
8020 KB |
Correct |
46 |
Correct |
177 ms |
8020 KB |
Correct |
47 |
Correct |
175 ms |
8020 KB |
Correct |
48 |
Correct |
178 ms |
8020 KB |
Correct |
49 |
Correct |
181 ms |
8020 KB |
Correct |
50 |
Correct |
177 ms |
8016 KB |
Correct |
51 |
Correct |
181 ms |
8136 KB |
Correct |
52 |
Correct |
175 ms |
8020 KB |
Correct |
53 |
Correct |
176 ms |
8128 KB |
Correct |
54 |
Correct |
1 ms |
3420 KB |
Correct |
55 |
Correct |
1 ms |
3420 KB |
Correct |
56 |
Correct |
72 ms |
6372 KB |
Correct |
57 |
Correct |
81 ms |
6392 KB |
Correct |
58 |
Correct |
81 ms |
6228 KB |
Correct |
59 |
Correct |
115 ms |
6736 KB |
Correct |
60 |
Correct |
113 ms |
6968 KB |
Correct |
61 |
Correct |
121 ms |
7156 KB |
Correct |
62 |
Correct |
161 ms |
7764 KB |
Correct |
63 |
Correct |
158 ms |
7764 KB |
Correct |
64 |
Correct |
148 ms |
7508 KB |
Correct |
65 |
Correct |
150 ms |
7552 KB |
Correct |
66 |
Correct |
142 ms |
7252 KB |
Correct |
67 |
Correct |
143 ms |
7248 KB |
Correct |
68 |
Correct |
89 ms |
6044 KB |
Correct |
69 |
Correct |
64 ms |
5100 KB |
Correct |
70 |
Correct |
141 ms |
7000 KB |
Correct |
71 |
Correct |
135 ms |
6740 KB |
Correct |
72 |
Correct |
140 ms |
6960 KB |
Correct |
73 |
Correct |
136 ms |
7036 KB |
Correct |
74 |
Correct |
12 ms |
5416 KB |
Correct |
75 |
Correct |
182 ms |
8124 KB |
Correct |
76 |
Correct |
184 ms |
7936 KB |
Correct |
77 |
Correct |
1 ms |
3420 KB |
Correct |
78 |
Correct |
123 ms |
8144 KB |
Correct |
79 |
Correct |
121 ms |
8020 KB |
Correct |
80 |
Correct |
127 ms |
8024 KB |
Correct |
81 |
Correct |
68 ms |
6384 KB |
Correct |
82 |
Correct |
80 ms |
6224 KB |
Correct |
83 |
Correct |
84 ms |
6388 KB |
Correct |
84 |
Correct |
15 ms |
4700 KB |
Correct |
85 |
Correct |
153 ms |
7252 KB |
Correct |
86 |
Correct |
152 ms |
7348 KB |
Correct |
87 |
Correct |
134 ms |
7104 KB |
Correct |
88 |
Correct |
11 ms |
5212 KB |
Correct |
89 |
Correct |
8 ms |
5208 KB |
Correct |