#include<bits/stdc++.h>
using namespace std;
#define int long long
#define pii pair<int, int>
vector<int> flowers;
vector<int> sprinklers;
int under(vector<int>& v, int x){
int res =0;
for(int step= (1<<20); step>=1; step/=2){
if(res + step < v.size() && v[res+step]<=x){
res+= step;
}
}
return res;
}
int above(vector<int>& v, int x){
int res =v.size()-1;
for(int step= (1<<20); step>=1; step/=2){
if(res - step >=0 && v[res-step]>=x){
res-= step;
}
}
return res;
}
pii get_coverage(pii interval){
return {above(flowers, interval.first), 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;
}
Compilation message
Main.cpp: In function 'long long int under(std::vector<long long int>&, long long int)':
Main.cpp:15:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
15 | if(res + step < v.size() && v[res+step]<=x){
| ~~~~~~~~~~~^~~~~~~~~~
# |
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 |
22 ms |
2256 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Correct |
28 ms |
2268 KB |
Correct |
5 |
Correct |
29 ms |
2256 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
5 ms |
860 KB |
Correct |
9 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
32 ms |
1488 KB |
Correct |
3 |
Correct |
54 ms |
900 KB |
Correct |
4 |
Correct |
543 ms |
7024 KB |
Correct |
5 |
Correct |
494 ms |
6768 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
533 ms |
6892 KB |
Correct |
9 |
Correct |
567 ms |
6868 KB |
Correct |
10 |
Correct |
564 ms |
7024 KB |
Correct |
11 |
Correct |
312 ms |
6096 KB |
Correct |
12 |
Correct |
260 ms |
3276 KB |
Correct |
13 |
Correct |
389 ms |
6516 KB |
Correct |
14 |
Correct |
396 ms |
6764 KB |
Correct |
15 |
Correct |
417 ms |
6696 KB |
Correct |
16 |
Correct |
421 ms |
6684 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Correct |
0 ms |
432 KB |
Correct |
5 |
Correct |
1 ms |
348 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
1 ms |
344 KB |
Correct |
10 |
Correct |
1 ms |
348 KB |
Correct |
11 |
Correct |
0 ms |
348 KB |
Correct |
12 |
Correct |
1 ms |
344 KB |
Correct |
13 |
Correct |
1 ms |
348 KB |
Correct |
14 |
Correct |
0 ms |
348 KB |
Correct |
15 |
Correct |
0 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
1 ms |
348 KB |
Correct |
18 |
Correct |
0 ms |
348 KB |
Correct |
19 |
Correct |
0 ms |
348 KB |
Correct |
20 |
Correct |
1 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
82 ms |
1760 KB |
Correct |
3 |
Correct |
404 ms |
6768 KB |
Correct |
4 |
Correct |
470 ms |
6948 KB |
Correct |
5 |
Correct |
449 ms |
6948 KB |
Correct |
6 |
Correct |
407 ms |
6772 KB |
Correct |
7 |
Correct |
411 ms |
6776 KB |
Correct |
8 |
Correct |
454 ms |
6880 KB |
Correct |
9 |
Correct |
413 ms |
6940 KB |
Correct |
10 |
Correct |
408 ms |
6772 KB |
Correct |
11 |
Correct |
404 ms |
6772 KB |
Correct |
12 |
Correct |
0 ms |
348 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
165 ms |
6160 KB |
Correct |
15 |
Correct |
267 ms |
6144 KB |
Correct |
16 |
Correct |
260 ms |
6180 KB |
Correct |
17 |
Correct |
416 ms |
6948 KB |
Correct |
18 |
Correct |
400 ms |
6772 KB |
Correct |
19 |
Correct |
394 ms |
6768 KB |
Correct |
20 |
Correct |
400 ms |
6776 KB |
Correct |
21 |
Correct |
431 ms |
6940 KB |
Correct |
22 |
Correct |
431 ms |
6948 KB |
Correct |
23 |
Correct |
417 ms |
6772 KB |
Correct |
24 |
Correct |
434 ms |
6948 KB |
Correct |
25 |
Correct |
428 ms |
6772 KB |
Correct |
26 |
Correct |
264 ms |
5024 KB |
Correct |
27 |
Correct |
139 ms |
2788 KB |
Correct |
28 |
Correct |
404 ms |
6512 KB |
Correct |
29 |
Correct |
414 ms |
6516 KB |
Correct |
30 |
Correct |
407 ms |
6696 KB |
Correct |
31 |
Correct |
348 ms |
5656 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
0 ms |
348 KB |
Correct |
3 |
Correct |
22 ms |
2256 KB |
Correct |
4 |
Correct |
0 ms |
348 KB |
Correct |
5 |
Correct |
28 ms |
2268 KB |
Correct |
6 |
Correct |
29 ms |
2256 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
5 ms |
860 KB |
Correct |
10 |
Correct |
0 ms |
348 KB |
Correct |
11 |
Correct |
32 ms |
1488 KB |
Correct |
12 |
Correct |
54 ms |
900 KB |
Correct |
13 |
Correct |
543 ms |
7024 KB |
Correct |
14 |
Correct |
494 ms |
6768 KB |
Correct |
15 |
Correct |
0 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
533 ms |
6892 KB |
Correct |
18 |
Correct |
567 ms |
6868 KB |
Correct |
19 |
Correct |
564 ms |
7024 KB |
Correct |
20 |
Correct |
312 ms |
6096 KB |
Correct |
21 |
Correct |
260 ms |
3276 KB |
Correct |
22 |
Correct |
389 ms |
6516 KB |
Correct |
23 |
Correct |
396 ms |
6764 KB |
Correct |
24 |
Correct |
417 ms |
6696 KB |
Correct |
25 |
Correct |
421 ms |
6684 KB |
Correct |
26 |
Correct |
0 ms |
348 KB |
Correct |
27 |
Correct |
0 ms |
432 KB |
Correct |
28 |
Correct |
1 ms |
348 KB |
Correct |
29 |
Correct |
0 ms |
348 KB |
Correct |
30 |
Correct |
0 ms |
348 KB |
Correct |
31 |
Correct |
0 ms |
348 KB |
Correct |
32 |
Correct |
1 ms |
344 KB |
Correct |
33 |
Correct |
1 ms |
348 KB |
Correct |
34 |
Correct |
0 ms |
348 KB |
Correct |
35 |
Correct |
1 ms |
344 KB |
Correct |
36 |
Correct |
1 ms |
348 KB |
Correct |
37 |
Correct |
0 ms |
348 KB |
Correct |
38 |
Correct |
0 ms |
348 KB |
Correct |
39 |
Correct |
0 ms |
348 KB |
Correct |
40 |
Correct |
1 ms |
348 KB |
Correct |
41 |
Correct |
0 ms |
348 KB |
Correct |
42 |
Correct |
0 ms |
348 KB |
Correct |
43 |
Correct |
1 ms |
348 KB |
Correct |
44 |
Correct |
82 ms |
1760 KB |
Correct |
45 |
Correct |
404 ms |
6768 KB |
Correct |
46 |
Correct |
470 ms |
6948 KB |
Correct |
47 |
Correct |
449 ms |
6948 KB |
Correct |
48 |
Correct |
407 ms |
6772 KB |
Correct |
49 |
Correct |
411 ms |
6776 KB |
Correct |
50 |
Correct |
454 ms |
6880 KB |
Correct |
51 |
Correct |
413 ms |
6940 KB |
Correct |
52 |
Correct |
408 ms |
6772 KB |
Correct |
53 |
Correct |
404 ms |
6772 KB |
Correct |
54 |
Correct |
0 ms |
348 KB |
Correct |
55 |
Correct |
0 ms |
348 KB |
Correct |
56 |
Correct |
165 ms |
6160 KB |
Correct |
57 |
Correct |
267 ms |
6144 KB |
Correct |
58 |
Correct |
260 ms |
6180 KB |
Correct |
59 |
Correct |
416 ms |
6948 KB |
Correct |
60 |
Correct |
400 ms |
6772 KB |
Correct |
61 |
Correct |
394 ms |
6768 KB |
Correct |
62 |
Correct |
400 ms |
6776 KB |
Correct |
63 |
Correct |
431 ms |
6940 KB |
Correct |
64 |
Correct |
431 ms |
6948 KB |
Correct |
65 |
Correct |
417 ms |
6772 KB |
Correct |
66 |
Correct |
434 ms |
6948 KB |
Correct |
67 |
Correct |
428 ms |
6772 KB |
Correct |
68 |
Correct |
264 ms |
5024 KB |
Correct |
69 |
Correct |
139 ms |
2788 KB |
Correct |
70 |
Correct |
404 ms |
6512 KB |
Correct |
71 |
Correct |
414 ms |
6516 KB |
Correct |
72 |
Correct |
407 ms |
6696 KB |
Correct |
73 |
Correct |
348 ms |
5656 KB |
Correct |
74 |
Correct |
34 ms |
2260 KB |
Correct |
75 |
Correct |
563 ms |
8896 KB |
Correct |
76 |
Correct |
596 ms |
8820 KB |
Correct |
77 |
Correct |
0 ms |
604 KB |
Correct |
78 |
Correct |
539 ms |
8788 KB |
Correct |
79 |
Correct |
545 ms |
8820 KB |
Correct |
80 |
Correct |
564 ms |
8816 KB |
Correct |
81 |
Correct |
298 ms |
7132 KB |
Correct |
82 |
Correct |
339 ms |
7160 KB |
Correct |
83 |
Correct |
331 ms |
7132 KB |
Correct |
84 |
Correct |
40 ms |
1492 KB |
Correct |
85 |
Correct |
445 ms |
7932 KB |
Correct |
86 |
Correct |
428 ms |
8048 KB |
Correct |
87 |
Correct |
373 ms |
7064 KB |
Correct |
88 |
Correct |
27 ms |
2260 KB |
Correct |
89 |
Correct |
29 ms |
2256 KB |
Correct |