#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
vector<int> flowers;
vector<int> sprinklers;
int last_under(vector<int>& v, int x){
return (upper_bound(v.begin(), v.end(), x) - v.begin())-1;
}
int first_above(vector<int>& v, int x){
return lower_bound(v.begin(), v.end(), x) - v.begin();
}
pii get_coverage(pii interval){
return {first_above(flowers, interval.first), last_under(flowers, interval.second)};
}
bool is_in(pii inter, int pos){
return (pos>=inter.first && pos<=inter.second);
}
int N, M;
struct DP{
int val= 0;
int anc =0;
string p;
DP(){};
DP(int _val, int _anc, string s){
p = s;
val = _val;
anc= _anc;
}
};
void chmax(DP& a, DP b){
if(b.val>a.val){
a= b;
}
}
vector<DP> solve(int k){
vector<DP> dp(N+1);
for(int cur_sprinkler = 0; cur_sprinkler<N; cur_sprinkler++){
int uncovered_flower = dp[cur_sprinkler].val;
pii before_inter = get_coverage({sprinklers[cur_sprinkler]-k, sprinklers[cur_sprinkler]});
if(is_in(before_inter, uncovered_flower)){
chmax(dp[cur_sprinkler+1], DP(before_inter.second+1, cur_sprinkler, string("L")));
}
before_inter = get_coverage({sprinklers[cur_sprinkler], sprinklers[cur_sprinkler]+k});
if(is_in(before_inter, uncovered_flower)){
chmax(dp[cur_sprinkler+1], DP(before_inter.second+1, cur_sprinkler, string("R")));
}
if(cur_sprinkler<N-1){
if(sprinklers[cur_sprinkler+1]-sprinklers[cur_sprinkler]<k){
pii interval = {sprinklers[cur_sprinkler+1]-k, sprinklers[cur_sprinkler]+k};
if(is_in(get_coverage(interval), uncovered_flower)){
chmax(dp[cur_sprinkler+2], DP(before_inter.second+1, cur_sprinkler, string("LR")));
}
}
}
chmax(dp[cur_sprinkler+1], DP(uncovered_flower, cur_sprinkler, string("R")));
}
return dp;
}
string res;
void backtrack(vector<DP>& dp, int pos){
bool ok = true;
while(ok){
res.append(dp[pos].p.begin(), dp[pos].p.end());
pos= dp[pos].anc;
ok = pos>0;
}
}
signed main(){
cin>>N>>M;
sprinklers.resize(N);
for(int i = 0; i<N; i++){
cin>>sprinklers[i];
}
int k;
flowers.push_back(sprinklers[0]);
for(int i = 0; i<M; i++){
cin>>k;
flowers.push_back(k);
}
flowers.push_back(sprinklers.back());
sort(flowers.begin(), flowers.end());
int K=1e9;
if(solve(K).back().val != M+2){
cout<<-1<<endl;
return 0;
}
for(int step = (1<<30); step>=1; step/=2){
if(K-step>=0 && solve(K-step).back().val == M+2){
K-=step;
}
}
cout<<K<<endl;
auto res_dp = solve(K);
backtrack(res_dp, N);
reverse(res.begin(), res.end());
cout<<res<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Correct |
2 |
Correct |
23 ms |
1444 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Correct |
27 ms |
1488 KB |
Correct |
5 |
Correct |
27 ms |
1492 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
6 ms |
752 KB |
Correct |
9 |
Correct |
0 ms |
344 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
30 ms |
1520 KB |
Correct |
3 |
Correct |
41 ms |
904 KB |
Correct |
4 |
Correct |
492 ms |
6948 KB |
Correct |
5 |
Correct |
500 ms |
7020 KB |
Correct |
6 |
Correct |
1 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
319 ms |
6948 KB |
Correct |
9 |
Correct |
340 ms |
7020 KB |
Correct |
10 |
Correct |
365 ms |
7016 KB |
Correct |
11 |
Correct |
156 ms |
6132 KB |
Correct |
12 |
Correct |
231 ms |
3268 KB |
Correct |
13 |
Correct |
326 ms |
6680 KB |
Correct |
14 |
Correct |
345 ms |
6516 KB |
Correct |
15 |
Correct |
363 ms |
6688 KB |
Correct |
16 |
Correct |
315 ms |
6508 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
1 ms |
344 KB |
Correct |
4 |
Correct |
1 ms |
348 KB |
Correct |
5 |
Correct |
1 ms |
348 KB |
Correct |
6 |
Correct |
1 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
1 ms |
348 KB |
Correct |
9 |
Correct |
0 ms |
348 KB |
Correct |
10 |
Correct |
1 ms |
348 KB |
Correct |
11 |
Correct |
0 ms |
348 KB |
Correct |
12 |
Correct |
1 ms |
348 KB |
Correct |
13 |
Correct |
1 ms |
348 KB |
Correct |
14 |
Correct |
0 ms |
348 KB |
Correct |
15 |
Correct |
1 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
0 ms |
348 KB |
Correct |
18 |
Correct |
0 ms |
596 KB |
Correct |
19 |
Correct |
1 ms |
348 KB |
Correct |
20 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
85 ms |
1608 KB |
Correct |
3 |
Correct |
427 ms |
6768 KB |
Correct |
4 |
Correct |
420 ms |
8808 KB |
Correct |
5 |
Correct |
425 ms |
8784 KB |
Correct |
6 |
Correct |
443 ms |
8812 KB |
Correct |
7 |
Correct |
423 ms |
8880 KB |
Correct |
8 |
Correct |
427 ms |
8908 KB |
Correct |
9 |
Correct |
421 ms |
8884 KB |
Correct |
10 |
Correct |
425 ms |
8816 KB |
Correct |
11 |
Correct |
429 ms |
8816 KB |
Correct |
12 |
Correct |
0 ms |
344 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
96 ms |
7128 KB |
Correct |
15 |
Correct |
164 ms |
6964 KB |
Correct |
16 |
Correct |
155 ms |
7160 KB |
Correct |
17 |
Correct |
229 ms |
7380 KB |
Correct |
18 |
Correct |
258 ms |
7532 KB |
Correct |
19 |
Correct |
283 ms |
7808 KB |
Correct |
20 |
Correct |
403 ms |
8492 KB |
Correct |
21 |
Correct |
413 ms |
8560 KB |
Correct |
22 |
Correct |
417 ms |
8304 KB |
Correct |
23 |
Correct |
449 ms |
8304 KB |
Correct |
24 |
Correct |
441 ms |
8092 KB |
Correct |
25 |
Correct |
433 ms |
8304 KB |
Correct |
26 |
Correct |
185 ms |
6044 KB |
Correct |
27 |
Correct |
156 ms |
3540 KB |
Correct |
28 |
Correct |
398 ms |
7756 KB |
Correct |
29 |
Correct |
358 ms |
7532 KB |
Correct |
30 |
Correct |
388 ms |
7760 KB |
Correct |
31 |
Correct |
376 ms |
7252 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
23 ms |
1444 KB |
Correct |
4 |
Correct |
0 ms |
348 KB |
Correct |
5 |
Correct |
27 ms |
1488 KB |
Correct |
6 |
Correct |
27 ms |
1492 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
6 ms |
752 KB |
Correct |
10 |
Correct |
0 ms |
344 KB |
Correct |
11 |
Correct |
30 ms |
1520 KB |
Correct |
12 |
Correct |
41 ms |
904 KB |
Correct |
13 |
Correct |
492 ms |
6948 KB |
Correct |
14 |
Correct |
500 ms |
7020 KB |
Correct |
15 |
Correct |
1 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
319 ms |
6948 KB |
Correct |
18 |
Correct |
340 ms |
7020 KB |
Correct |
19 |
Correct |
365 ms |
7016 KB |
Correct |
20 |
Correct |
156 ms |
6132 KB |
Correct |
21 |
Correct |
231 ms |
3268 KB |
Correct |
22 |
Correct |
326 ms |
6680 KB |
Correct |
23 |
Correct |
345 ms |
6516 KB |
Correct |
24 |
Correct |
363 ms |
6688 KB |
Correct |
25 |
Correct |
315 ms |
6508 KB |
Correct |
26 |
Correct |
1 ms |
344 KB |
Correct |
27 |
Correct |
1 ms |
348 KB |
Correct |
28 |
Correct |
1 ms |
348 KB |
Correct |
29 |
Correct |
1 ms |
348 KB |
Correct |
30 |
Correct |
0 ms |
348 KB |
Correct |
31 |
Correct |
1 ms |
348 KB |
Correct |
32 |
Correct |
0 ms |
348 KB |
Correct |
33 |
Correct |
1 ms |
348 KB |
Correct |
34 |
Correct |
0 ms |
348 KB |
Correct |
35 |
Correct |
1 ms |
348 KB |
Correct |
36 |
Correct |
1 ms |
348 KB |
Correct |
37 |
Correct |
0 ms |
348 KB |
Correct |
38 |
Correct |
1 ms |
348 KB |
Correct |
39 |
Correct |
0 ms |
348 KB |
Correct |
40 |
Correct |
0 ms |
348 KB |
Correct |
41 |
Correct |
0 ms |
596 KB |
Correct |
42 |
Correct |
1 ms |
348 KB |
Correct |
43 |
Correct |
0 ms |
348 KB |
Correct |
44 |
Correct |
85 ms |
1608 KB |
Correct |
45 |
Correct |
427 ms |
6768 KB |
Correct |
46 |
Correct |
420 ms |
8808 KB |
Correct |
47 |
Correct |
425 ms |
8784 KB |
Correct |
48 |
Correct |
443 ms |
8812 KB |
Correct |
49 |
Correct |
423 ms |
8880 KB |
Correct |
50 |
Correct |
427 ms |
8908 KB |
Correct |
51 |
Correct |
421 ms |
8884 KB |
Correct |
52 |
Correct |
425 ms |
8816 KB |
Correct |
53 |
Correct |
429 ms |
8816 KB |
Correct |
54 |
Correct |
0 ms |
344 KB |
Correct |
55 |
Correct |
0 ms |
348 KB |
Correct |
56 |
Correct |
96 ms |
7128 KB |
Correct |
57 |
Correct |
164 ms |
6964 KB |
Correct |
58 |
Correct |
155 ms |
7160 KB |
Correct |
59 |
Correct |
229 ms |
7380 KB |
Correct |
60 |
Correct |
258 ms |
7532 KB |
Correct |
61 |
Correct |
283 ms |
7808 KB |
Correct |
62 |
Correct |
403 ms |
8492 KB |
Correct |
63 |
Correct |
413 ms |
8560 KB |
Correct |
64 |
Correct |
417 ms |
8304 KB |
Correct |
65 |
Correct |
449 ms |
8304 KB |
Correct |
66 |
Correct |
441 ms |
8092 KB |
Correct |
67 |
Correct |
433 ms |
8304 KB |
Correct |
68 |
Correct |
185 ms |
6044 KB |
Correct |
69 |
Correct |
156 ms |
3540 KB |
Correct |
70 |
Correct |
398 ms |
7756 KB |
Correct |
71 |
Correct |
358 ms |
7532 KB |
Correct |
72 |
Correct |
388 ms |
7760 KB |
Correct |
73 |
Correct |
376 ms |
7252 KB |
Correct |
74 |
Correct |
39 ms |
2260 KB |
Correct |
75 |
Correct |
587 ms |
8888 KB |
Correct |
76 |
Correct |
574 ms |
8808 KB |
Correct |
77 |
Correct |
0 ms |
348 KB |
Correct |
78 |
Correct |
329 ms |
8976 KB |
Correct |
79 |
Correct |
347 ms |
8808 KB |
Correct |
80 |
Correct |
394 ms |
8808 KB |
Correct |
81 |
Correct |
155 ms |
7128 KB |
Correct |
82 |
Correct |
207 ms |
7160 KB |
Correct |
83 |
Correct |
195 ms |
7164 KB |
Correct |
84 |
Correct |
61 ms |
1496 KB |
Correct |
85 |
Correct |
400 ms |
7976 KB |
Correct |
86 |
Correct |
419 ms |
8132 KB |
Correct |
87 |
Correct |
380 ms |
6980 KB |
Correct |
88 |
Correct |
26 ms |
2260 KB |
Correct |
89 |
Correct |
29 ms |
2252 KB |
Correct |