답안 #1039636

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1039636 2024-07-31T06:22:02 Z 박영우(#11029) Sprinklers (CEOI24_sprinklers) C++17
100 / 100
181 ms 11464 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 = max(findne(S[i]), 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 344 KB Correct
2 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Correct
2 Correct 7 ms 604 KB Correct
3 Correct 0 ms 348 KB Correct
4 Correct 7 ms 824 KB Correct
5 Correct 7 ms 860 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 344 KB Correct
8 Correct 1 ms 344 KB Correct
9 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 11 ms 2904 KB Correct
3 Correct 9 ms 2908 KB Correct
4 Correct 99 ms 4452 KB Correct
5 Correct 113 ms 4436 KB Correct
6 Correct 1 ms 2396 KB Correct
7 Correct 1 ms 2392 KB Correct
8 Correct 80 ms 2840 KB Correct
9 Correct 60 ms 3156 KB Correct
10 Correct 115 ms 4328 KB Correct
11 Correct 36 ms 3900 KB Correct
12 Correct 55 ms 2140 KB Correct
13 Correct 147 ms 4400 KB Correct
14 Correct 122 ms 4520 KB Correct
15 Correct 103 ms 4480 KB Correct
16 Correct 123 ms 4180 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 1 ms 348 KB Correct
4 Correct 0 ms 348 KB Correct
5 Correct 0 ms 348 KB Correct
6 Correct 0 ms 348 KB Correct
7 Correct 0 ms 344 KB Correct
8 Correct 0 ms 472 KB Correct
9 Correct 0 ms 2524 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 0 ms 348 KB Correct
12 Correct 0 ms 604 KB Correct
13 Correct 0 ms 348 KB Correct
14 Correct 0 ms 468 KB Correct
15 Correct 0 ms 348 KB Correct
16 Correct 0 ms 348 KB Correct
17 Correct 0 ms 348 KB Correct
18 Correct 0 ms 348 KB Correct
19 Correct 0 ms 2516 KB Correct
20 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 24 ms 1372 KB Correct
3 Correct 124 ms 7764 KB Correct
4 Correct 134 ms 11208 KB Correct
5 Correct 127 ms 11344 KB Correct
6 Correct 145 ms 11464 KB Correct
7 Correct 133 ms 11180 KB Correct
8 Correct 139 ms 11092 KB Correct
9 Correct 140 ms 11344 KB Correct
10 Correct 156 ms 11004 KB Correct
11 Correct 149 ms 11104 KB Correct
12 Correct 1 ms 2396 KB Correct
13 Correct 0 ms 344 KB Correct
14 Correct 21 ms 5768 KB Correct
15 Correct 50 ms 8168 KB Correct
16 Correct 58 ms 10388 KB Correct
17 Correct 65 ms 4944 KB Correct
18 Correct 50 ms 3408 KB Correct
19 Correct 53 ms 5712 KB Correct
20 Correct 160 ms 10888 KB Correct
21 Correct 162 ms 10836 KB Correct
22 Correct 162 ms 10320 KB Correct
23 Correct 180 ms 10372 KB Correct
24 Correct 135 ms 8528 KB Correct
25 Correct 134 ms 6840 KB Correct
26 Correct 60 ms 6740 KB Correct
27 Correct 60 ms 5716 KB Correct
28 Correct 140 ms 8016 KB Correct
29 Correct 118 ms 6936 KB Correct
30 Correct 137 ms 9240 KB Correct
31 Correct 181 ms 9192 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 344 KB Correct
2 Correct 0 ms 348 KB Correct
3 Correct 7 ms 604 KB Correct
4 Correct 0 ms 348 KB Correct
5 Correct 7 ms 824 KB Correct
6 Correct 7 ms 860 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 0 ms 344 KB Correct
9 Correct 1 ms 344 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 11 ms 2904 KB Correct
12 Correct 9 ms 2908 KB Correct
13 Correct 99 ms 4452 KB Correct
14 Correct 113 ms 4436 KB Correct
15 Correct 1 ms 2396 KB Correct
16 Correct 1 ms 2392 KB Correct
17 Correct 80 ms 2840 KB Correct
18 Correct 60 ms 3156 KB Correct
19 Correct 115 ms 4328 KB Correct
20 Correct 36 ms 3900 KB Correct
21 Correct 55 ms 2140 KB Correct
22 Correct 147 ms 4400 KB Correct
23 Correct 122 ms 4520 KB Correct
24 Correct 103 ms 4480 KB Correct
25 Correct 123 ms 4180 KB Correct
26 Correct 1 ms 348 KB Correct
27 Correct 0 ms 348 KB Correct
28 Correct 0 ms 348 KB Correct
29 Correct 0 ms 348 KB Correct
30 Correct 0 ms 344 KB Correct
31 Correct 0 ms 472 KB Correct
32 Correct 0 ms 2524 KB Correct
33 Correct 0 ms 348 KB Correct
34 Correct 0 ms 348 KB Correct
35 Correct 0 ms 604 KB Correct
36 Correct 0 ms 348 KB Correct
37 Correct 0 ms 468 KB Correct
38 Correct 0 ms 348 KB Correct
39 Correct 0 ms 348 KB Correct
40 Correct 0 ms 348 KB Correct
41 Correct 0 ms 348 KB Correct
42 Correct 0 ms 2516 KB Correct
43 Correct 0 ms 348 KB Correct
44 Correct 24 ms 1372 KB Correct
45 Correct 124 ms 7764 KB Correct
46 Correct 134 ms 11208 KB Correct
47 Correct 127 ms 11344 KB Correct
48 Correct 145 ms 11464 KB Correct
49 Correct 133 ms 11180 KB Correct
50 Correct 139 ms 11092 KB Correct
51 Correct 140 ms 11344 KB Correct
52 Correct 156 ms 11004 KB Correct
53 Correct 149 ms 11104 KB Correct
54 Correct 1 ms 2396 KB Correct
55 Correct 0 ms 344 KB Correct
56 Correct 21 ms 5768 KB Correct
57 Correct 50 ms 8168 KB Correct
58 Correct 58 ms 10388 KB Correct
59 Correct 65 ms 4944 KB Correct
60 Correct 50 ms 3408 KB Correct
61 Correct 53 ms 5712 KB Correct
62 Correct 160 ms 10888 KB Correct
63 Correct 162 ms 10836 KB Correct
64 Correct 162 ms 10320 KB Correct
65 Correct 180 ms 10372 KB Correct
66 Correct 135 ms 8528 KB Correct
67 Correct 134 ms 6840 KB Correct
68 Correct 60 ms 6740 KB Correct
69 Correct 60 ms 5716 KB Correct
70 Correct 140 ms 8016 KB Correct
71 Correct 118 ms 6936 KB Correct
72 Correct 137 ms 9240 KB Correct
73 Correct 181 ms 9192 KB Correct
74 Correct 12 ms 1880 KB Correct
75 Correct 117 ms 11400 KB Correct
76 Correct 133 ms 11176 KB Correct
77 Correct 0 ms 348 KB Correct
78 Correct 54 ms 4740 KB Correct
79 Correct 86 ms 5204 KB Correct
80 Correct 47 ms 7756 KB Correct
81 Correct 49 ms 7876 KB Correct
82 Correct 53 ms 9556 KB Correct
83 Correct 49 ms 8100 KB Correct
84 Correct 17 ms 1600 KB Correct
85 Correct 146 ms 8784 KB Correct
86 Correct 149 ms 8788 KB Correct
87 Correct 178 ms 9232 KB Correct
88 Correct 7 ms 1628 KB Correct
89 Correct 7 ms 1652 KB Correct