답안 #1075739

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1075739 2024-08-26T08:57:00 Z GrindMachine Sprinklers (CEOI24_sprinklers) C++17
0 / 100
2000 ms 5828 KB
#include <bits/stdc++.h>
using namespace std;
 
typedef long long int ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
 
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL)
#define pb push_back
#define endl '\n'
#define conts continue
#define ff first
#define ss second
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define sz(a) (int)a.size()
 
#define rep(i,n) for(int i = 0; i < n; ++i)
#define rep1(i,n) for(int i = 1; i <= n; ++i) 
#define rev(i,s,e) for(int i = s; i >= e; --i)
#define trav(i,a) for(auto &i : a)
 
template<typename T>
void amin(T &x, T y){
	x = min(x,y);
}
 
template<typename T>
void amax(T &x, T y){
	x = max(x,y);
}
 
template<typename A,typename B>
string to_string(pair<A,B> p);
 
string to_string(const string &s){
	return "'"+s+"'";
}
 
string to_string(const char *s){
	return to_string((string)s);
}
 
string to_string(bool b){
	return b?"true":"false";
}
 
template<typename A>
string to_string(A v){
	string res = "{";
	trav(x,v){
		res += to_string(x)+",";
	}
	if(res.back() == ',') res.pop_back();
	res += "}";
	return res;
}
 
template<typename A,typename B>
string to_string(pair<A,B> p){
	return "("+to_string(p.ff)+","+to_string(p.ss)+")";
}
 
#define debug(x) cout << #x << " = "; cout << to_string(x) << endl
 
const int MOD = 1e9 + 7;
const int N = 1e5 + 5;
const int inf1 = 1e9 + 5;
const ll inf2 = (ll)1e18 + 5;
 
void solve(int test_case){
	ll n,m; cin >> n >> m;
	vector<ll> a(n+5), b(m+5);
	rep1(i,n) cin >> a[i], a[i]++;
	rep1(i,m) cin >> b[i], b[i]++;

	// sort(a.begin()+1,a.begin()+n+1);
	// sort(b.begin()+1,b.begin()+m+1);
 
	rep1(i,m){
		if(binary_search(a.begin()+1,a.begin()+n+1,b[i])){
			b[i] = -1;
		}
	}
 
	vector<array<ll,3>> c;
	rep1(i,n) c.pb({a[i],i,1});
	rep1(i,m){
		if(b[i] != -1){
			c.pb({b[i],i,2});
		}
	}
	sort(all(c));
	ll siz = sz(c);
 
	vector<char> ans(n+5,'?');
 
	auto ok = [&](ll mid, bool construct){
		vector<ll> dp1(m+5,-inf2);
		vector<pll> par(siz+5,{-1,-1});
		dp1[0] = 0;
		ll ind = -1;
 		ll prev_ok = -1;

		for(auto [x,i,t] : c){
			ll first_ok = -1;
			rep1(j,m){
				if(dp1[j] >= 0){
					first_ok = j;
					break;
				}
			}

			assert(first_ok >= prev_ok);
			prev_ok = first_ok;

			ind++;
			ll mxj = 0;

			if(t == 1){
				pll mx = {-inf2,-1};
				ll first_big = lower_bound(b.begin()+1,b.begin()+m+1,a[i]-mid)-b.begin();
				amin(first_big,first_ok);
				if(first_big != -1 and dp1[first_big] >= 0){
					mx = {dp1[first_big],first_big};
				}

				rep1(j,m){
					if(b[j] > a[i]) break;
					if(dp1[j] < 0) conts;
 
					// turn left
					// if(mx.ss == -1 and b[j] >= a[i]-mid){
					// 	pll px = {dp1[j],j};
					// 	mx = px;
					// }
 
					// turn right
					dp1[j] = a[i]+mid;
				}
 
				// turn right
				if(dp1[0] >= 0){
					dp1[0] = a[i]+mid;
				}
 
				// turn left
				if(mx.ff > dp1[0]){
					dp1[0] = mx.ff;
					par[ind] = {0,mx.ss};
				}
			}
			else{
				if(dp1[0] >= x) conts;
				dp1[i] = dp1[0];
				dp1[0] = -inf2;
				par[ind] = {i,0};
				mxj = i;
			}
		}
 	
 		// cout << endl << endl << endl;

		if(construct){
			ll j = 0;
			rev(ind,siz-1,0){
				if(par[ind].ff == j){
					ll j2 = par[ind].ss;
					if(c[ind][2] == 1){
						ll i = c[ind][1];
						assert(j == 0 and j2 > j);
						ans[i] = 'L';
					}
					j = j2;
				}
				else{
					if(c[ind][2] == 1){
						ll i = c[ind][1];
						ans[i] = 'R';
					}
				}
			}	
 
			assert(!j);
		}
 
		return dp1[0] >= 0;
	};
 
	ll lo = 0, hi = inf1;
	ll best = -1;
 
	while(lo <= hi){
		ll mid = (lo+hi) >> 1;
		if(ok(mid,0)){
			best = mid;
			hi = mid-1;
		}
		else{
			lo = mid+1;
		}
	}
 
	cout << best << endl;
	if(best == -1) return;
 
	ok(best,1);
	rep1(i,n) cout << ans[i];
	cout << endl;
}
 
int main(){
	int t = 1;
	// cin >> t;
 
	rep1(i,t){
		solve(i);
	}
 
	return 0;
}

Compilation message

Main.cpp: In lambda function:
Main.cpp:119:7: warning: variable 'mxj' set but not used [-Wunused-but-set-variable]
  119 |    ll mxj = 0;
      |       ^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Incorrect 36 ms 4828 KB User solution is incorrect
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Incorrect 1379 ms 5828 KB User solution is incorrect
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 0 ms 348 KB Correct
3 Incorrect 1 ms 348 KB User solution is incorrect
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Execution timed out 2063 ms 5508 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 0 ms 348 KB Correct
3 Incorrect 36 ms 4828 KB User solution is incorrect
4 Halted 0 ms 0 KB -