# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1140674 | ghammazhassan | Sprinklers (CEOI24_sprinklers) | C++20 | 0 ms | 0 KiB |
// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n";
const int N=2e5+5;
const int M=1e9+7;
void solve()
{
int n,m;
cin >> n >> m;
vector<int>a(n),b(m);
for (int i=0;i<n;i++){
cin >> a[i];
}
for (int i=0;i<m;i++){
cin >> b[i];
}
if (n==1 and a[0]>b[0] and a[0]<b[m-1]){
cout << -1 << endl;
return;
}
else if (n==1){
if (a[0]>b[0]){
cout << a[0]-b[0] << endl;
cout << 'L' << endl;
}
else{
cout << b[m-1]-a[0] << endl;
cout << 'R' << endl;
}
return;
}
int l=1e9;
int mi=l;
vector<pair<int,string>>q;
// while (l){
// int i=0;
// queue<int>o;
// for (int j:b){
// o.push(j);
// }
// string s;
// for (int j:a){
// s+='W';
// }
// while (!o.empty()){
// if (i>=n){
// break;
// }
// int he=o.front();
// while (!o.empty() and a[i]<=o.front()){
// he =o.front();
// if (he<=a[i]+mi and he>=a[i]){
// o.pop();
// s[i]='R';
// }
// else if (i!=n-1){
// s[i]='R';
// i++;
// }
// else{
// break;
// }
// }
// if (o.empty()){
// continue;
// }
// he=o.front();
// int f=he+mi;
// while (i<n-1 and a[i+1]<=f){
// s[i]='R';
// i++;
// }
// if (s[i]=='R'){
// break;
// }
// s[i]='L';
// while (!o.empty()){
// he =o.front();
// if (he>=a[i]-mi and he<=a[i]){
// o.pop();
// }
// else{
// break;
// }
// }
// i++;
// }
// // cout << mi << " " << s << endl;
// for (int i=0;i<n;i++){
// if (s[i]!='L'){
// s[i]='R';
// }
// }
// q.push_back({mi,s});
// i=0;
// while (!o.empty()){
// int he=o.front();
// if ((he>=a[i]-mi and he<=a[i] and s[i]=='L') or (he<=a[i]+mi and he>=a[i] and s[i]=='R')){
// o.pop();
// }
// else if (i!=n-1){
// i++;
// }
// else{
// break;
// }
// }
// if (o.empty()){
// break;
// }
// else{
// l*=2;
// }
// // cout << mi << endl;
// mi=l;
// }
int h=l;
l=0;
mi=(l+h)/2;
int r=100;
while (l<h and r--){
int i=0;
queue<int>o;
for (int j:b){
o.push(j);
}
string s;
for (int j:a){
s+='W';
}
while (!o.empty()){
if (i>=n){
break;
}
int he=o.front();
while (!o.empty() and a[i]<=o.front()){
he =o.front();
if (he<=a[i]+mi and he>=a[i]){
o.pop();
s[i]='R';
}
else if (i!=n-1){
s[i]='R';
i++;
}
else{
break;
}
}
if (o.empty()){
continue;
}
he=o.front();
int f=he+mi;
while (i<n-1 and a[i+1]<=f){
i++;
}
while (i){
i--;
int u1=0;
int u2=m-1;
int u3=(u1+u2)/2;
while (u1<u2){
if (b[u3]>=a[i]){
u2=u3;
}
else{
u1=u3+1;
}
u3=(u1+u2)/2;
}
if (b[u3]>a[i] and b[u3]<=a[i+1]){
i++;
break;
}
}
if (s[i]=='R'){
break;
}
while (i<n-1 and s[i]=='L'){
i++;
}
s[i]='L';
while (!o.empty()){
he =o.front();
if (he>=a[i]-mi and he<=a[i]){
o.pop();
}
else{
break;
}
}
i++;
}
// cout << mi << " " << s << endl;
for (int i=0;i<n;i++){
if (s[i]!='L'){
s[i]='R';
}
}
q.push_back({mi,s});
i=0;
while (!o.empty()){
int he=o.front();
if ((he>=a[i]-mi and he<=a[i] and s[i]=='L') or (he<=a[i]+mi and he>=a[i] and s[i]=='R')){
o.pop();
}
else if (i!=n-1){
i++;
}
else{
break;
}
}
if (o.empty()){
h=mi;
}
else{
l=mi+1;
}
// cout << mi << endl;
mi=(l+h)/2;
}
for (auto i:q){
if (i.first==mi){
cout << mi <<endl;
cout << i.second << endl;
break;
}
}
}
signed main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
cout << fixed<<setprecision(9);
int t=1;
// cin >> t;
for (int _=1;_<=t;_++){
solve();
}
}// #include <bits/stdc++.h>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <map>
#include <vector>
#include <iomanip>
#include <string>
#include <queue>
#include <set>
#include <deque>
using namespace std;
#define int long long
#define endl "\n";
const int N=2e5+5;
const int M=1e9+7;
void solve()
{
int n,m;
cin >> n >> m;
vector<int>a(n),b(m);
for (int i=0;i<n;i++){
cin >> a[i];
}
for (int i=0;i<m;i++){
cin >> b[i];
}
if (n==1 and a[0]>b[0] and a[0]<b[m-1]){
cout << -1 << endl;
return;
}
else if (n==1){
if (a[0]>b[0]){
cout << a[0]-b[0] << endl;
cout << 'L' << endl;
}
else{
cout << b[m-1]-a[0] << endl;
cout << 'R' << endl;
}
return;
}
int l=1e9;
int mi=l;
vector<pair<int,string>>q;
// while (l){
// int i=0;
// queue<int>o;
// for (int j:b){
// o.push(j);
// }
// string s;
// for (int j:a){
// s+='W';
// }
// while (!o.empty()){
// if (i>=n){
// break;
// }
// int he=o.front();
// while (!o.empty() and a[i]<=o.front()){
// he =o.front();
// if (he<=a[i]+mi and he>=a[i]){
// o.pop();
// s[i]='R';
// }
// else if (i!=n-1){
// s[i]='R';
// i++;
// }
// else{
// break;
// }
// }
// if (o.empty()){
// continue;
// }
// he=o.front();
// int f=he+mi;
// while (i<n-1 and a[i+1]<=f){
// s[i]='R';
// i++;
// }
// if (s[i]=='R'){
// break;
// }
// s[i]='L';
// while (!o.empty()){
// he =o.front();
// if (he>=a[i]-mi and he<=a[i]){
// o.pop();
// }
// else{
// break;
// }
// }
// i++;
// }
// // cout << mi << " " << s << endl;
// for (int i=0;i<n;i++){
// if (s[i]!='L'){
// s[i]='R';
// }
// }
// q.push_back({mi,s});
// i=0;
// while (!o.empty()){
// int he=o.front();
// if ((he>=a[i]-mi and he<=a[i] and s[i]=='L') or (he<=a[i]+mi and he>=a[i] and s[i]=='R')){
// o.pop();
// }
// else if (i!=n-1){
// i++;
// }
// else{
// break;
// }
// }
// if (o.empty()){
// break;
// }
// else{
// l*=2;
// }
// // cout << mi << endl;
// mi=l;
// }
int h=l;
l=0;
mi=(l+h)/2;
int r=100;
while (l<h and r--){
int i=0;
queue<int>o;
for (int j:b){
o.push(j);
}
string s;
for (int j:a){
s+='W';
}
while (!o.empty()){
if (i>=n){
break;
}
int he=o.front();
while (!o.empty() and a[i]<=o.front()){
he =o.front();
if (he<=a[i]+mi and he>=a[i]){
o.pop();
s[i]='R';
}
else if (i!=n-1){
s[i]='R';
i++;
}
else{
break;
}
}
if (o.empty()){
continue;
}
he=o.front();
int f=he+mi;
while (i<n-1 and a[i+1]<=f){
i++;
}
while (i){
i--;
int u1=0;
int u2=m-1;
int u3=(u1+u2)/2;
while (u1<u2){
if (b[u3]>=a[i]){
u2=u3;
}
else{
u1=u3+1;
}
u3=(u1+u2)/2;
}
if (b[u3]>a[i] and b[u3]<=a[i+1]){
i++;
break;
}
}
if (s[i]=='R'){
break;
}
while (i<n-1 and s[i]=='L'){
i++;
}
s[i]='L';
while (!o.empty()){
he =o.front();
if (he>=a[i]-mi and he<=a[i]){
o.pop();
}
else{
break;
}
}
i++;
}
// cout << mi << " " << s << endl;
for (int i=0;i<n;i++){
if (s[i]!='L'){
s[i]='R';
}
}
q.push_back({mi,s});
i=0;
while (!o.empty()){
int he=o.front();
if ((he>=a[i]-mi and he<=a[i] and s[i]=='L') or (he<=a[i]+mi and he>=a[i] and s[i]=='R')){
o.pop();
}
else if (i!=n-1){
i++;
}
else{
break;
}
}
if (o.empty()){
h=mi;
}
else{
l=mi+1;
}
// cout << mi << endl;
mi=(l+h)/2;
}
for (auto i:q){
if (i.first==mi){
cout << mi <<endl;
cout << i.second << endl;
break;
}
}
}
signed main()
{
ios::sync_with_stdio(0);//DO NOT USE IN INTERACTIVE
cin.tie(0), cout.tie(0);//DO NOT USE IN INTERACTIVE
cout << fixed<<setprecision(9);
int t=1;
// cin >> t;
for (int _=1;_<=t;_++){
solve();
}
}