#include <bits/stdc++.h>
#define pb push_back
#define int long long
using namespace std;
const int mod = 1e9 + 7;
const int inf = 1e15;
const int N = 1e5+5;
int32_t main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
int n, m;
cin>>n>>m;
vector<int> s(n), f(m);
for(int i = 0; i < n; i++){
cin>>s[i];
}
for(int i = 0; i < m; i++){
cin>>f[i];
}
vector<int> right(n), us(n);
int l = 0, r = mod;
while(l < r){
int mid = (l + r)/2;
int ind = 0;
for(int i = 0; i < n; i++){
while(ind < m && s[i] + mid >= f[ind]) ind++;
right[i] = ind; // sağı sulayarak alamayacağımız ilk çiçek
}
ind = 0;
for(int i = 0; i < n; i++){
while(ind < m && s[i] >= f[ind]) ind++;
us[i] = ind; // benden büyük ilk çiçek
}
vector<int> dp(n+1); // dp[i] => ilk i tane sprinkle ile maximum alınabilecek flower prefixi
for(int i = 0; i < n; i++){
dp[i+1] = max(dp[i+1], dp[i]);
if(dp[i] == m) continue;
int j = dp[i];
if(s[i] - mid <= f[j]){ // sola
dp[i+1] = max(dp[i+1], us[i]);
}
if(f[j] >= s[i]){
dp[i+1] = max(dp[i+1], right[i]);
}
if(i < n-1 && f[j] <= s[i+1] && s[i+1] - mid <= f[j]){
dp[i+2] = max({dp[i+2], right[i]});
}
}
if(dp[n] == m) r = mid;
else l = mid + 1;
}
int ans = l;
if(ans == mod){
cout<<-1<<endl;
return 0;
}
cout<<ans<<endl;
int ind = 0;
for(int i = 0; i < n; i++){
while(ind < m && s[i] + ans >= f[ind]) ind++;
right[i] = ind; // sağı sulayarak alabileceğim son çiçek + 1
}
ind = 0;
for(int i = 0; i < n; i++){
while(ind < m && s[i] >= f[ind]) ind++;
us[i] = ind; // benden büyük ilk çiçek
}
vector<int> dp(n+1); // dp[i] => ilk i tane sprinkle ile maximum alınabilecek flower prefixi
vector<int> par(n+1), ptype(n+1);
par[0] = -1;
for(int i = 0; i < n; i++){
if(dp[i+1] < dp[i]){
dp[i+1] = dp[i];
par[i+1] = i;
ptype[i+1] = 0;
}
if(dp[i] == m) continue;
int j = dp[i];
if(s[i] - ans <= f[j]){ // sola
if(dp[i+1] < us[i]){
dp[i+1] = us[i];
par[i+1] = i;
ptype[i+1] = 0;
}
}
if(f[j] >= s[i]){
if(dp[i+1] < right[i]){
dp[i+1] = right[i];
par[i+1] = i;
ptype[i+1] = 1;
}
}
if(i < n-1 && f[j] <= s[i+1] && s[i+1] - ans <= f[j]){
if(dp[i+2] < right[i]){
dp[i+2] = right[i];
par[i+2] = i;
ptype[i+2] = 2;
}
}
}
vector<int> d(n+1);
for(int i = n; i > 0; i = par[i]){
if(par[i] == i-2){
d[i-1] = 0;
d[i-2] = 1;
}
else if(ptype[i] == 1){
d[i-1] = 1;
}
else if(ptype[i] == 0){
d[i-1] = 0;
}
}
for(int i = 0; i < n; i++){
if(d[i]) cout<<"R";
else cout<<"L";
}
cout<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Correct |
2 |
Correct |
1 ms |
336 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Correct |
2 |
Correct |
9 ms |
1104 KB |
Correct |
3 |
Correct |
1 ms |
336 KB |
Correct |
4 |
Correct |
10 ms |
1104 KB |
Correct |
5 |
Correct |
9 ms |
1272 KB |
Correct |
6 |
Correct |
1 ms |
336 KB |
Correct |
7 |
Correct |
1 ms |
336 KB |
Correct |
8 |
Correct |
2 ms |
592 KB |
Correct |
9 |
Correct |
1 ms |
336 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Correct |
2 |
Correct |
10 ms |
1272 KB |
Correct |
3 |
Correct |
5 ms |
1016 KB |
Correct |
4 |
Correct |
55 ms |
6712 KB |
Correct |
5 |
Correct |
58 ms |
6616 KB |
Correct |
6 |
Correct |
1 ms |
336 KB |
Correct |
7 |
Correct |
1 ms |
336 KB |
Correct |
8 |
Correct |
36 ms |
6720 KB |
Correct |
9 |
Correct |
41 ms |
6712 KB |
Correct |
10 |
Correct |
49 ms |
6712 KB |
Correct |
11 |
Correct |
31 ms |
5968 KB |
Correct |
12 |
Correct |
26 ms |
3112 KB |
Correct |
13 |
Correct |
47 ms |
6424 KB |
Correct |
14 |
Correct |
47 ms |
6624 KB |
Correct |
15 |
Correct |
48 ms |
6460 KB |
Correct |
16 |
Correct |
42 ms |
6456 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Correct |
2 |
Correct |
1 ms |
336 KB |
Correct |
3 |
Correct |
1 ms |
336 KB |
Correct |
4 |
Correct |
1 ms |
336 KB |
Correct |
5 |
Correct |
1 ms |
336 KB |
Correct |
6 |
Correct |
1 ms |
336 KB |
Correct |
7 |
Correct |
1 ms |
460 KB |
Correct |
8 |
Correct |
1 ms |
508 KB |
Correct |
9 |
Correct |
1 ms |
336 KB |
Correct |
10 |
Correct |
1 ms |
336 KB |
Correct |
11 |
Correct |
1 ms |
336 KB |
Correct |
12 |
Correct |
1 ms |
336 KB |
Correct |
13 |
Correct |
1 ms |
336 KB |
Correct |
14 |
Correct |
1 ms |
336 KB |
Correct |
15 |
Correct |
1 ms |
336 KB |
Correct |
16 |
Correct |
1 ms |
336 KB |
Correct |
17 |
Correct |
1 ms |
336 KB |
Correct |
18 |
Correct |
1 ms |
336 KB |
Correct |
19 |
Correct |
1 ms |
336 KB |
Correct |
20 |
Correct |
1 ms |
336 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Correct |
2 |
Correct |
16 ms |
1616 KB |
Correct |
3 |
Correct |
84 ms |
6676 KB |
Correct |
4 |
Correct |
87 ms |
8760 KB |
Correct |
5 |
Correct |
85 ms |
8760 KB |
Correct |
6 |
Correct |
86 ms |
8768 KB |
Correct |
7 |
Correct |
88 ms |
8756 KB |
Correct |
8 |
Correct |
87 ms |
8616 KB |
Correct |
9 |
Correct |
92 ms |
8776 KB |
Correct |
10 |
Correct |
98 ms |
8760 KB |
Correct |
11 |
Correct |
94 ms |
8504 KB |
Correct |
12 |
Correct |
1 ms |
336 KB |
Correct |
13 |
Correct |
1 ms |
336 KB |
Correct |
14 |
Correct |
31 ms |
6976 KB |
Correct |
15 |
Correct |
30 ms |
6976 KB |
Correct |
16 |
Correct |
33 ms |
7144 KB |
Correct |
17 |
Correct |
28 ms |
7232 KB |
Correct |
18 |
Correct |
32 ms |
7488 KB |
Correct |
19 |
Correct |
40 ms |
7632 KB |
Correct |
20 |
Correct |
78 ms |
8252 KB |
Correct |
21 |
Correct |
78 ms |
8252 KB |
Correct |
22 |
Correct |
75 ms |
8032 KB |
Correct |
23 |
Correct |
75 ms |
7992 KB |
Correct |
24 |
Correct |
68 ms |
7832 KB |
Correct |
25 |
Correct |
73 ms |
7840 KB |
Correct |
26 |
Correct |
34 ms |
5708 KB |
Correct |
27 |
Correct |
34 ms |
3364 KB |
Correct |
28 |
Correct |
62 ms |
7476 KB |
Correct |
29 |
Correct |
57 ms |
7480 KB |
Correct |
30 |
Correct |
64 ms |
7480 KB |
Correct |
31 |
Correct |
42 ms |
6868 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Correct |
2 |
Correct |
1 ms |
336 KB |
Correct |
3 |
Correct |
9 ms |
1104 KB |
Correct |
4 |
Correct |
1 ms |
336 KB |
Correct |
5 |
Correct |
10 ms |
1104 KB |
Correct |
6 |
Correct |
9 ms |
1272 KB |
Correct |
7 |
Correct |
1 ms |
336 KB |
Correct |
8 |
Correct |
1 ms |
336 KB |
Correct |
9 |
Correct |
2 ms |
592 KB |
Correct |
10 |
Correct |
1 ms |
336 KB |
Correct |
11 |
Correct |
10 ms |
1272 KB |
Correct |
12 |
Correct |
5 ms |
1016 KB |
Correct |
13 |
Correct |
55 ms |
6712 KB |
Correct |
14 |
Correct |
58 ms |
6616 KB |
Correct |
15 |
Correct |
1 ms |
336 KB |
Correct |
16 |
Correct |
1 ms |
336 KB |
Correct |
17 |
Correct |
36 ms |
6720 KB |
Correct |
18 |
Correct |
41 ms |
6712 KB |
Correct |
19 |
Correct |
49 ms |
6712 KB |
Correct |
20 |
Correct |
31 ms |
5968 KB |
Correct |
21 |
Correct |
26 ms |
3112 KB |
Correct |
22 |
Correct |
47 ms |
6424 KB |
Correct |
23 |
Correct |
47 ms |
6624 KB |
Correct |
24 |
Correct |
48 ms |
6460 KB |
Correct |
25 |
Correct |
42 ms |
6456 KB |
Correct |
26 |
Correct |
1 ms |
336 KB |
Correct |
27 |
Correct |
1 ms |
336 KB |
Correct |
28 |
Correct |
1 ms |
336 KB |
Correct |
29 |
Correct |
1 ms |
336 KB |
Correct |
30 |
Correct |
1 ms |
460 KB |
Correct |
31 |
Correct |
1 ms |
508 KB |
Correct |
32 |
Correct |
1 ms |
336 KB |
Correct |
33 |
Correct |
1 ms |
336 KB |
Correct |
34 |
Correct |
1 ms |
336 KB |
Correct |
35 |
Correct |
1 ms |
336 KB |
Correct |
36 |
Correct |
1 ms |
336 KB |
Correct |
37 |
Correct |
1 ms |
336 KB |
Correct |
38 |
Correct |
1 ms |
336 KB |
Correct |
39 |
Correct |
1 ms |
336 KB |
Correct |
40 |
Correct |
1 ms |
336 KB |
Correct |
41 |
Correct |
1 ms |
336 KB |
Correct |
42 |
Correct |
1 ms |
336 KB |
Correct |
43 |
Correct |
1 ms |
336 KB |
Correct |
44 |
Correct |
16 ms |
1616 KB |
Correct |
45 |
Correct |
84 ms |
6676 KB |
Correct |
46 |
Correct |
87 ms |
8760 KB |
Correct |
47 |
Correct |
85 ms |
8760 KB |
Correct |
48 |
Correct |
86 ms |
8768 KB |
Correct |
49 |
Correct |
88 ms |
8756 KB |
Correct |
50 |
Correct |
87 ms |
8616 KB |
Correct |
51 |
Correct |
92 ms |
8776 KB |
Correct |
52 |
Correct |
98 ms |
8760 KB |
Correct |
53 |
Correct |
94 ms |
8504 KB |
Correct |
54 |
Correct |
1 ms |
336 KB |
Correct |
55 |
Correct |
1 ms |
336 KB |
Correct |
56 |
Correct |
31 ms |
6976 KB |
Correct |
57 |
Correct |
30 ms |
6976 KB |
Correct |
58 |
Correct |
33 ms |
7144 KB |
Correct |
59 |
Correct |
28 ms |
7232 KB |
Correct |
60 |
Correct |
32 ms |
7488 KB |
Correct |
61 |
Correct |
40 ms |
7632 KB |
Correct |
62 |
Correct |
78 ms |
8252 KB |
Correct |
63 |
Correct |
78 ms |
8252 KB |
Correct |
64 |
Correct |
75 ms |
8032 KB |
Correct |
65 |
Correct |
75 ms |
7992 KB |
Correct |
66 |
Correct |
68 ms |
7832 KB |
Correct |
67 |
Correct |
73 ms |
7840 KB |
Correct |
68 |
Correct |
34 ms |
5708 KB |
Correct |
69 |
Correct |
34 ms |
3364 KB |
Correct |
70 |
Correct |
62 ms |
7476 KB |
Correct |
71 |
Correct |
57 ms |
7480 KB |
Correct |
72 |
Correct |
64 ms |
7480 KB |
Correct |
73 |
Correct |
42 ms |
6868 KB |
Correct |
74 |
Correct |
12 ms |
2128 KB |
Correct |
75 |
Correct |
88 ms |
8608 KB |
Correct |
76 |
Correct |
88 ms |
8756 KB |
Correct |
77 |
Correct |
1 ms |
336 KB |
Correct |
78 |
Correct |
35 ms |
8580 KB |
Correct |
79 |
Correct |
44 ms |
8676 KB |
Correct |
80 |
Correct |
56 ms |
8648 KB |
Correct |
81 |
Correct |
41 ms |
6868 KB |
Correct |
82 |
Correct |
32 ms |
6976 KB |
Correct |
83 |
Correct |
33 ms |
6968 KB |
Correct |
84 |
Correct |
10 ms |
1616 KB |
Correct |
85 |
Correct |
63 ms |
7736 KB |
Correct |
86 |
Correct |
66 ms |
7736 KB |
Correct |
87 |
Correct |
47 ms |
7108 KB |
Correct |
88 |
Correct |
12 ms |
2128 KB |
Correct |
89 |
Correct |
10 ms |
2128 KB |
Correct |