이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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);
for (int i = 0; i < n; i++) cin >> p[i] >> q[i];
for (int i = 0; i < m; i++) cin >> r[i] >> s[i];
vector<bool> type_1(d, false);
for (int i = 0; i < n; i++) type_1[q[i]] = true;
vector<int> last(d, -1);
for (int i = 0; i < m; i++) if (!type_1[s[i]]) last[s[i]] = max(last[s[i]], r[i]);
vector< set<int> > type_2(d, set<int>());
for (int i = 0; i < d; i++) if (last[i] != -1) type_2[last[i]].insert(i);
vector< vector<int> > by_r(d);
for (int i = 0; i < m; i++) by_r[r[i]].push_back(s[i]);
set<int> all;
vector<int> v, cnt(d, 0);
for (int i = 0; i < n; i++) all.insert(q[i]), v.push_back(q[i]), cnt[p[i]]++;
for (int i = 0; i < m; i++) all.insert(s[i]), v.push_back(s[i]);
sort(v.begin(), v.end());
int ans = INF, mx_b = -INF;
for (int i = 1; i < (int) v.size(); i++) mx_b = max(mx_b, v[i] - v[i - 1]);
if ((int) v.size() >= 2) mx_b = max(mx_b, v[0] - v.back() + d);
for (int l = 0; l < d; l++) {
int mx = mx_b, cnt_1 = 0;
set<int> st = all;
for (int r = l; r < l + d; r++) {
int tmp_r = r; r %= d;
cnt_1 += cnt[r];
for (auto &x : type_2[r]) {
auto it = st.find(x);
if ((int) st.size() > 2) {
set<int>::iterator prv, nxt = next(it);
if (it == st.begin()) prv = prev(st.end());
else prv = prev(it);
if (nxt == st.end()) nxt = st.begin();
mx = max(mx, (*nxt - *prv + d) % d);
}
st.erase(x);
if ((int) st.size() <= 1) mx = d;
}
r = tmp_r;
if (cnt_1 == n) ans = min(ans, (r - l + 1) * (d - mx + 1));
}
for (auto &x : by_r[l]) {
if (type_1[x]) continue;
type_2[last[x] % d].erase(x);
last[x] = l + d;
type_2[last[x] % d].insert(x);
}
}
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... |