답안 #1047676

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1047676 2024-08-07T14:57:44 Z Tsovak Sprinklers (CEOI24_sprinklers) C++17
100 / 100
117 ms 4224 KB
// -------------------- Includes -------------------- //
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <stdio.h>
#include <cstdlib>
#include <stdlib.h>
#include <array>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <list>
#include <iterator>
#include <numeric>
#include <complex>
#include <tuple>
#include <utility>
#include <cassert>
#include <assert.h>
#include <climits>
#include <limits.h>
#include <ctime>
#include <time.h>
#include <random>
#include <chrono>
#include <fstream>
using namespace std;

// -------------------- Typedefs -------------------- //

typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ld;

// -------------------- Defines -------------------- //

#define pr pair
#define mpr make_pair
#define first
#define ss second

#define mset multiset
#define mmap multimap
#define uset unordered_set
#define umap unordered_map
#define umset unordered_multiset
#define ummap unordered_multimap
#define pqueue priority_queue

#define sz(x) (int((x).size()))
#define len(x) (int((x).length()))
#define all(x) (x).begin(), (x).end()
#define clr(x) (x).clear()

#define ft front
#define bk back
#define pf push_front
#define pb push_back
#define popf pop_front
#define popb pop_back

#define lb lower_bound
#define ub upper_bound
#define bs binary_search

// -------------------- Constants -------------------- //

const int MAX = int(1e9 + 5);
const ll MAXL = ll(1e18 + 5);
const ll MOD = ll(1e9 + 7);
const ll MOD2 = ll(998244353);

// -------------------- Functions -------------------- //

void fastio()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	return;
}

void precision(int x)
{
	cout << fixed << setprecision(x);
	return;
}

ll gcd0(ll a, ll b)
{
	while (b) {
		a %= b;
		swap(a, b);
	}
	return a;
}

ll lcm0(ll a, ll b)
{
	return a / gcd0(a, b) * b;
}

bool is_prime(ll a)
{
	if (a == 1) return false;
	for (ll i = 2; i * i <= a; i++) if (a % i == 0) return false;
	return true;
}

bool is_square(ll a)
{
	ll b = ll(sqrtl(ld(a)));
	return (b * b == a);
}

bool is_cube(ll a)
{
	ll b = ll(cbrtl(ld(a)));
	return (b * b * b == a);
}

int digit_sum(ll a)
{
	int sum = 0;
	while (a) {
		sum += int(a % 10);
		a /= 10;
	}
	return sum;
}

ll binpow(ll a, int b)
{
	ll ans = 1;
	while (b) {
		if (b & 1) ans *= a;
		b >>= 1;
		a *= a;
	}
	return ans;
}

ll binpow_mod(ll a, ll b, ll mod)
{
	ll ans = 1;
	while (b) {
		if (b & 1) ans = (ans * a) % mod;
		b >>= 1;
		a = (a * a) % mod;
	}
	return ans;
}

ll factorial(int a)
{
	ll ans = 1;
	for (int i = 2; i <= a; i++) ans *= ll(i);
	return ans;
}

ll factorial_mod(int a, ll mod)
{
	ll ans = 1;
	for (int i = 2; i <= a; i++) ans = (ans * ll(i)) % mod;
	return ans;
}

// -------------------- Solution -------------------- //

const int N = 100005, M = 100005;
int a[N], b[M];
int ans[N];
int n, m;

int dp[N];
int from[N];

bool covers(int l, int r, int e) { return (l <= b[e] && b[e] <= r); }

bool check(int h)
{
	int i, j;

	for (i = 0; i <= n; i++) {
		dp[i] = 0;
		from[i] = -1;
	}

	dp[0] = 1;

	for (i = 0; i < n; i++) {
		if (dp[i + 1] < dp[i]) {
			dp[i + 1] = dp[i];
			from[i + 1] = 0;
		}

		if (covers(a[i + 1] - h, a[i + 1], dp[i])) {
			j = ub(b + 1, b + m + 1, a[i + 1]) - b;

			if (dp[i + 1] < j) {
				dp[i + 1] = j;
				from[i + 1] = 1;
			}
		}

		if (covers(a[i + 1], a[i + 1] + h, dp[i])) {
			j = ub(b + 1, b + m + 1, a[i + 1] + h) - b;

			if (dp[i + 1] < j) {
				dp[i + 1] = j;
				from[i + 1] = 2;
			}
		}


		if (i + 2 <= n && a[i + 2] - a[i + 1] < h && covers(a[i + 2] - h, a[i + 1] + h, dp[i])) {
			j = ub(b + 1, b + m + 1, a[i + 1] + h) - b;

			if (dp[i + 2] < j) {
				dp[i + 2] = j;
				from[i + 2] = 3;
			}
		}
	}

	return (dp[n] == m + 1);
}

