답안 #1039627

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1039627 2024-07-31T06:10:42 Z 박영우(#11029) Sprinklers (CEOI24_sprinklers) C++17
9 / 100
137 ms 8016 KB
//#define LOCAL
#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<short, short> pss;
#define MAX 1010101
#define MAXS 20
#define INF 1'000'000'010'000'000'000
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define TC 0
#ifdef LOCAL
#define DEBUG(a) cout<<a
#else
#define DEBUG(...) 
#endif
int S[MAX];
int F[MAX];
int dp[2][MAX];
int ans[MAX];
int path[2][MAX];
int N, M;
int findpv(int x) {
	int ind = lower_bound(F + 1, F + M + 1, x) - F;
	ind--;
	return ind;
}
int findne(int x) {
	int ind = upper_bound(F + 1, F + M + 1, x) - F;
	return ind;
}
bool chk(int m) {
	if (S[1] - m <= F[1]) dp[0][1] = findne(S[1]);
	else dp[0][1] = 0;
	if (S[1] <= F[1]) dp[1][1] = findne(S[1] + m);
	else dp[1][1] = 1;
	path[0][1] = 0;
	path[1][1] = 1;
	int i;
	DEBUG('m' << m << ln);
	for (i = 2; i <= N; i++) {
		dp[0][i] = dp[1][i] = 0;
		if (dp[0][i - 1]) {
			if (F[dp[0][i - 1]] < S[i]) {
				dp[1][i] = dp[0][i - 1];
				path[1][i] = 0;
				if (S[i] - F[dp[0][i - 1]] <= m) {
					dp[0][i] = findne(S[i]);
					path[0][i] = 0;
				}
			}
			else {
				dp[1][i] = findne(S[i] + m);
				path[1][i] = 0;
			}
		}
		if (dp[1][i - 1]) {
			if (F[dp[1][i - 1]] < S[i]) {
				if (dp[1][i] < dp[1][i - 1]) {
					dp[1][i] = dp[1][i - 1];
					path[1][i] = 1;
				}
				if (S[i] - F[dp[1][i - 1]] <= m) {
					int nv = findne(S[i - 1] + m);
					if (dp[0][i] < nv) {
						dp[0][i] = nv;
						path[0][i] = 1;
					}
				}
			}
			else {
				int nv = findne(S[i] + m);
				if (dp[1][i] < nv) {
					dp[1][i] = nv;
					path[1][i] = 1;
				}
			}
		}
	}
	for (i = 1; i <= N; i++) DEBUG(dp[0][i] << bb);
	DEBUG(ln);
	for (i = 1; i <= N; i++) DEBUG(dp[1][i] << bb);
	DEBUG(ln);
	return (dp[0][N] > M || dp[1][N] > M);
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> N >> M;
	int i;
	set<int> st;
	for (i = 1; i <= N; i++) cin >> S[i], st.insert(S[i]);
	for (i = 1; i <= M; i++) {
		cin >> F[i];
		if (st.find(F[i]) != st.end()) {
			i--;
			M--;
		}
	}
	if (!M) {
		cout << 0 << ln;
		for (i = 1; i <= N; i++) cout << 'L';
		return 0;
	}
	int l, r;
	l = 0;
	r = 1'000'000'100;
	//r = 2;
	while (r - l > 1) {
		int m = l + r >> 1;
		if (chk(m)) r = m;
		else l = m;
	}
	if (!chk(r)) {
		cout << -1;
		return 0;
	}
	cout << r << ln;
	int v = 0;
	if (dp[1][N] > M) v = 1;
	string s;
	for (i = N; i >= 1; i--) {
		if (v) s.push_back('R');
		else s.push_back('L');
		v = path[v][i];
	}
	reverse(s.begin(), s.end());
	cout << s;
}

Compilation message

Main.cpp: In function 'int main()':
Main.cpp:118:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  118 |   int m = l + r >> 1;
      |           ~~^~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 6 ms 1880 KB Correct
3 Correct 1 ms 2392 KB Correct
4 Correct 7 ms 1628 KB Correct
5 Correct 7 ms 1628 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 2 ms 596 KB Correct
9 Correct 0 ms 2512 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 11 ms 3928 KB Correct
3 Correct 9 ms 860 KB Correct
4 Correct 116 ms 6544 KB Correct
5 Correct 118 ms 8016 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 62 ms 4796 KB Correct
9 Correct 55 ms 5216 KB Correct
10 Correct 107 ms 6224 KB Correct
11 Correct 45 ms 6484 KB Correct
12 Correct 51 ms 3412 KB Correct
13 Correct 137 ms 5460 KB Correct
14 Correct 110 ms 5804 KB Correct
15 Correct 96 ms 5932 KB Correct
16 Correct 103 ms 6740 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 0 ms 348 KB Correct
3 Incorrect 0 ms 348 KB User solution is worse than jury's solution
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Incorrect 24 ms 2396 KB User solution is worse than jury's solution
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 6 ms 1880 KB Correct
4 Correct 1 ms 2392 KB Correct
5 Correct 7 ms 1628 KB Correct
6 Correct 7 ms 1628 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 0 ms 348 KB Correct
9 Correct 2 ms 596 KB Correct
10 Correct 0 ms 2512 KB Correct
11 Correct 11 ms 3928 KB Correct
12 Correct 9 ms 860 KB Correct
13 Correct 116 ms 6544 KB Correct
14 Correct 118 ms 8016 KB Correct
15 Correct 0 ms 348 KB Correct
16 Correct 0 ms 348 KB Correct
17 Correct 62 ms 4796 KB Correct
18 Correct 55 ms 5216 KB Correct
19 Correct 107 ms 6224 KB Correct
20 Correct 45 ms 6484 KB Correct
21 Correct 51 ms 3412 KB Correct
22 Correct 137 ms 5460 KB Correct
23 Correct 110 ms 5804 KB Correct
24 Correct 96 ms 5932 KB Correct
25 Correct 103 ms 6740 KB Correct
26 Incorrect 0 ms 348 KB User solution is worse than jury's solution
27 Halted 0 ms 0 KB -