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>
using namespace std;
const int INF = 0x3f3f3f3f;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, d; cin >> n >> m >> d;
vector<int> p(n), q(n), r(m), s(m);
vector<int> cnt_a(d, 0);
for (int i = 0; i < n; i++) cin >> p[i] >> q[i], cnt_a[p[i]]++;
vector< vector<int> > s2(d, vector<int>());
for (int i = 0; i < m; i++) cin >> r[i] >> s[i], s2[r[i]].push_back(s[i]);
int ans = INF;
for (int x1 = 0; x1 < d; x1++) {
int cnt_1 = 0;
multiset<int> st, st_diff;
for (int i = 0; i < n; i++) st.insert(q[i]);
for (int i = 0; i < m; i++) st.insert(s[i]);
for (auto it = next(st.begin()); it != st.end(); it++) st_diff.insert(*it - *prev(it));
if ((int) st.size() >= 2) st_diff.insert(*st.begin() + d - *st.rbegin());
for (int x2 = x1; x2 < x1 + d; x2++) {
cnt_1 += cnt_a[x2 % d];
for (auto &s : s2[x2 % d]) {
auto it = st.find(s);
if ((int) st.size() > 2) {
auto prv = prev(it), nxt = next(it);
if (it == st.begin()) prv = prev(st.end());
if (nxt == st.end()) nxt = st.begin();
st_diff.erase(st_diff.find(((*it - *prv) % d + d) % d));
st_diff.erase(st_diff.find(((*nxt - *it) % d + d) % d));
st_diff.insert(((*nxt - *prv) % d + d) % d);
st.erase(it);
}
}
if (cnt_1 < n) continue;
if ((int) st.size() < 2) ans = min(ans, x2 - x1 + 1);
else ans = min(ans, (x2 - x1 + 1) * (d - *st_diff.rbegin() + 1));
}
}
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |