# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
965436 | Pring | Garden (JOI23_garden) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("O3","unroll-loops")
#pragma GCC target("avx2","popcnt","sse4","abm")
using namespace std;
#ifdef MIKU
string dbmc = "\033[1;38;2;57;197;187m", dbrs = "\033[0m";
#define debug(x...) cout << dbmc << "[" << #x << "]: ", dout(x)
void dout() { cout << dbrs << endl; }
template <typename T, typename ...U>
void dout(T t, U ...u) { cout << t << (sizeof...(u) ? ", " : ""); dout(u...); }
#else
#define debug(...) 39
#endif
#define fs first
#define sc second
#define mp make_pair
#define FOR(i, j, k) for (int i = j, Z = k; i < Z; i++)
using ll = long long;
typedef pair<int, int> pii;
const int MXN = 5005;
int n, m, d, p[MXN], q[MXN];
multiset<int> MS;
vector<int> bck[MXN * 2];
int c[MXN * 2];
int ori;
int CALC_ORI() {
vector<int> v;
FOR(i, 0, n + m) v.push_back(q[i]);
sort(v.begin(), v.end());
int ans = 0;
FOR(i, 1, n + m) ans = max(ans, v[i] - v[i - 1]);
ans = max(ans, d - v.back() + v.front());
return ans;
}
int calc(int sr) {
int big = ori, cnt = 0, ans = INT_MAX;
FOR(i, 0, d) {
cnt += c[sr + i];
for (auto &j : bck[sr + i]) {
auto it = MS.find(q[j] + d);
auto itr = next(it), itl = prev(it);
big = max(big, *itr - *itl);
MS.erase(MS.find(q[j]));
MS.erase(MS.find(q[j] + d));
MS.erase(MS.find(q[j] + d + d));
}
if (cnt == n) ans = min(ans, (i + 1) * (d - big + 1));
}
FOR(i, n, n + m) {
MS.insert(q[i]);
MS.insert(q[i] + d);
MS.insert(q[i] + d + d);
}
return ans;
}
void miku() {
cin >> n >> m >> d;
FOR(i, 0, n) {
cin >> p[i] >> q[i];
cnt[p[i]]++;
cnt[p[i] + d]++;
}
FOR(i, n, n + m) {
cin >> p[i] >> q[i];
bck[p[i]].push_back(i);
bck[p[i] + d].push_back(i);
}
ori = CALC_ORI();
FOR(i, 0, n + m) {
MS.insert(q[i]);
MS.insert(q[i] + d);
MS.insert(q[i] + d + d);
}
int ans = INT_MAX;
FOR(i, 0, d) ans = min(ans, calc(i));
cout << ans << '\n';
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin.exceptions(cin.failbit);
miku();
return 0;
}