답안 #1039631

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1039631 2024-07-31T06:15:59 Z 박영우(#11029) Sprinklers (CEOI24_sprinklers) C++17
9 / 100
122 ms 9524 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]);
					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 9 ms 1628 KB Correct
3 Correct 0 ms 348 KB Correct
4 Correct 9 ms 1628 KB Correct
5 Correct 7 ms 1628 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 1 ms 2396 KB Correct
8 Correct 2 ms 604 KB Correct
9 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 9 ms 1884 KB Correct
3 Correct 9 ms 2904 KB Correct
4 Correct 109 ms 6376 KB Correct
5 Correct 114 ms 6480 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 55 ms 4792 KB Correct
9 Correct 57 ms 5200 KB Correct
10 Correct 93 ms 6228 KB Correct
11 Correct 36 ms 4688 KB Correct
12 Correct 50 ms 3412 KB Correct
13 Correct 118 ms 5468 KB Correct
14 Correct 114 ms 5716 KB Correct
15 Correct 98 ms 6004 KB Correct
16 Correct 114 ms 6740 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 1 ms 604 KB Correct
4 Correct 0 ms 468 KB Correct
5 Incorrect 1 ms 348 KB User solution is worse than jury's solution
6 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 2396 KB Correct
2 Correct 27 ms 4440 KB Correct
3 Incorrect 122 ms 9524 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 Correct 0 ms 348 KB Correct
3 Correct 9 ms 1628 KB Correct
4 Correct 0 ms 348 KB Correct
5 Correct 9 ms 1628 KB Correct
6 Correct 7 ms 1628 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 1 ms 2396 KB Correct
9 Correct 2 ms 604 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 9 ms 1884 KB Correct
12 Correct 9 ms 2904 KB Correct
13 Correct 109 ms 6376 KB Correct
14 Correct 114 ms 6480 KB Correct
15 Correct 0 ms 348 KB Correct
16 Correct 0 ms 348 KB Correct
17 Correct 55 ms 4792 KB Correct
18 Correct 57 ms 5200 KB Correct
19 Correct 93 ms 6228 KB Correct
20 Correct 36 ms 4688 KB Correct
21 Correct 50 ms 3412 KB Correct
22 Correct 118 ms 5468 KB Correct
23 Correct 114 ms 5716 KB Correct
24 Correct 98 ms 6004 KB Correct
25 Correct 114 ms 6740 KB Correct
26 Correct 1 ms 604 KB Correct
27 Correct 0 ms 468 KB Correct
28 Incorrect 1 ms 348 KB User solution is worse than jury's solution
29 Halted 0 ms 0 KB -