Submission #1296176

#TimeUsernameProblemLanguageResultExecution timeMemory
1296176am_aadvikGarden (JOI23_garden)C++20
0 / 100
3097 ms96248 KiB
#include <iostream>
#include<vector>
#include<set>
const int inf = 1e9;
using namespace std;

int n, m, d;
int solve(int x, int y, vector<pair<int, int>>& a, vector<pair<int, int>>& b) {
	vector<vector<vector<int>>> g(d, vector<vector<int>>(d, vector<int>(0)));
	int al = d, ar = 0, au = n, ad = 0;
	//o(d^2*(n+m)) To Be Optimized to o(d^2*m)
	for (int i = 0; i < d; ++i)
		for (int j = 0; j < d; ++j) {
			for (int k = 0; k < n; ++k) if ((((i + x) % d) == (a[k].first % d))
				&& (((j + y) % d) == (a[k].second % d))) g[i][j].push_back(k),
					al = min(al, j + y), ar = max(ar, j + y), au = min(au, i + x), ad = max(ad, i + x);
			for (int k = 0; k < m; ++k) if ((((i + x) % d) == (b[k].first % d))
				|| (((j + y) % d) == (b[k].second % d))) g[i][j].push_back(k + n);
		}
	//o(d^2*m)
	int ans = inf;
	for (int si = x; si <= au; ++si)
		for (int ei = max(si, ad); ei < (x + d); ++ei) {
			//row si to row di
			int sj = al, ej = ar;
			for (auto o : b) {
				int r = o.first % d;
				while (r < si) r += d;
				if (r > ei) {
					int c = o.second % d;
					while (c < y) c += d;
					sj = min(sj, c), ej = max(ej, c);
				}
			}
			int cur = (ej - sj + 1) * (ei - si + 1);
			if (cur == 9) {
				cur += 0;
			}
			ans = min(ans, cur);
		}
	return ans;
}
int main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL);
	cin >> n >> m >> d;
	vector<pair<int, int>> a(n);
	vector<pair<int, int>> b(m);
	for (auto& x : a) cin >> x.first >> x.second;
	for (auto& x : b) cin >> x.first >> x.second;

	int ans = inf;
	ans = min(ans, solve(0, 0, a, b));
	ans = min(ans, solve(d, d, a, b));
	for (int x = max(0, d / 2 - 2); x <= (d / 2 + 2); ++x)
		for (int y = max(0, d / 2 - 2); y <= (d / 2 + 2); ++y)
			ans = min(ans, solve(x, y, a, b));
	cout << ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...