#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ff first
#define ss second
ll ttt;
const ll INF=1e18;
const ll MOD=1e9+7;
const ll N=100007;
ll n,m,k;
int f[N],s[N];
int dp[N];
int par[N];
bool pardir[N];
int getFlowerAmount(int l, int r){
auto it1=lower_bound(f+1,f+m+1,l);
auto it2=upper_bound(f+1,f+m+1,r);
int cnt=(it2-it1);
//cout<<"GetFlowerAmount: "<<l<<" "<<r<<" - "<<cnt<<endl;
return cnt;
}
bool check(int vl){
//cout<<vl<<endl;
for(int i=0;i<=n;i++){
dp[i]=0;
par[i]=0;
}
for(int i=0;i<n;i++){
if(dp[i+1]<dp[i]){
dp[i+1]=dp[i];
par[i+1]=i;
pardir[i+1]=true;
}
int ind=dp[i]+1;
if(ind==m+1){
continue;
}
if(f[ind]>=s[i+1]&&f[ind]<=s[i+1]+vl){
//cout<<1<<endl;
int pos=dp[i]+getFlowerAmount(f[ind],s[i+1]+vl);
if(pos>dp[i+1]){
par[i+1]=i;
pardir[i+1]=true;
}
dp[i+1]=max(dp[i+1],pos);
//cout<<i+1<<" -> "<<dp[i+1]<<endl;
}
if(f[ind]>=s[i+1]-vl&&f[ind]<=s[i+1]){
//cout<<2<<endl;
int pos=dp[i]+getFlowerAmount(f[ind],s[i+1]);
if(pos>dp[i+1]){
par[i+1]=i;
pardir[i+1]=false;
}
dp[i+1]=max(dp[i+1],pos);
//cout<<i+1<<" -> "<<dp[i+1]<<endl;
}
if(i<n-1&&s[i+2]-vl<=s[i+1]&&s[i+2]<=s[i+1]+vl&&f[ind]>=s[i+2]-vl&&f[ind]<=s[i+1]+vl){
//cout<<3<<endl;
int pos=dp[i]+getFlowerAmount(f[ind],s[i+1]+vl);
if(pos>dp[i+2]){
par[i+2]=i;
}
dp[i+2]=max(dp[i+2],pos);
//cout<<i+2<<" -> "<<dp[i+2]<<endl;
}
//cout<<i<<": "<<dp[i]<<endl;
}
//cout<<n<<": "<<dp[n]<<endl;
//cout<<endl;
if(dp[n]==m){
return true;
}
return false;
}
string getAnswer(){
string ans="";
for(int i=0;i<n;i++){
ans+='L';
}
int cur=n;
while(cur>0){
if(dp[cur]==0){
break;
}
if(cur-par[cur]==2){
ans[cur-2]='R';
}
else if(pardir[cur]){
ans[cur-1]='R';
}
cur=par[cur];
}
return ans;
}
int binsearch(){
int l=0,r=MOD;
while(l<r){
int mid=(l+r)/2;
if(check(mid)){
r=mid;
}
else{
l=mid+1;
}
}
return l;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
// freopen("input.txt","r",stdin);
// freopen("wrongout.txt","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>s[i];
}
for(int i=1;i<=m;i++){
cin>>f[i];
}
int res=binsearch();
if(res==MOD){
cout<<-1<<endl;
return 0;
}
check(res);
string ans=getAnswer();
cout<<res<<endl<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
1 ms |
344 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Correct |
2 |
Correct |
4 ms |
604 KB |
Correct |
3 |
Correct |
0 ms |
348 KB |
Correct |
4 |
Correct |
8 ms |
632 KB |
Correct |
5 |
Correct |
6 ms |
860 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
1 ms |
436 KB |
Correct |
9 |
Correct |
0 ms |
348 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
8 ms |
860 KB |
Correct |
3 |
Correct |
7 ms |
604 KB |
Correct |
4 |
Correct |
92 ms |
2368 KB |
Correct |
5 |
Correct |
100 ms |
2356 KB |
Correct |
6 |
Correct |
0 ms |
348 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
16 ms |
2396 KB |
Correct |
9 |
Correct |
17 ms |
2376 KB |
Correct |
10 |
Correct |
18 ms |
2396 KB |
Correct |
11 |
Correct |
13 ms |
1884 KB |
Correct |
12 |
Correct |
56 ms |
1364 KB |
Correct |
13 |
Correct |
64 ms |
2132 KB |
Correct |
14 |
Correct |
73 ms |
2240 KB |
Correct |
15 |
Correct |
71 ms |
2128 KB |
Correct |
16 |
Correct |
53 ms |
2052 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
1 ms |
344 KB |
Correct |
3 |
Correct |
0 ms |
344 KB |
Correct |
4 |
Correct |
0 ms |
348 KB |
Correct |
5 |
Correct |
0 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 |
0 ms |
348 KB |
Correct |
13 |
Correct |
0 ms |
348 KB |
Correct |
14 |
Correct |
0 ms |
348 KB |
Correct |
15 |
Correct |
0 ms |
460 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
0 ms |
348 KB |
Correct |
18 |
Correct |
0 ms |
348 KB |
Correct |
19 |
Correct |
0 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 |
38 ms |
856 KB |
Correct |
3 |
Correct |
158 ms |
2352 KB |
Correct |
4 |
Correct |
166 ms |
3668 KB |
Correct |
5 |
Correct |
163 ms |
3628 KB |
Correct |
6 |
Correct |
157 ms |
3832 KB |
Correct |
7 |
Correct |
165 ms |
3596 KB |
Correct |
8 |
Correct |
165 ms |
3652 KB |
Correct |
9 |
Correct |
166 ms |
3656 KB |
Correct |
10 |
Correct |
166 ms |
3644 KB |
Correct |
11 |
Correct |
166 ms |
3664 KB |
Correct |
12 |
Correct |
0 ms |
348 KB |
Correct |
13 |
Correct |
1 ms |
344 KB |
Correct |
14 |
Correct |
18 ms |
2700 KB |
Correct |
15 |
Correct |
17 ms |
2652 KB |
Correct |
16 |
Correct |
17 ms |
2652 KB |
Correct |
17 |
Correct |
13 ms |
2904 KB |
Correct |
18 |
Correct |
15 ms |
3160 KB |
Correct |
19 |
Correct |
34 ms |
2904 KB |
Correct |
20 |
Correct |
132 ms |
3648 KB |
Correct |
21 |
Correct |
132 ms |
3668 KB |
Correct |
22 |
Correct |
114 ms |
3260 KB |
Correct |
23 |
Correct |
118 ms |
3316 KB |
Correct |
24 |
Correct |
107 ms |
3260 KB |
Correct |
25 |
Correct |
102 ms |
3152 KB |
Correct |
26 |
Correct |
38 ms |
2388 KB |
Correct |
27 |
Correct |
68 ms |
1744 KB |
Correct |
28 |
Correct |
99 ms |
2900 KB |
Correct |
29 |
Correct |
84 ms |
2964 KB |
Correct |
30 |
Correct |
88 ms |
2900 KB |
Correct |
31 |
Correct |
125 ms |
3060 KB |
Correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Correct |
2 |
Correct |
1 ms |
344 KB |
Correct |
3 |
Correct |
4 ms |
604 KB |
Correct |
4 |
Correct |
0 ms |
348 KB |
Correct |
5 |
Correct |
8 ms |
632 KB |
Correct |
6 |
Correct |
6 ms |
860 KB |
Correct |
7 |
Correct |
0 ms |
348 KB |
Correct |
8 |
Correct |
0 ms |
348 KB |
Correct |
9 |
Correct |
1 ms |
436 KB |
Correct |
10 |
Correct |
0 ms |
348 KB |
Correct |
11 |
Correct |
8 ms |
860 KB |
Correct |
12 |
Correct |
7 ms |
604 KB |
Correct |
13 |
Correct |
92 ms |
2368 KB |
Correct |
14 |
Correct |
100 ms |
2356 KB |
Correct |
15 |
Correct |
0 ms |
348 KB |
Correct |
16 |
Correct |
0 ms |
348 KB |
Correct |
17 |
Correct |
16 ms |
2396 KB |
Correct |
18 |
Correct |
17 ms |
2376 KB |
Correct |
19 |
Correct |
18 ms |
2396 KB |
Correct |
20 |
Correct |
13 ms |
1884 KB |
Correct |
21 |
Correct |
56 ms |
1364 KB |
Correct |
22 |
Correct |
64 ms |
2132 KB |
Correct |
23 |
Correct |
73 ms |
2240 KB |
Correct |
24 |
Correct |
71 ms |
2128 KB |
Correct |
25 |
Correct |
53 ms |
2052 KB |
Correct |
26 |
Correct |
0 ms |
344 KB |
Correct |
27 |
Correct |
0 ms |
348 KB |
Correct |
28 |
Correct |
0 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 |
0 ms |
348 KB |
Correct |
36 |
Correct |
0 ms |
348 KB |
Correct |
37 |
Correct |
0 ms |
348 KB |
Correct |
38 |
Correct |
0 ms |
460 KB |
Correct |
39 |
Correct |
0 ms |
348 KB |
Correct |
40 |
Correct |
0 ms |
348 KB |
Correct |
41 |
Correct |
0 ms |
348 KB |
Correct |
42 |
Correct |
0 ms |
348 KB |
Correct |
43 |
Correct |
0 ms |
348 KB |
Correct |
44 |
Correct |
38 ms |
856 KB |
Correct |
45 |
Correct |
158 ms |
2352 KB |
Correct |
46 |
Correct |
166 ms |
3668 KB |
Correct |
47 |
Correct |
163 ms |
3628 KB |
Correct |
48 |
Correct |
157 ms |
3832 KB |
Correct |
49 |
Correct |
165 ms |
3596 KB |
Correct |
50 |
Correct |
165 ms |
3652 KB |
Correct |
51 |
Correct |
166 ms |
3656 KB |
Correct |
52 |
Correct |
166 ms |
3644 KB |
Correct |
53 |
Correct |
166 ms |
3664 KB |
Correct |
54 |
Correct |
0 ms |
348 KB |
Correct |
55 |
Correct |
1 ms |
344 KB |
Correct |
56 |
Correct |
18 ms |
2700 KB |
Correct |
57 |
Correct |
17 ms |
2652 KB |
Correct |
58 |
Correct |
17 ms |
2652 KB |
Correct |
59 |
Correct |
13 ms |
2904 KB |
Correct |
60 |
Correct |
15 ms |
3160 KB |
Correct |
61 |
Correct |
34 ms |
2904 KB |
Correct |
62 |
Correct |
132 ms |
3648 KB |
Correct |
63 |
Correct |
132 ms |
3668 KB |
Correct |
64 |
Correct |
114 ms |
3260 KB |
Correct |
65 |
Correct |
118 ms |
3316 KB |
Correct |
66 |
Correct |
107 ms |
3260 KB |
Correct |
67 |
Correct |
102 ms |
3152 KB |
Correct |
68 |
Correct |
38 ms |
2388 KB |
Correct |
69 |
Correct |
68 ms |
1744 KB |
Correct |
70 |
Correct |
99 ms |
2900 KB |
Correct |
71 |
Correct |
84 ms |
2964 KB |
Correct |
72 |
Correct |
88 ms |
2900 KB |
Correct |
73 |
Correct |
125 ms |
3060 KB |
Correct |
74 |
Correct |
12 ms |
1624 KB |
Correct |
75 |
Correct |
147 ms |
3668 KB |
Correct |
76 |
Correct |
181 ms |
3668 KB |
Correct |
77 |
Correct |
0 ms |
348 KB |
Correct |
78 |
Correct |
19 ms |
3636 KB |
Correct |
79 |
Correct |
20 ms |
3588 KB |
Correct |
80 |
Correct |
19 ms |
3664 KB |
Correct |
81 |
Correct |
14 ms |
2652 KB |
Correct |
82 |
Correct |
16 ms |
2648 KB |
Correct |
83 |
Correct |
16 ms |
2652 KB |
Correct |
84 |
Correct |
20 ms |
1116 KB |
Correct |
85 |
Correct |
123 ms |
3268 KB |
Correct |
86 |
Correct |
133 ms |
3408 KB |
Correct |
87 |
Correct |
129 ms |
2896 KB |
Correct |
88 |
Correct |
6 ms |
1372 KB |
Correct |
89 |
Correct |
6 ms |
1368 KB |
Correct |