#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define ll long long
// #define int ll
#define all(v) v.begin(),v.end()
#define sz(s) ((int)s.size())
#define pb push_back
#define F first
#define S second
#define pii pair<int,int>
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
using namespace std;
const ll inf=1e18;
const int MAX=1e5+10;
const int MX=1e8;
const int mod=1e9+7;
int binpow(int a,int n){
if(!n)return 1;
if(n%2==1)return a*binpow(a,n-1)%mod;
int k=binpow(a,n/2);
return k*k%mod;
}
int n,m;
int s[MAX],f[MAX];
int dp[MAX][3];
int pr[MAX][3];
vector<int> vf;
bool check(int mid){
memset(dp,-1,sizeof(dp));
memset(pr,-1,sizeof(pr));
for(int i=1;i<=n;i++){
for(int j=0;j<3;j++){
int k=i-1;
if(ub(all(vf),dp[k][j])==vf.end()||vf[ub(all(vf),dp[k][j])-vf.begin()]>=s[i]){
if(max(dp[k][j],s[i]+mid)>dp[i][0]){
pr[i][0]=j;
dp[i][0]=max(dp[k][j],s[i]+mid);
}
}
if(ub(all(vf),dp[k][j])==vf.end()||vf[ub(all(vf),dp[k][j])-vf.begin()]>=s[i]-mid){
if(max(dp[k][j],s[i])>dp[i][2]){
pr[i][2]=j;
dp[i][2]=max(dp[k][j],s[i]);
}
}
k--;
if(k<0)continue;
if(s[i]-s[i-1]>mid){
if(ub(all(vf),dp[k][j])==vf.end()||vf[ub(all(vf),dp[k][j])-vf.begin()]>=s[i-1]){
if(ub(all(vf),max(dp[k][j],s[i-1]+mid))==vf.end()||vf[ub(all(vf),max(dp[k][j],s[i-1]+mid))-vf.begin()]>=s[i]-mid){
if(max(dp[k][j],s[i])>dp[i][1]){
pr[i][1]=j;
dp[i][1]=max(dp[k][j],s[i]);
}
}
}
}
else{
if(ub(all(vf),dp[k][j])==vf.end()||vf[ub(all(vf),dp[k][j])-vf.begin()]>=s[i]-mid){
if(max(dp[k][j],s[i-1]+mid)>dp[i][1]){
pr[i][1]=j;
dp[i][1]=max(dp[k][j],s[i-1]+mid);
}
}
}
}
// for(int j=0;j<4;j++){
// if(dp[i][j]!=-1)cout<<i<<" "<<j<<" "<<dp[i][j]<<"\n";
// }
}
for(int j=0;j<3;j++){
// cout<<dp[n][j]<<"\n";
if(dp[n][j]>=f[m])return 1;
}
return 0;
}
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++)cin>>s[i];
for(int i=1;i<=m;i++){
cin>>f[i];
vf.pb(f[i]);
}
int l=0,r=1e9,res=-1;
while(l<=r){
int m=(l+r)/2;
if(check(m)){
r=m-1;
res=m;
}
else l=m+1;
}
cout<<res<<"\n";
if(res!=-1){
check(res);
int a=n,b=-1;
string s;
for(int k=0;k<3;k++)if(dp[n][k]>=f[n]){
b=k;
break;
}
while(a>0){
if(b==0){
b=pr[a][b];
a--;
s.pb('R');
}
else if(b==1){
b=pr[a][b];
a-=2;
s.pb('L');
s.pb('R');
}
else if(b==2){
b=pr[a][b];
a--;
s.pb('L');
}
}
reverse(all(s));
cout<<s<<"\n";
}
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Correct |
2 |
Correct |
3 ms |
2640 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2640 KB |
Correct |
2 |
Correct |
9 ms |
3536 KB |
Correct |
3 |
Correct |
3 ms |
2812 KB |
Correct |
4 |
Correct |
10 ms |
3536 KB |
Correct |
5 |
Correct |
10 ms |
3536 KB |
Correct |
6 |
Correct |
3 ms |
2640 KB |
Correct |
7 |
Correct |
2 ms |
2640 KB |
Correct |
8 |
Correct |
4 ms |
3112 KB |
Correct |
9 |
Correct |
3 ms |
2808 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Correct |
2 |
Correct |
29 ms |
3536 KB |
Correct |
3 |
Correct |
87 ms |
2916 KB |
Correct |
4 |
Correct |
1014 ms |
4188 KB |
Correct |
5 |
Correct |
1095 ms |
4112 KB |
Correct |
6 |
Correct |
3 ms |
2640 KB |
Correct |
7 |
Correct |
3 ms |
2640 KB |
Correct |
8 |
Correct |
527 ms |
4116 KB |
Correct |
9 |
Correct |
567 ms |
4140 KB |
Correct |
10 |
Correct |
792 ms |
4296 KB |
Correct |
11 |
Correct |
393 ms |
3448 KB |
Correct |
12 |
Correct |
432 ms |
3792 KB |
Correct |
13 |
Correct |
851 ms |
3864 KB |
Correct |
14 |
Correct |
865 ms |
3884 KB |
Correct |
15 |
Correct |
1004 ms |
3792 KB |
Correct |
16 |
Correct |
758 ms |
4040 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Correct |
2 |
Correct |
3 ms |
2640 KB |
Correct |
3 |
Correct |
3 ms |
2640 KB |
Correct |
4 |
Correct |
3 ms |
2640 KB |
Correct |
5 |
Correct |
3 ms |
2640 KB |
Correct |
6 |
Correct |
3 ms |
2640 KB |
Correct |
7 |
Correct |
3 ms |
2640 KB |
Correct |
8 |
Correct |
3 ms |
2640 KB |
Correct |
9 |
Correct |
3 ms |
2640 KB |
Correct |
10 |
Correct |
3 ms |
2640 KB |
Correct |
11 |
Correct |
3 ms |
2640 KB |
Correct |
12 |
Correct |
3 ms |
2640 KB |
Correct |
13 |
Correct |
3 ms |
2640 KB |
Correct |
14 |
Correct |
3 ms |
2640 KB |
Correct |
15 |
Correct |
3 ms |
2640 KB |
Correct |
16 |
Correct |
3 ms |
2640 KB |
Correct |
17 |
Correct |
3 ms |
2640 KB |
Correct |
18 |
Correct |
3 ms |
2640 KB |
Correct |
19 |
Correct |
3 ms |
2640 KB |
Correct |
20 |
Correct |
3 ms |
2640 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Correct |
2 |
Correct |
192 ms |
3784 KB |
Correct |
3 |
Correct |
1271 ms |
4300 KB |
Correct |
4 |
Correct |
1306 ms |
6088 KB |
Correct |
5 |
Correct |
1295 ms |
6044 KB |
Correct |
6 |
Correct |
1290 ms |
6044 KB |
Correct |
7 |
Correct |
1283 ms |
6088 KB |
Correct |
8 |
Correct |
1300 ms |
6088 KB |
Correct |
9 |
Correct |
1283 ms |
6088 KB |
Correct |
10 |
Correct |
1310 ms |
6044 KB |
Correct |
11 |
Correct |
1319 ms |
6048 KB |
Correct |
12 |
Correct |
3 ms |
2640 KB |
Correct |
13 |
Correct |
3 ms |
2640 KB |
Correct |
14 |
Correct |
355 ms |
4424 KB |
Correct |
15 |
Correct |
579 ms |
4440 KB |
Correct |
16 |
Correct |
535 ms |
4444 KB |
Correct |
17 |
Correct |
521 ms |
4808 KB |
Correct |
18 |
Correct |
536 ms |
4872 KB |
Correct |
19 |
Correct |
632 ms |
5076 KB |
Correct |
20 |
Correct |
1085 ms |
5656 KB |
Correct |
21 |
Correct |
1037 ms |
5664 KB |
Correct |
22 |
Correct |
933 ms |
5576 KB |
Correct |
23 |
Correct |
967 ms |
5456 KB |
Correct |
24 |
Correct |
855 ms |
5256 KB |
Correct |
25 |
Correct |
862 ms |
5264 KB |
Correct |
26 |
Correct |
705 ms |
4132 KB |
Correct |
27 |
Correct |
464 ms |
3992 KB |
Correct |
28 |
Correct |
888 ms |
4920 KB |
Correct |
29 |
Correct |
799 ms |
4840 KB |
Correct |
30 |
Correct |
881 ms |
4888 KB |
Correct |
31 |
Correct |
830 ms |
5244 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2640 KB |
Correct |
2 |
Correct |
3 ms |
2640 KB |
Correct |
3 |
Correct |
9 ms |
3536 KB |
Correct |
4 |
Correct |
3 ms |
2812 KB |
Correct |
5 |
Correct |
10 ms |
3536 KB |
Correct |
6 |
Correct |
10 ms |
3536 KB |
Correct |
7 |
Correct |
3 ms |
2640 KB |
Correct |
8 |
Correct |
2 ms |
2640 KB |
Correct |
9 |
Correct |
4 ms |
3112 KB |
Correct |
10 |
Correct |
3 ms |
2808 KB |
Correct |
11 |
Correct |
29 ms |
3536 KB |
Correct |
12 |
Correct |
87 ms |
2916 KB |
Correct |
13 |
Correct |
1014 ms |
4188 KB |
Correct |
14 |
Correct |
1095 ms |
4112 KB |
Correct |
15 |
Correct |
3 ms |
2640 KB |
Correct |
16 |
Correct |
3 ms |
2640 KB |
Correct |
17 |
Correct |
527 ms |
4116 KB |
Correct |
18 |
Correct |
567 ms |
4140 KB |
Correct |
19 |
Correct |
792 ms |
4296 KB |
Correct |
20 |
Correct |
393 ms |
3448 KB |
Correct |
21 |
Correct |
432 ms |
3792 KB |
Correct |
22 |
Correct |
851 ms |
3864 KB |
Correct |
23 |
Correct |
865 ms |
3884 KB |
Correct |
24 |
Correct |
1004 ms |
3792 KB |
Correct |
25 |
Correct |
758 ms |
4040 KB |
Correct |
26 |
Correct |
3 ms |
2640 KB |
Correct |
27 |
Correct |
3 ms |
2640 KB |
Correct |
28 |
Correct |
3 ms |
2640 KB |
Correct |
29 |
Correct |
3 ms |
2640 KB |
Correct |
30 |
Correct |
3 ms |
2640 KB |
Correct |
31 |
Correct |
3 ms |
2640 KB |
Correct |
32 |
Correct |
3 ms |
2640 KB |
Correct |
33 |
Correct |
3 ms |
2640 KB |
Correct |
34 |
Correct |
3 ms |
2640 KB |
Correct |
35 |
Correct |
3 ms |
2640 KB |
Correct |
36 |
Correct |
3 ms |
2640 KB |
Correct |
37 |
Correct |
3 ms |
2640 KB |
Correct |
38 |
Correct |
3 ms |
2640 KB |
Correct |
39 |
Correct |
3 ms |
2640 KB |
Correct |
40 |
Correct |
3 ms |
2640 KB |
Correct |
41 |
Correct |
3 ms |
2640 KB |
Correct |
42 |
Correct |
3 ms |
2640 KB |
Correct |
43 |
Correct |
3 ms |
2640 KB |
Correct |
44 |
Correct |
192 ms |
3784 KB |
Correct |
45 |
Correct |
1271 ms |
4300 KB |
Correct |
46 |
Correct |
1306 ms |
6088 KB |
Correct |
47 |
Correct |
1295 ms |
6044 KB |
Correct |
48 |
Correct |
1290 ms |
6044 KB |
Correct |
49 |
Correct |
1283 ms |
6088 KB |
Correct |
50 |
Correct |
1300 ms |
6088 KB |
Correct |
51 |
Correct |
1283 ms |
6088 KB |
Correct |
52 |
Correct |
1310 ms |
6044 KB |
Correct |
53 |
Correct |
1319 ms |
6048 KB |
Correct |
54 |
Correct |
3 ms |
2640 KB |
Correct |
55 |
Correct |
3 ms |
2640 KB |
Correct |
56 |
Correct |
355 ms |
4424 KB |
Correct |
57 |
Correct |
579 ms |
4440 KB |
Correct |
58 |
Correct |
535 ms |
4444 KB |
Correct |
59 |
Correct |
521 ms |
4808 KB |
Correct |
60 |
Correct |
536 ms |
4872 KB |
Correct |
61 |
Correct |
632 ms |
5076 KB |
Correct |
62 |
Correct |
1085 ms |
5656 KB |
Correct |
63 |
Correct |
1037 ms |
5664 KB |
Correct |
64 |
Correct |
933 ms |
5576 KB |
Correct |
65 |
Correct |
967 ms |
5456 KB |
Correct |
66 |
Correct |
855 ms |
5256 KB |
Correct |
67 |
Correct |
862 ms |
5264 KB |
Correct |
68 |
Correct |
705 ms |
4132 KB |
Correct |
69 |
Correct |
464 ms |
3992 KB |
Correct |
70 |
Correct |
888 ms |
4920 KB |
Correct |
71 |
Correct |
799 ms |
4840 KB |
Correct |
72 |
Correct |
881 ms |
4888 KB |
Correct |
73 |
Correct |
830 ms |
5244 KB |
Correct |
74 |
Correct |
60 ms |
4560 KB |
Correct |
75 |
Correct |
1077 ms |
6052 KB |
Correct |
76 |
Correct |
1192 ms |
6100 KB |
Correct |
77 |
Correct |
3 ms |
2640 KB |
Correct |
78 |
Correct |
606 ms |
6108 KB |
Correct |
79 |
Correct |
793 ms |
6064 KB |
Correct |
80 |
Correct |
591 ms |
6064 KB |
Correct |
81 |
Correct |
383 ms |
4420 KB |
Correct |
82 |
Correct |
484 ms |
4444 KB |
Correct |
83 |
Correct |
470 ms |
4424 KB |
Correct |
84 |
Correct |
92 ms |
3960 KB |
Correct |
85 |
Correct |
910 ms |
5240 KB |
Correct |
86 |
Correct |
1046 ms |
5276 KB |
Correct |
87 |
Correct |
825 ms |
5240 KB |
Correct |
88 |
Correct |
11 ms |
4560 KB |
Correct |
89 |
Correct |
15 ms |
4452 KB |
Correct |