Submission #1140674

#TimeUsernameProblemLanguageResultExecution timeMemory
1140674ghammazhassanSprinklers (CEOI24_sprinklers)C++20
Compilation error
0 ms0 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();
    }
}

Compilation message (stderr)

Main.cpp:273:11: error: redefinition of 'const long long int N'
  273 | const int N=2e5+5;
      |           ^
Main.cpp:15:11: note: 'const long long int N' previously defined here
   15 | const int N=2e5+5;
      |           ^
Main.cpp:274:11: error: redefinition of 'const long long int M'
  274 | const int M=1e9+7;
      |           ^
Main.cpp:16:11: note: 'const long long int M' previously defined here
   16 | const int M=1e9+7;
      |           ^
Main.cpp:275:6: error: redefinition of 'void solve()'
  275 | void solve()
      |      ^~~~~
Main.cpp:17:6: note: 'void solve()' previously defined here
   17 | void solve()
      |      ^~~~~
Main.cpp:503:8: error: redefinition of 'int main()'
  503 | signed main()
      |        ^~~~
Main.cpp:245:8: note: 'int main()' previously defined here
  245 | signed main()
      |        ^~~~
Main.cpp: In function 'int main()':
Main.cpp:248:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  248 |         freopen("input.txt","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:249:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  249 |         freopen("output.txt","w",stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~