#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]);
}
int 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);
for(int i=1;i<=n;i++)
{
if(l[i] == 0)
{
while(true);
}
}
int poz = n;
while(poz >= 1)
{
r[poz] = dir[poz];
if(l[poz] == poz - 1)
{
r[poz - 1] = dirm[poz];
}
else
{
for(int j=poz-1;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 |
9 ms |
1628 KB |
Correct |
3 |
Correct |
1 ms |
344 KB |
Correct |
4 |
Correct |
11 ms |
1628 KB |
Correct |
5 |
Correct |
10 ms |
1644 KB |
Correct |
6 |
Correct |
1 ms |
348 KB |
Correct |
7 |
Correct |
1 ms |
348 KB |
Correct |
8 |
Correct |
3 ms |
760 KB |
Correct |
9 |
Correct |
1 ms |
348 KB |
Correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
11 ms |
1640 KB |
Correct |
3 |
Correct |
9 ms |
784 KB |
Correct |
4 |
Correct |
95 ms |
4072 KB |
Correct |
5 |
Correct |
51 ms |
4168 KB |
Correct |
6 |
Correct |
1 ms |
336 KB |
Correct |
7 |
Correct |
1 ms |
468 KB |
Correct |
8 |
Correct |
91 ms |
4072 KB |
Correct |
9 |
Correct |
146 ms |
4072 KB |
Correct |
10 |
Correct |
26 ms |
4212 KB |
Correct |
11 |
Correct |
35 ms |
2888 KB |
Correct |
12 |
Correct |
28 ms |
2556 KB |
Correct |
13 |
Correct |
56 ms |
3076 KB |
Correct |
14 |
Correct |
99 ms |
3292 KB |
Correct |
15 |
Correct |
81 ms |
3408 KB |
Correct |
16 |
Correct |
51 ms |
3012 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 |
336 KB |
Correct |
5 |
Runtime error |
6 ms |
4176 KB |
Execution killed with signal 11 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
44 ms |
1872 KB |
Correct |
3 |
Correct |
191 ms |
4140 KB |
Correct |
4 |
Correct |
204 ms |
4168 KB |
Correct |
5 |
Correct |
193 ms |
4152 KB |
Correct |
6 |
Correct |
193 ms |
4144 KB |
Correct |
7 |
Correct |
193 ms |
4164 KB |
Correct |
8 |
Runtime error |
200 ms |
6124 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Correct |
2 |
Correct |
1 ms |
348 KB |
Correct |
3 |
Correct |
9 ms |
1628 KB |
Correct |
4 |
Correct |
1 ms |
344 KB |
Correct |
5 |
Correct |
11 ms |
1628 KB |
Correct |
6 |
Correct |
10 ms |
1644 KB |
Correct |
7 |
Correct |
1 ms |
348 KB |
Correct |
8 |
Correct |
1 ms |
348 KB |
Correct |
9 |
Correct |
3 ms |
760 KB |
Correct |
10 |
Correct |
1 ms |
348 KB |
Correct |
11 |
Correct |
11 ms |
1640 KB |
Correct |
12 |
Correct |
9 ms |
784 KB |
Correct |
13 |
Correct |
95 ms |
4072 KB |
Correct |
14 |
Correct |
51 ms |
4168 KB |
Correct |
15 |
Correct |
1 ms |
336 KB |
Correct |
16 |
Correct |
1 ms |
468 KB |
Correct |
17 |
Correct |
91 ms |
4072 KB |
Correct |
18 |
Correct |
146 ms |
4072 KB |
Correct |
19 |
Correct |
26 ms |
4212 KB |
Correct |
20 |
Correct |
35 ms |
2888 KB |
Correct |
21 |
Correct |
28 ms |
2556 KB |
Correct |
22 |
Correct |
56 ms |
3076 KB |
Correct |
23 |
Correct |
99 ms |
3292 KB |
Correct |
24 |
Correct |
81 ms |
3408 KB |
Correct |
25 |
Correct |
51 ms |
3012 KB |
Correct |
26 |
Correct |
1 ms |
336 KB |
Correct |
27 |
Correct |
1 ms |
336 KB |
Correct |
28 |
Runtime error |
6 ms |
4176 KB |
Execution killed with signal 11 |
29 |
Halted |
0 ms |
0 KB |
- |