답안 #801613

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
801613 2023-08-02T06:55:33 Z 박영우(#10093) Cultivation (JOI17_cultivation) C++17
60 / 100
2000 ms 28732 KB
#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 pair<ll, ll> pll;
typedef pair<ll, ll> pii;
#define MAX 202020
#define MAXS 20
#define INF 2000000000000020
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
ll H, W, N;
pii arr[MAX];
ll ans = 2e9;
vector<ll> st[MAX], en[MAX];
ll U[MAX], D[MAX], UD[MAX];
bool test(ll d) {
	if (d <= 0) return false;
	vector<ll> ys;
	ll i, j;
	for (i = 1; i <= N; i++) ys.push_back(arr[i].second), ys.push_back(arr[i].second + d);
	sort(ys.begin(), ys.end());
	ys.erase(unique(ys.begin(), ys.end()), ys.end());
	ll X = ys.size();
	multiset<ll> ds, alls;
	for (i = 0; i < X; i++) U[i] = D[i] = UD[i] = 0, st[i].clear(), en[i].clear();
	for (i = 1; i <= N; i++) {
		ll l, r;
		l = lower_bound(ys.begin(), ys.end(), arr[i].second) - ys.begin();
		r = lower_bound(ys.begin(), ys.end(), arr[i].second + d) - ys.begin();
		st[l].push_back(i);
		en[r].push_back(i);
	}
	for (i = 0; i < X; i++) {
		for (auto v : st[i]) {
			ll x = arr[v].first;
			if (alls.size() && alls.find(x) == alls.end()) {
				auto it = alls.lower_bound(x);
				if (it == alls.end()) ds.insert(x - *(--it) - 1);
				else if (it == alls.begin()) ds.insert(*it - x - 1);
				else {
					ll ne = *it;
					it--;
					ll pv = *it;
					ds.erase(ds.find(ne - pv - 1));
					ds.insert(ne - x - 1);
					ds.insert(x - pv - 1);
				}
			}
			alls.insert(x);
		}
		for (auto v : en[i]) {
			ll x = arr[v].first;
			alls.erase(alls.find(x));
			if (alls.size() && alls.find(x) == alls.end()) {
				auto it = alls.lower_bound(x);
				if (it == alls.end()) ds.erase(ds.find(x - *(--it) - 1));
				else if (it == alls.begin()) ds.erase(ds.find(*it - x - 1));
				else {
					ll ne = *it;
					it--;
					ll pv = *it;
					ds.insert(ne - pv - 1);
					ds.erase(ds.find(ne - x - 1));
					ds.erase(ds.find(x - pv - 1));
				}
			}
		}
		if (ds.size()) UD[i] = *ds.rbegin();
		if (alls.empty()) UD[i] = 2e9;
		else {
			U[i] = *alls.begin() - 1;
			D[i] = H - *alls.rbegin();
		}
	}
	priority_queue<pii> up, down, su;
	ll mn = INF;
	j = 0;
	for (i = 0; i < X - 1; i++) {
		up.emplace(U[i], i);
		down.emplace(D[i], i);
		su.emplace(UD[i], i);
		while (j + 1 <= i && ys[i + 1] - ys[j + 1] >= W) j++;
		while (up.size() && up.top().second < j) up.pop();
		while (down.size() && down.top().second < j) down.pop();
		while (su.size() && su.top().second < j) su.pop();
		if (ys[i + 1] - ys[j] >= W) mn = min(mn, max(up.top().first + down.top().first, su.top().first));
	}
	if (mn >= INF) return false;
	ans = min(ans, mn + d - 1);
	return true;
}
ll ans2 = INF;
void nc(ll l, ll r) {
	vector<ll> ys;
	ll i, j;
	for (i = 1; i <= N; i++) ys.push_back(arr[i].second - l), ys.push_back(arr[i].second + r + 1);
	ys.push_back(1);
	ys.push_back(W + 1);
	ys.push_back(W);
	sort(ys.begin(), ys.end());
	ys.erase(unique(ys.begin(), ys.end()), ys.end());
	ll X = ys.size();
	multiset<ll> ds, alls;
	for (i = 0; i < X; i++) U[i] = D[i] = UD[i] = 0, st[i].clear(), en[i].clear();
	for (i = 1; i <= N; i++) {
		ll low, high;
		low = lower_bound(ys.begin(), ys.end(), arr[i].second - l) - ys.begin();
		high = lower_bound(ys.begin(), ys.end(), arr[i].second + r + 1) - ys.begin();
		st[low].push_back(i);
		en[high].push_back(i);
	}
	ll uu, dd, uudd;
	uu = dd = uudd = 0;
	for (i = 0; i < X; i++) {
		for (auto v : st[i]) {
			ll x = arr[v].first;
			if (alls.size() && alls.find(x) == alls.end()) {
				auto it = alls.lower_bound(x);
				if (it == alls.end()) ds.insert(x - *(--it) - 1);
				else if (it == alls.begin()) ds.insert(*it - x - 1);
				else {
					ll ne = *it;
					it--;
					ll pv = *it;
					ds.erase(ds.find(ne - pv - 1));
					ds.insert(ne - x - 1);
					ds.insert(x - pv - 1);
				}
			}
			alls.insert(x);
		}
		for (auto v : en[i]) {
			ll x = arr[v].first;
			alls.erase(alls.find(x));
			if (alls.size() && alls.find(x) == alls.end()) {
				auto it = alls.lower_bound(x);
				if (it == alls.end()) ds.erase(ds.find(x - *(--it) - 1));
				else if (it == alls.begin()) ds.erase(ds.find(*it - x - 1));
				else {
					ll ne = *it;
					it--;
					ll pv = *it;
					ds.insert(ne - pv - 1);
					ds.erase(ds.find(ne - x - 1));
					ds.erase(ds.find(x - pv - 1));
				}
			}
		}
		if (ys[i] > 0 && ys[i] <= W) {
			if (ds.size()) uudd = max(uudd, *ds.rbegin());
			if (alls.empty()) uudd = INF;
			else {
				uu = max(uu, *alls.begin() - 1);
				dd = max(dd, H - *alls.rbegin());
			}
		}
	}
	if (uudd < INF) ans2 = min(ans2, max(uu + dd, uudd) + l + r);
}
ll naive() {
	vector<ll> lv;
	ll i, j;
	for (i = 1; i <= N; i++) {
		for (j = 1; j <= N; j++) lv.push_back(abs(arr[j].second - arr[i].second));
		lv.push_back(arr[i].second - 1);
		lv.push_back(W - arr[i].second);
	}
	sort(lv.begin(), lv.end());
	lv.erase(unique(lv.begin(), lv.end()), lv.end());
	for (auto l : lv) for (auto r : lv) nc(l, r);
	return ans2;
}
signed main() {
	ios::sync_with_stdio(false), cin.tie(0);
	cin >> H >> W >> N;
	swap(H, W);
	ll i, j;
	vector<ll> ls;
	ls.push_back(W);
	for (i = 1; i <= 100; i++) ls.push_back(i);
	for (i = 1; i <= N; i++) cin >> arr[i].first >> arr[i].second, swap(arr[i].first, arr[i].second);
	vector<ll> sv;
	for (i = 1; i <= N; i++) {
		for (j = i + 1; j <= N; j++) ls.push_back(abs(arr[j].second - arr[i].second));
		for (j = i + 1; j <= N; j++) ls.push_back(abs(arr[j].second - arr[i].second) + 1);
		for (j = i + 1; j <= N; j++) ls.push_back(abs(arr[j].second - arr[i].second) - 1);
		ls.push_back(arr[i].second - 1);
		ls.push_back(W - arr[i].second);
		ls.push_back(arr[i].second);
		ls.push_back(W - arr[i].second + 1);
		ls.push_back(arr[i].second + 1);
		ls.push_back(W - arr[i].second + 2);
		for (j = i + 1; j <= N; j++) sv.push_back(abs(arr[j].second - arr[i].second));
		sv.push_back(arr[i].second - 1);
		sv.push_back(W - arr[i].second);
	}
	vector<ll> cpy;
	for (auto v : ls) cpy.push_back(W - v), cpy.push_back(W - v + 1), cpy.push_back(W - v - 1), cpy.push_back(W + v + 1), cpy.push_back(W + v), cpy.push_back(W + v - 1);
	for (auto v : cpy) ls.push_back(v);
	sort(ls.begin(), ls.end());
	ls.erase(unique(ls.begin(), ls.end()), ls.end());
	reverse(ls.begin(), ls.end());
	while (ls.back() <= 0) ls.pop_back();
	for (auto v : ls) for (i = -3; i <= 3; i++) test(v + i);
	//assert(ans == naive());
	cout << ans << ln;
}

Compilation message

cultivation.cpp: In function 'void nc(ll, ll)':
cultivation.cpp:102:8: warning: unused variable 'j' [-Wunused-variable]
  102 |  ll i, j;
      |        ^
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9856 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 5 ms 9732 KB Output is correct
10 Correct 4 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9752 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 4 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9856 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 5 ms 9732 KB Output is correct
10 Correct 4 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9752 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 4 ms 9812 KB Output is correct
17 Correct 8 ms 9940 KB Output is correct
18 Correct 21 ms 10828 KB Output is correct
19 Correct 11 ms 10068 KB Output is correct
20 Correct 8 ms 9940 KB Output is correct
21 Correct 18 ms 10352 KB Output is correct
22 Correct 44 ms 13760 KB Output is correct
23 Correct 18 ms 10068 KB Output is correct
24 Correct 93 ms 19460 KB Output is correct
25 Correct 69 ms 14648 KB Output is correct
26 Correct 150 ms 28604 KB Output is correct
27 Correct 167 ms 28680 KB Output is correct
28 Correct 84 ms 19556 KB Output is correct
29 Correct 155 ms 28700 KB Output is correct
30 Correct 158 ms 28680 KB Output is correct
31 Correct 169 ms 28692 KB Output is correct
32 Correct 165 ms 28708 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9856 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 5 ms 9732 KB Output is correct
10 Correct 4 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9752 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 4 ms 9812 KB Output is correct
17 Correct 8 ms 9940 KB Output is correct
18 Correct 21 ms 10828 KB Output is correct
19 Correct 11 ms 10068 KB Output is correct
20 Correct 8 ms 9940 KB Output is correct
21 Correct 18 ms 10352 KB Output is correct
22 Correct 44 ms 13760 KB Output is correct
23 Correct 18 ms 10068 KB Output is correct
24 Correct 93 ms 19460 KB Output is correct
25 Correct 69 ms 14648 KB Output is correct
26 Correct 150 ms 28604 KB Output is correct
27 Correct 167 ms 28680 KB Output is correct
28 Correct 84 ms 19556 KB Output is correct
29 Correct 155 ms 28700 KB Output is correct
30 Correct 158 ms 28680 KB Output is correct
31 Correct 169 ms 28692 KB Output is correct
32 Correct 165 ms 28708 KB Output is correct
33 Correct 208 ms 28712 KB Output is correct
34 Correct 249 ms 28700 KB Output is correct
35 Correct 268 ms 28708 KB Output is correct
36 Correct 294 ms 28708 KB Output is correct
37 Correct 272 ms 28704 KB Output is correct
38 Correct 309 ms 28692 KB Output is correct
39 Correct 296 ms 28700 KB Output is correct
40 Correct 317 ms 28732 KB Output is correct
41 Correct 260 ms 28700 KB Output is correct
42 Correct 242 ms 28688 KB Output is correct
43 Correct 337 ms 28732 KB Output is correct
44 Correct 239 ms 28692 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 9960 KB Output is correct
2 Correct 242 ms 10028 KB Output is correct
3 Correct 271 ms 10040 KB Output is correct
4 Correct 225 ms 10008 KB Output is correct
5 Correct 304 ms 10032 KB Output is correct
6 Correct 52 ms 9940 KB Output is correct
7 Correct 23 ms 9984 KB Output is correct
8 Correct 271 ms 10028 KB Output is correct
9 Correct 105 ms 9940 KB Output is correct
10 Correct 265 ms 10032 KB Output is correct
11 Correct 328 ms 10028 KB Output is correct
12 Correct 269 ms 10024 KB Output is correct
13 Correct 62 ms 10028 KB Output is correct
14 Correct 143 ms 10032 KB Output is correct
15 Correct 37 ms 9940 KB Output is correct
16 Correct 275 ms 9940 KB Output is correct
17 Correct 279 ms 10028 KB Output is correct
18 Correct 70 ms 10060 KB Output is correct
19 Correct 273 ms 10028 KB Output is correct
20 Correct 207 ms 9960 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 102 ms 9960 KB Output is correct
2 Correct 242 ms 10028 KB Output is correct
3 Correct 271 ms 10040 KB Output is correct
4 Correct 225 ms 10008 KB Output is correct
5 Correct 304 ms 10032 KB Output is correct
6 Correct 52 ms 9940 KB Output is correct
7 Correct 23 ms 9984 KB Output is correct
8 Correct 271 ms 10028 KB Output is correct
9 Correct 105 ms 9940 KB Output is correct
10 Correct 265 ms 10032 KB Output is correct
11 Correct 328 ms 10028 KB Output is correct
12 Correct 269 ms 10024 KB Output is correct
13 Correct 62 ms 10028 KB Output is correct
14 Correct 143 ms 10032 KB Output is correct
15 Correct 37 ms 9940 KB Output is correct
16 Correct 275 ms 9940 KB Output is correct
17 Correct 279 ms 10028 KB Output is correct
18 Correct 70 ms 10060 KB Output is correct
19 Correct 273 ms 10028 KB Output is correct
20 Correct 207 ms 9960 KB Output is correct
21 Execution timed out 2045 ms 12440 KB Time limit exceeded
22 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9728 KB Output is correct
3 Correct 5 ms 9856 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 6 ms 9812 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 6 ms 9812 KB Output is correct
9 Correct 5 ms 9732 KB Output is correct
10 Correct 4 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9752 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 6 ms 9812 KB Output is correct
15 Correct 6 ms 9812 KB Output is correct
16 Correct 4 ms 9812 KB Output is correct
17 Correct 8 ms 9940 KB Output is correct
18 Correct 21 ms 10828 KB Output is correct
19 Correct 11 ms 10068 KB Output is correct
20 Correct 8 ms 9940 KB Output is correct
21 Correct 18 ms 10352 KB Output is correct
22 Correct 44 ms 13760 KB Output is correct
23 Correct 18 ms 10068 KB Output is correct
24 Correct 93 ms 19460 KB Output is correct
25 Correct 69 ms 14648 KB Output is correct
26 Correct 150 ms 28604 KB Output is correct
27 Correct 167 ms 28680 KB Output is correct
28 Correct 84 ms 19556 KB Output is correct
29 Correct 155 ms 28700 KB Output is correct
30 Correct 158 ms 28680 KB Output is correct
31 Correct 169 ms 28692 KB Output is correct
32 Correct 165 ms 28708 KB Output is correct
33 Correct 208 ms 28712 KB Output is correct
34 Correct 249 ms 28700 KB Output is correct
35 Correct 268 ms 28708 KB Output is correct
36 Correct 294 ms 28708 KB Output is correct
37 Correct 272 ms 28704 KB Output is correct
38 Correct 309 ms 28692 KB Output is correct
39 Correct 296 ms 28700 KB Output is correct
40 Correct 317 ms 28732 KB Output is correct
41 Correct 260 ms 28700 KB Output is correct
42 Correct 242 ms 28688 KB Output is correct
43 Correct 337 ms 28732 KB Output is correct
44 Correct 239 ms 28692 KB Output is correct
45 Correct 102 ms 9960 KB Output is correct
46 Correct 242 ms 10028 KB Output is correct
47 Correct 271 ms 10040 KB Output is correct
48 Correct 225 ms 10008 KB Output is correct
49 Correct 304 ms 10032 KB Output is correct
50 Correct 52 ms 9940 KB Output is correct
51 Correct 23 ms 9984 KB Output is correct
52 Correct 271 ms 10028 KB Output is correct
53 Correct 105 ms 9940 KB Output is correct
54 Correct 265 ms 10032 KB Output is correct
55 Correct 328 ms 10028 KB Output is correct
56 Correct 269 ms 10024 KB Output is correct
57 Correct 62 ms 10028 KB Output is correct
58 Correct 143 ms 10032 KB Output is correct
59 Correct 37 ms 9940 KB Output is correct
60 Correct 275 ms 9940 KB Output is correct
61 Correct 279 ms 10028 KB Output is correct
62 Correct 70 ms 10060 KB Output is correct
63 Correct 273 ms 10028 KB Output is correct
64 Correct 207 ms 9960 KB Output is correct
65 Execution timed out 2045 ms 12440 KB Time limit exceeded
66 Halted 0 ms 0 KB -