void solve()
{
	int i, j;

	cin >> n >> m;

	for (i = 1; i <= n; i++) cin >> a[i];
	for (i = 1; i <= m; i++) {
		cin >> b[i];

		if (i > 1 && b[i] == b[i - 1]) {
			i--;
			m--;
		}
	}

	if (n == 1) {
		if (a[1] <= b[1]) cout << b[m] - a[1] << "\nR\n";
		else if (b[m] <= a[1]) cout << a[1] - b[1] << "\nL\n";
		else cout << "-1\n";

		return;
	}


	int l = 0, r = 1e9, mid;

	while (l < r) {
		mid = (l + r) / 2;

		if (check(mid)) r = mid;
		else l = mid + 1;
	}

	check(l);

	i = n;
	while (i) {
		if (from[i] == -1) assert(false);

		if (from[i] <= 1) {
			ans[i] = -1;
			i--;
		}
		else if (from[i] == 2) {
			ans[i] = 1;
			i--;
		}
		else {
			ans[i] = -1;
			ans[i - 1] = 1;
			i -= 2;
		}
	}


	cout << l << "\n";
	for (i = 1; i <= n; i++) {
		if (ans[i] == 0) i /= a[1];

		cout << (ans[i] == -1 ? 'L' : 'R');
	}
	cout << "\n";

	return;
}

void precalc()
{
	return;
}

int main()
{
	fastio();

	precalc();

	int tests = 1, tc;
	//cin >> tests;
	for (tc = 1; tc <= tests; tc++) {
		solve();
	}

	return 0;
}

/*
	# # # #   # # # #   # # # #   #       #    #       #     #
	   #      #         #     #    #     #    # #      #   #
	   #      # # # #   #     #     #   #    #   #     # #
	   #            #   #     #      # #    # # # #    #   #
	   #      # # # #   # # # #       #    #       #   #     #
*/

Compilation message

Main.cpp: In function 'void solve()':
Main.cpp:245:9: warning: unused variable 'j' [-Wunused-variable]
  245 |  int i, j;
      |         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Correct
