Submission #801548

# Submission time Handle Problem Language Result Execution time Memory
801548 2023-08-02T06:33:15 Z 박영우(#10093) Cultivation (JOI17_cultivation) C++17
30 / 100
422 ms 16752 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<int, int> pii;
#define MAX 202020
#define MAXS 20
#define INF 2000000020
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
int H, W, N;
pii arr[MAX];
int ans = 2e9;
vector<int> st[MAX], en[MAX];
int U[MAX], D[MAX], UD[MAX];
bool test(int d) {
	if (d <= 0) return false;
	vector<int> ys;
	int 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());
	int X = ys.size();
	multiset<int> 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++) {
		int 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]) {
			int 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 {
					int ne = *it;
					it--;
					int 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]) {
			int 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 {
					int ne = *it;
					it--;
					int 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;
	int 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;
}
int ans2 = 2e9;
void nc(int l, int r) {
	vector<int> ys;
	int 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());
	int X = ys.size();
	multiset<int> 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++) {
		int 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);
	}
	int uu, dd, uudd;
	uu = dd = uudd = 0;
	for (i = 0; i < X; i++) {
		for (auto v : st[i]) {
			int 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 {
					int ne = *it;
					it--;
					int 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]) {
			int 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 {
					int ne = *it;
					it--;
					int 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 = 2e9;
			else {
				uu = max(uu, *alls.begin() - 1);
				dd = max(dd, H - *alls.rbegin());
			}
		}
	}
	ans2 = min(ans2, max(uu + dd, uudd) + l + r);
}
int naive() {
	vector<int> lv;
	int 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);
	int i, j;
	vector<int> 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<int> 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<int> 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(int, int)':
cultivation.cpp:102:9: warning: unused variable 'j' [-Wunused-variable]
  102 |  int i, j;
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9816 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 6 ms 9768 KB Output is correct
4 Correct 6 ms 9824 KB Output is correct
5 Correct 6 ms 9732 KB Output is correct
6 Correct 5 ms 9816 KB Output is correct
7 Correct 6 ms 9816 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 6 ms 9800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9816 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 6 ms 9768 KB Output is correct
4 Correct 6 ms 9824 KB Output is correct
5 Correct 6 ms 9732 KB Output is correct
6 Correct 5 ms 9816 KB Output is correct
7 Correct 6 ms 9816 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 6 ms 9800 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 17 ms 10204 KB Output is correct
19 Correct 17 ms 9824 KB Output is correct
20 Correct 8 ms 9824 KB Output is correct
21 Correct 31 ms 9948 KB Output is correct
22 Correct 63 ms 10768 KB Output is correct
23 Correct 12 ms 9812 KB Output is correct
24 Correct 109 ms 12228 KB Output is correct
25 Correct 81 ms 11468 KB Output is correct
26 Correct 182 ms 16708 KB Output is correct
27 Correct 214 ms 16716 KB Output is correct
28 Correct 111 ms 12288 KB Output is correct
29 Correct 160 ms 16640 KB Output is correct
30 Correct 183 ms 16752 KB Output is correct
31 Correct 192 ms 16740 KB Output is correct
32 Correct 187 ms 16708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9816 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 6 ms 9768 KB Output is correct
4 Correct 6 ms 9824 KB Output is correct
5 Correct 6 ms 9732 KB Output is correct
6 Correct 5 ms 9816 KB Output is correct
7 Correct 6 ms 9816 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 6 ms 9800 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 17 ms 10204 KB Output is correct
19 Correct 17 ms 9824 KB Output is correct
20 Correct 8 ms 9824 KB Output is correct
21 Correct 31 ms 9948 KB Output is correct
22 Correct 63 ms 10768 KB Output is correct
23 Correct 12 ms 9812 KB Output is correct
24 Correct 109 ms 12228 KB Output is correct
25 Correct 81 ms 11468 KB Output is correct
26 Correct 182 ms 16708 KB Output is correct
27 Correct 214 ms 16716 KB Output is correct
28 Correct 111 ms 12288 KB Output is correct
29 Correct 160 ms 16640 KB Output is correct
30 Correct 183 ms 16752 KB Output is correct
31 Correct 192 ms 16740 KB Output is correct
32 Correct 187 ms 16708 KB Output is correct
33 Correct 28 ms 16712 KB Output is correct
34 Correct 314 ms 16716 KB Output is correct
35 Correct 357 ms 16708 KB Output is correct
36 Correct 409 ms 16724 KB Output is correct
37 Correct 368 ms 16708 KB Output is correct
38 Correct 393 ms 16708 KB Output is correct
39 Correct 422 ms 16708 KB Output is correct
40 Correct 407 ms 16708 KB Output is correct
41 Correct 327 ms 16708 KB Output is correct
42 Correct 332 ms 16740 KB Output is correct
43 Correct 402 ms 16708 KB Output is correct
44 Correct 315 ms 16708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Incorrect 121 ms 9856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 121 ms 9856 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9816 KB Output is correct
2 Correct 6 ms 9812 KB Output is correct
3 Correct 6 ms 9768 KB Output is correct
4 Correct 6 ms 9824 KB Output is correct
5 Correct 6 ms 9732 KB Output is correct
6 Correct 5 ms 9816 KB Output is correct
7 Correct 6 ms 9816 KB Output is correct
8 Correct 5 ms 9812 KB Output is correct
9 Correct 5 ms 9812 KB Output is correct
10 Correct 6 ms 9940 KB Output is correct
11 Correct 7 ms 9812 KB Output is correct
12 Correct 5 ms 9820 KB Output is correct
13 Correct 6 ms 9812 KB Output is correct
14 Correct 5 ms 9820 KB Output is correct
15 Correct 5 ms 9812 KB Output is correct
16 Correct 6 ms 9800 KB Output is correct
17 Correct 6 ms 9812 KB Output is correct
18 Correct 17 ms 10204 KB Output is correct
19 Correct 17 ms 9824 KB Output is correct
20 Correct 8 ms 9824 KB Output is correct
21 Correct 31 ms 9948 KB Output is correct
22 Correct 63 ms 10768 KB Output is correct
23 Correct 12 ms 9812 KB Output is correct
24 Correct 109 ms 12228 KB Output is correct
25 Correct 81 ms 11468 KB Output is correct
26 Correct 182 ms 16708 KB Output is correct
27 Correct 214 ms 16716 KB Output is correct
28 Correct 111 ms 12288 KB Output is correct
29 Correct 160 ms 16640 KB Output is correct
30 Correct 183 ms 16752 KB Output is correct
31 Correct 192 ms 16740 KB Output is correct
32 Correct 187 ms 16708 KB Output is correct
33 Correct 28 ms 16712 KB Output is correct
34 Correct 314 ms 16716 KB Output is correct
35 Correct 357 ms 16708 KB Output is correct
36 Correct 409 ms 16724 KB Output is correct
37 Correct 368 ms 16708 KB Output is correct
38 Correct 393 ms 16708 KB Output is correct
39 Correct 422 ms 16708 KB Output is correct
40 Correct 407 ms 16708 KB Output is correct
41 Correct 327 ms 16708 KB Output is correct
42 Correct 332 ms 16740 KB Output is correct
43 Correct 402 ms 16708 KB Output is correct
44 Correct 315 ms 16708 KB Output is correct
45 Incorrect 121 ms 9856 KB Output isn't correct
46 Halted 0 ms 0 KB -