#include <bits/stdc++.h>
using namespace std;
const int nmax = 1e5;
int n,m;
int s[nmax + 5], f[nmax + 5];
int dp[nmax + 5];
char dir[nmax + 5], dirm[nmax + 5];
int l[nmax + 5];
char r[nmax + 5];
int count_flowers(int l, int r)
{
if(l > r)
{
return 0;
}
int st = 1;
int dr = m;
int poz_l = 0, poz_r = 0;
while(st <= dr)
{
int mij = (st + dr) >> 1;
if(f[mij] >= l)
{
poz_l = mij;
dr = mij - 1;
}
else
{
st = mij + 1;
}
}
st = 1;
dr = m;
while(st <= dr)
{
int mij = (st + dr) >> 1;
if(f[mij] <= r)
{
poz_r = mij;
st = mij + 1;
}
else
{
dr = mij - 1;
}
}
if(poz_l == 0 || poz_r == 0)
{
return 0;
}
return (poz_r - poz_l + 1);
}
int search_last(int val)
{
int st = 1;
int dr = n;
int poz = 0;
while(st <= dr)
{
int mij = (st + dr) >> 1;
if(s[mij] >= val)
{
poz = mij;
dr = mij - 1;
}
else
{
st = mij + 1;
}
}
return poz;
}
bool ok(int k)
{
dp[0] = -1;
for(int i=1;i<=n;i++)
{
if(count_flowers(dp[i - 1] + 1, s[i] - 1) == 0)
{
dp[i] = s[i] + k;
dir[i] = 'R';
l[i] = i;
continue;
}
dir[i] = 'L';
int poz = search_last(s[i] - k);
l[i] = poz;
if(poz == i)
{
if(count_flowers(dp[i - 1] + 1, s[i] - k - 1) == 0)
{
dp[i] = s[i];
}
else
{
dp[i] = -1;
}
}
else if(poz == i - 1)
{
if(count_flowers(dp[i - 2] + 1, s[i] - k - 1) == 0)
{
dirm[i] = 'R';
dp[i] = s[i - 1] + k;
}
else if(count_flowers(dp[i - 2] + 1, s[i - 1] - k - 1) == 0)
{
dirm[i] = 'L';
dp[i] = s[i];
}
else
{
dp[i] = -1;
}
}
else
{
if(count_flowers(dp[poz - 1] + 1, s[poz] - k - 1) == 0)
{
dp[i] = s[i - 1] + k;
}
else
{
dp[i] = -1;
}
}
}
return (dp[n] >= f[m]);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef home
freopen("nr.in","r",stdin);
freopen("nr.out","w",stdout);
#endif // home
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>s[i];
}
for(int i=1;i<=m;i++)
{
cin>>f[i];
}
int st = 0;
int dr = 1e9;
int rez = -1;
while(st <= dr)
{
int mij = (st + dr) >> 1;
if(ok(mij))
{
rez = mij;
dr = mij - 1;
}
else
{
st = mij + 1;
}
}
cout<<rez<<'\n';
if(rez == -1)
{
return 0;
}
ok(rez);
int poz = n;
while(poz >= 1)
{
r[poz] = dir[poz];
if(l[poz] == poz - 1)
{
r[poz - 1] = dirm[poz];
}
else if(l[poz] != poz)
{
r[poz - 1] = 'R';
for(int j=poz-2;j>=l[poz];j--)
{
r[j] = 'L';
}
}
poz = l[poz] - 1;
}
for(int i=1;i<=n;i++)
{
cout<<r[i];
}
cout<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
1 ms |
348 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
6 ms |
604 KB |
Correct |
3 |
Correct |
1 ms |
344 KB |
Correct |
4 |
Correct |
10 ms |
868 KB |
Correct |
5 |
Correct |
12 ms |
816 KB |
Correct |
6 |
Correct |
1 ms |
348 KB |
Correct |
7 |
Correct |
1 ms |
348 KB |
Correct |
8 |
Correct |
3 ms |
348 KB |
Correct |
9 |
Correct |
1 ms |
336 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
14 ms |
848 KB |
Correct |
3 |
Correct |
10 ms |
604 KB |
Correct |
4 |
Correct |
97 ms |
2120 KB |
Correct |
5 |
Correct |
61 ms |
2128 KB |
Correct |
6 |
Correct |
1 ms |
336 KB |
Correct |
7 |
Correct |
1 ms |
336 KB |
Correct |
8 |
Correct |
156 ms |
2120 KB |
Correct |
9 |
Correct |
214 ms |
2136 KB |
Correct |
10 |
Correct |
33 ms |
2140 KB |
Correct |
11 |
Correct |
33 ms |
1876 KB |
Correct |
12 |
Correct |
41 ms |
1328 KB |
Correct |
13 |
Correct |
49 ms |
2132 KB |
Correct |
14 |
Correct |
101 ms |
2116 KB |
Correct |
15 |
Correct |
80 ms |
2096 KB |
Correct |
16 |
Correct |
48 ms |
1996 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
1 ms |
348 KB |
Correct |
3 |
Correct |
1 ms |
336 KB |
Correct |
4 |
Correct |
1 ms |
340 KB |
Correct |
5 |
Correct |
1 ms |
336 KB |
Correct |
6 |
Correct |
1 ms |
336 KB |
Correct |
7 |
Correct |
1 ms |
336 KB |
Correct |
8 |
Correct |
1 ms |
476 KB |
Correct |
9 |
Correct |
1 ms |
336 KB |
Correct |
10 |
Correct |
2 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 |
352 KB |
Correct |
15 |
Correct |
1 ms |
352 KB |
Correct |
16 |
Correct |
1 ms |
352 KB |
Correct |
17 |
Correct |
1 ms |
352 KB |
Correct |
18 |
Correct |
1 ms |
352 KB |
Correct |
19 |
Correct |
1 ms |
352 KB |
Correct |
20 |
Correct |
1 ms |
352 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
49 ms |
1024 KB |
Correct |
3 |
Correct |
183 ms |
2244 KB |
Correct |
4 |
Correct |
209 ms |
2216 KB |
Correct |
5 |
Correct |
209 ms |
2376 KB |
Correct |
6 |
Correct |
182 ms |
2376 KB |
Correct |
7 |
Correct |
213 ms |
2376 KB |
Correct |
8 |
Correct |
190 ms |
2376 KB |
Correct |
9 |
Correct |
206 ms |
4168 KB |
Correct |
10 |
Correct |
195 ms |
4408 KB |
Correct |
11 |
Correct |
200 ms |
4176 KB |
Correct |
12 |
Correct |
1 ms |
336 KB |
Correct |
13 |
Correct |
1 ms |
336 KB |
Correct |
14 |
Correct |
55 ms |
2880 KB |
Correct |
15 |
Correct |
53 ms |
2888 KB |
Correct |
16 |
Correct |
57 ms |
2888 KB |
Correct |
17 |
Correct |
21 ms |
2896 KB |
Correct |
18 |
Correct |
25 ms |
2896 KB |
Correct |
19 |
Correct |
22 ms |
3152 KB |
Correct |
20 |
Correct |
118 ms |
3900 KB |
Correct |
21 |
Correct |
117 ms |
3760 KB |
Correct |
22 |
Correct |
74 ms |
3752 KB |
Correct |
23 |
Correct |
70 ms |
3572 KB |
Correct |
24 |
Correct |
39 ms |
3400 KB |
Correct |
25 |
Correct |
25 ms |
3408 KB |
Correct |
26 |
Correct |
73 ms |
2480 KB |
Correct |
27 |
Correct |
81 ms |
1792 KB |
Correct |
28 |
Correct |
62 ms |
3144 KB |
Correct |
29 |
Correct |
51 ms |
3152 KB |
Correct |
30 |
Correct |
54 ms |
3144 KB |
Correct |
31 |
Correct |
105 ms |
3144 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
1 ms |
348 KB |
Correct |
3 |
Correct |
6 ms |
604 KB |
Correct |
4 |
Correct |
1 ms |
344 KB |
Correct |
5 |
Correct |
10 ms |
868 KB |
Correct |
6 |
Correct |
12 ms |
816 KB |
Correct |
7 |
Correct |
1 ms |
348 KB |
Correct |
8 |
Correct |
1 ms |
348 KB |
Correct |
9 |
Correct |
3 ms |
348 KB |
Correct |
10 |
Correct |
1 ms |
336 KB |
Correct |
11 |
Correct |
14 ms |
848 KB |
Correct |
12 |
Correct |
10 ms |
604 KB |
Correct |
13 |
Correct |
97 ms |
2120 KB |
Correct |
14 |
Correct |
61 ms |
2128 KB |
Correct |
15 |
Correct |
1 ms |
336 KB |
Correct |
16 |
Correct |
1 ms |
336 KB |
Correct |
17 |
Correct |
156 ms |
2120 KB |
Correct |
18 |
Correct |
214 ms |
2136 KB |
Correct |
19 |
Correct |
33 ms |
2140 KB |
Correct |
20 |
Correct |
33 ms |
1876 KB |
Correct |
21 |
Correct |
41 ms |
1328 KB |
Correct |
22 |
Correct |
49 ms |
2132 KB |
Correct |
23 |
Correct |
101 ms |
2116 KB |
Correct |
24 |
Correct |
80 ms |
2096 KB |
Correct |
25 |
Correct |
48 ms |
1996 KB |
Correct |
26 |
Correct |
1 ms |
336 KB |
Correct |
27 |
Correct |
1 ms |
340 KB |
Correct |
28 |
Correct |
1 ms |
336 KB |
Correct |
29 |
Correct |
1 ms |
336 KB |
Correct |
30 |
Correct |
1 ms |
336 KB |
Correct |
31 |
Correct |
1 ms |
476 KB |
Correct |
32 |
Correct |
1 ms |
336 KB |
Correct |
33 |
Correct |
2 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 |
352 KB |
Correct |
38 |
Correct |
1 ms |
352 KB |
Correct |
39 |
Correct |
1 ms |
352 KB |
Correct |
40 |
Correct |
1 ms |
352 KB |
Correct |
41 |
Correct |
1 ms |
352 KB |
Correct |
42 |
Correct |
1 ms |
352 KB |
Correct |
43 |
Correct |
1 ms |
352 KB |
Correct |
44 |
Correct |
49 ms |
1024 KB |
Correct |
45 |
Correct |
183 ms |
2244 KB |
Correct |
46 |
Correct |
209 ms |
2216 KB |
Correct |
47 |
Correct |
209 ms |
2376 KB |
Correct |
48 |
Correct |
182 ms |
2376 KB |
Correct |
49 |
Correct |
213 ms |
2376 KB |
Correct |
50 |
Correct |
190 ms |
2376 KB |
Correct |
51 |
Correct |
206 ms |
4168 KB |
Correct |
52 |
Correct |
195 ms |
4408 KB |
Correct |
53 |
Correct |
200 ms |
4176 KB |
Correct |
54 |
Correct |
1 ms |
336 KB |
Correct |
55 |
Correct |
1 ms |
336 KB |
Correct |
56 |
Correct |
55 ms |
2880 KB |
Correct |
57 |
Correct |
53 ms |
2888 KB |
Correct |
58 |
Correct |
57 ms |
2888 KB |
Correct |
59 |
Correct |
21 ms |
2896 KB |
Correct |
60 |
Correct |
25 ms |
2896 KB |
Correct |
61 |
Correct |
22 ms |
3152 KB |
Correct |
62 |
Correct |
118 ms |
3900 KB |
Correct |
63 |
Correct |
117 ms |
3760 KB |
Correct |
64 |
Correct |
74 ms |
3752 KB |
Correct |
65 |
Correct |
70 ms |
3572 KB |
Correct |
66 |
Correct |
39 ms |
3400 KB |
Correct |
67 |
Correct |
25 ms |
3408 KB |
Correct |
68 |
Correct |
73 ms |
2480 KB |
Correct |
69 |
Correct |
81 ms |
1792 KB |
Correct |
70 |
Correct |
62 ms |
3144 KB |
Correct |
71 |
Correct |
51 ms |
3152 KB |
Correct |
72 |
Correct |
54 ms |
3144 KB |
Correct |
73 |
Correct |
105 ms |
3144 KB |
Correct |
74 |
Correct |
17 ms |
1900 KB |
Correct |
75 |
Correct |
121 ms |
4352 KB |
Correct |
76 |
Correct |
59 ms |
4168 KB |
Correct |
77 |
Correct |
2 ms |
336 KB |
Correct |
78 |
Correct |
162 ms |
4088 KB |
Correct |
79 |
Correct |
32 ms |
4196 KB |
Correct |
80 |
Correct |
183 ms |
4176 KB |
Correct |
81 |
Correct |
42 ms |
2888 KB |
Correct |
82 |
Correct |
27 ms |
2904 KB |
Correct |
83 |
Correct |
59 ms |
2720 KB |
Correct |
84 |
Correct |
13 ms |
1360 KB |
Correct |
85 |
Correct |
98 ms |
3348 KB |
Correct |
86 |
Correct |
77 ms |
3496 KB |
Correct |
87 |
Correct |
107 ms |
3144 KB |
Correct |
88 |
Correct |
10 ms |
1772 KB |
Correct |
89 |
Correct |
9 ms |
1836 KB |
Correct |