Submission #801553

# Submission time Handle Problem Language Result Execution time Memory
801553 2023-08-02T06:35:23 Z 박영우(#10093) Cultivation (JOI17_cultivation) C++17
30 / 100
802 ms 23664 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);
	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);
	for (auto v : cpy) ls.push_back(v);
	sort(ls.begin(), ls.end());
	while (ls.back() > W) ls.pop_back();
	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);
	//cout << ans << ln;
	cout << naive() << 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;
      |        ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 9864 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9848 KB Output is correct
8 Correct 4 ms 9812 KB Output is correct
9 Correct 4 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 9864 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9848 KB Output is correct
8 Correct 4 ms 9812 KB Output is correct
9 Correct 4 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 16 ms 10376 KB Output is correct
19 Correct 12 ms 9996 KB Output is correct
20 Correct 5 ms 9812 KB Output is correct
21 Correct 24 ms 10148 KB Output is correct
22 Correct 65 ms 11700 KB Output is correct
23 Correct 9 ms 9940 KB Output is correct
24 Correct 107 ms 14644 KB Output is correct
25 Correct 80 ms 13256 KB Output is correct
26 Correct 176 ms 23616 KB Output is correct
27 Correct 208 ms 23556 KB Output is correct
28 Correct 99 ms 14532 KB Output is correct
29 Correct 160 ms 23600 KB Output is correct
30 Correct 190 ms 23616 KB Output is correct
31 Correct 207 ms 23564 KB Output is correct
32 Correct 177 ms 23664 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 9864 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9848 KB Output is correct
8 Correct 4 ms 9812 KB Output is correct
9 Correct 4 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 16 ms 10376 KB Output is correct
19 Correct 12 ms 9996 KB Output is correct
20 Correct 5 ms 9812 KB Output is correct
21 Correct 24 ms 10148 KB Output is correct
22 Correct 65 ms 11700 KB Output is correct
23 Correct 9 ms 9940 KB Output is correct
24 Correct 107 ms 14644 KB Output is correct
25 Correct 80 ms 13256 KB Output is correct
26 Correct 176 ms 23616 KB Output is correct
27 Correct 208 ms 23556 KB Output is correct
28 Correct 99 ms 14532 KB Output is correct
29 Correct 160 ms 23600 KB Output is correct
30 Correct 190 ms 23616 KB Output is correct
31 Correct 207 ms 23564 KB Output is correct
32 Correct 177 ms 23664 KB Output is correct
33 Correct 25 ms 23644 KB Output is correct
34 Correct 316 ms 23600 KB Output is correct
35 Correct 340 ms 23616 KB Output is correct
36 Correct 411 ms 23592 KB Output is correct
37 Correct 319 ms 23616 KB Output is correct
38 Correct 424 ms 23616 KB Output is correct
39 Correct 419 ms 23640 KB Output is correct
40 Correct 440 ms 23556 KB Output is correct
41 Correct 326 ms 23616 KB Output is correct
42 Correct 334 ms 23616 KB Output is correct
43 Correct 427 ms 23616 KB Output is correct
44 Correct 300 ms 23616 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 128 ms 9896 KB Output is correct
2 Correct 683 ms 9940 KB Output is correct
3 Correct 777 ms 9984 KB Output is correct
4 Correct 621 ms 9976 KB Output is correct
5 Correct 802 ms 9976 KB Output is correct
6 Correct 151 ms 9980 KB Output is correct
7 Correct 19 ms 9940 KB Output is correct
8 Correct 704 ms 9976 KB Output is correct
9 Correct 266 ms 9940 KB Output is correct
10 Incorrect 772 ms 9984 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 128 ms 9896 KB Output is correct
2 Correct 683 ms 9940 KB Output is correct
3 Correct 777 ms 9984 KB Output is correct
4 Correct 621 ms 9976 KB Output is correct
5 Correct 802 ms 9976 KB Output is correct
6 Correct 151 ms 9980 KB Output is correct
7 Correct 19 ms 9940 KB Output is correct
8 Correct 704 ms 9976 KB Output is correct
9 Correct 266 ms 9940 KB Output is correct
10 Incorrect 772 ms 9984 KB Output isn't correct
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 9812 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 4 ms 9812 KB Output is correct
4 Correct 5 ms 9812 KB Output is correct
5 Correct 5 ms 9864 KB Output is correct
6 Correct 4 ms 9812 KB Output is correct
7 Correct 5 ms 9848 KB Output is correct
8 Correct 4 ms 9812 KB Output is correct
9 Correct 4 ms 9812 KB Output is correct
10 Correct 5 ms 9812 KB Output is correct
11 Correct 4 ms 9812 KB Output is correct
12 Correct 5 ms 9812 KB Output is correct
13 Correct 5 ms 9812 KB Output is correct
14 Correct 5 ms 9812 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 5 ms 9812 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 16 ms 10376 KB Output is correct
19 Correct 12 ms 9996 KB Output is correct
20 Correct 5 ms 9812 KB Output is correct
21 Correct 24 ms 10148 KB Output is correct
22 Correct 65 ms 11700 KB Output is correct
23 Correct 9 ms 9940 KB Output is correct
24 Correct 107 ms 14644 KB Output is correct
25 Correct 80 ms 13256 KB Output is correct
26 Correct 176 ms 23616 KB Output is correct
27 Correct 208 ms 23556 KB Output is correct
28 Correct 99 ms 14532 KB Output is correct
29 Correct 160 ms 23600 KB Output is correct
30 Correct 190 ms 23616 KB Output is correct
31 Correct 207 ms 23564 KB Output is correct
32 Correct 177 ms 23664 KB Output is correct
33 Correct 25 ms 23644 KB Output is correct
34 Correct 316 ms 23600 KB Output is correct
35 Correct 340 ms 23616 KB Output is correct
36 Correct 411 ms 23592 KB Output is correct
37 Correct 319 ms 23616 KB Output is correct
38 Correct 424 ms 23616 KB Output is correct
39 Correct 419 ms 23640 KB Output is correct
40 Correct 440 ms 23556 KB Output is correct
41 Correct 326 ms 23616 KB Output is correct
42 Correct 334 ms 23616 KB Output is correct
43 Correct 427 ms 23616 KB Output is correct
44 Correct 300 ms 23616 KB Output is correct
45 Correct 128 ms 9896 KB Output is correct
46 Correct 683 ms 9940 KB Output is correct
47 Correct 777 ms 9984 KB Output is correct
48 Correct 621 ms 9976 KB Output is correct
49 Correct 802 ms 9976 KB Output is correct
50 Correct 151 ms 9980 KB Output is correct
51 Correct 19 ms 9940 KB Output is correct
52 Correct 704 ms 9976 KB Output is correct
53 Correct 266 ms 9940 KB Output is correct
54 Incorrect 772 ms 9984 KB Output isn't correct
55 Halted 0 ms 0 KB -