2 Correct 0 ms 344 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Correct
2 Correct 7 ms 604 KB Correct
3 Correct 0 ms 348 KB Correct
4 Correct 6 ms 760 KB Correct
5 Correct 6 ms 856 KB Correct
6 Correct 1 ms 344 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 1 ms 348 KB Correct
9 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Correct
2 Correct 7 ms 604 KB Correct
3 Correct 6 ms 604 KB Correct
4 Correct 62 ms 2276 KB Correct
5 Correct 68 ms 2272 KB Correct
6 Correct 1 ms 348 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 46 ms 1972 KB Correct
9 Correct 42 ms 2128 KB Correct
10 Correct 46 ms 2172 KB Correct
11 Correct 15 ms 1880 KB Correct
12 Correct 32 ms 1364 KB Correct
13 Correct 73 ms 2132 KB Correct
14 Correct 70 ms 2140 KB Correct
15 Correct 60 ms 2128 KB Correct
16 Correct 68 ms 2128 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Correct
2 Correct 0 ms 344 KB Correct
3 Correct 1 ms 344 KB Correct
4 Correct 0 ms 348 KB Correct
5 Correct 0 ms 348 KB Correct
6 Correct 0 ms 476 KB Correct
7 Correct 0 ms 348 KB Correct
8 Correct 1 ms 348 KB Correct
9 Correct 0 ms 348 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 0 ms 348 KB Correct
12 Correct 0 ms 348 KB Correct
13 Correct 1 ms 348 KB Correct
14 Correct 0 ms 348 KB Correct
15 Correct 0 ms 344 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 348 KB Correct
20 Correct 0 ms 348 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Correct
2 Correct 24 ms 836 KB Correct
3 Correct 105 ms 3520 KB Correct
4 Correct 108 ms 4176 KB Correct
5 Correct 108 ms 4160 KB Correct
6 Correct 103 ms 4224 KB Correct
7 Correct 110 ms 4176 KB Correct
8 Correct 107 ms 4172 KB Correct
9 Correct 106 ms 4172 KB Correct
10 Correct 104 ms 4172 KB Correct
11 Correct 107 ms 4176 KB Correct
12 Correct 0 ms 348 KB Correct
13 Correct 0 ms 348 KB Correct
14 Correct 17 ms 2904 KB Correct
15 Correct 17 ms 2908 KB Correct
16 Correct 17 ms 2908 KB Correct
17 Correct 38 ms 2640 KB Correct
18 Correct 43 ms 2640 KB Correct
19 Correct 59 ms 2900 KB Correct
20 Correct 112 ms 3788 KB Correct
21 Correct 109 ms 3924 KB Correct
22 Correct 111 ms 3668 KB Correct
23 Correct 114 ms 3568 KB Correct
24 Correct 116 ms 3408 KB Correct
25 Correct 101 ms 3264 KB Correct
26 Correct 30 ms 2652 KB Correct
27 Correct 37 ms 1876 KB Correct
28 Correct 97 ms 3408 KB Correct
29 Correct 92 ms 3044 KB Correct
30 Correct 102 ms 3156 KB Correct
31 Correct 112 ms 3264 KB Correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 344 KB Correct
2 Correct 0 ms 344 KB Correct
3 Correct 7 ms 604 KB Correct
4 Correct 0 ms 348 KB Correct
5 Correct 6 ms 760 KB Correct
6 Correct 6 ms 856 KB Correct
7 Correct 1 ms 344 KB Correct
8 Correct 0 ms 348 KB Correct
9 Correct 1 ms 348 KB Correct
10 Correct 0 ms 348 KB Correct
11 Correct 7 ms 604 KB Correct
12 Correct 6 ms 604 KB Correct
13 Correct 62 ms 2276 KB Correct
14 Correct 68 ms 2272 KB Correct
15 Correct 1 ms 348 KB Correct
16 Correct 0 ms 348 KB Correct
17 Correct 46 ms 1972 KB Correct
18 Correct 42 ms 2128 KB Correct
19 Correct 46 ms 2172 KB Correct
20 Correct 15 ms 1880 KB Correct
21 Correct 32 ms 1364 KB Correct
22 Correct 73 ms 2132 KB Correct
23 Correct 70 ms 2140 KB Correct
24 Correct 60 ms 2128 KB Correct
25 Correct 68 ms 2128 KB Correct
26 Correct 1 ms 344 KB Correct
27 Correct 0 ms 348 KB Correct
28 Correct 0 ms 348 KB Correct
29 Correct 0 ms 476 KB Correct
30 Correct 0 ms 348 KB Correct
31 Correct 1 ms 348 KB Correct
32 Correct 0 ms 348 KB Correct
33 Correct 0 ms 348 KB Correct
34 Correct 0 ms 348 KB Correct
35 Correct 0 ms 348 KB Correct
36 Correct 1 ms 348 KB Correct
37 Correct 0 ms 348 KB Correct
38 Correct 0 ms 344 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 348 KB Correct
43 Correct 0 ms 348 KB Correct
44 Correct 24 ms 836 KB Correct
45 Correct 105 ms 3520 KB Correct
46 Correct 108 ms 4176 KB Correct
47 Correct 108 ms 4160 KB Correct
48 Correct 103 ms 4224 KB Correct
49 Correct 110 ms 4176 KB Correct
50 Correct 107 ms 4172 KB Correct
51 Correct 106 ms 4172 KB Correct
52 Correct 104 ms 4172 KB Correct
53 Correct 107 ms 4176 KB Correct
54 Correct 0 ms 348 KB Correct
55 Correct 0 ms 348 KB Correct
56 Correct 17 ms 2904 KB Correct
57 Correct 17 ms 2908 KB Correct
58 Correct 17 ms 2908 KB Correct
59 Correct 38 ms 2640 KB Correct
60 Correct 43 ms 2640 KB Correct
61 Correct 59 ms 2900 KB Correct
62 Correct 112 ms 3788 KB Correct
63 Correct 109 ms 3924 KB Correct
64 Correct 111 ms 3668 KB Correct
65 Correct 114 ms 3568 KB Correct
66 Correct 116 ms 3408 KB Correct
67 Correct 101 ms 3264 KB Correct
68 Correct 30 ms 2652 KB Correct
69 Correct 37 ms 1876 KB Correct
70 Correct 97 ms 3408 KB Correct
71 Correct 92 ms 3044 KB Correct
72 Correct 102 ms 3156 KB Correct
73 Correct 112 ms 3264 KB Correct
74 Correct 10 ms 1628 KB Correct
75 Correct 94 ms 4200 KB Correct
76 Correct 116 ms 4196 KB Correct
77 Correct 0 ms 348 KB Correct
78 Correct 31 ms 4124 KB Correct
79 Correct 39 ms 3988 KB Correct
80 Correct 29 ms 4176 KB Correct
81 Correct 15 ms 2904 KB Correct
82 Correct 18 ms 2864 KB Correct
83 Correct 17 ms 2876 KB Correct
84 Correct 13 ms 1112 KB Correct
85 Correct 101 ms 3424 KB Correct
86 Correct 90 ms 3532 KB Correct
87 Correct 117 ms 3248 KB Correct
88 Correct 6 ms 1624 KB Correct
89 Correct 7 ms 1624 KB Correct