이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 500500;
const int D = 5005;
int n, m, d, p[N], q[N], x[N], y[N], cx[D], cy[D], sy[D], px[D], py[D], a[D][D];
vector<int> stupid[D];
queue<int> que[D];
int ans = 1e9;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m >> d;
for (int i = 1; i <= n; i++) {
cin >> p[i] >> q[i];
cx[p[i]]++; cy[q[i]]++;
}
for (int i = 0; i < d; i++) {
int totx = 0, toty = 0;
for (int j = i; j < i + d; j++) {
totx += cx[j % d];
if (totx == n) {
px[i] = j - i + 1;
break;
}
}
for (int j = i; j < i + d; j++) {
toty += cy[j % d];
if (toty == n) {
py[i] = j - i + 1;
break;
}
}
}
// for (int i = 0; i < d; i++) cout << py[i] << ' ';
// cout << '\n';
for (int i = 1; i <= m; i++) {
cin >> x[i] >> y[i]; // long
sy[y[i]]++;
stupid[y[i]].push_back(x[i]);
}
for (int i = 0; i < d; i++) {
sort(stupid[i].begin(), stupid[i].end());
stupid[i].erase(unique(stupid[i].begin(), stupid[i].end()), stupid[i].end());
for (auto& stu : stupid[i]) que[i].push(stu);
}
for (int i = 0; i < d; i++) for (int j = 0; j < d; j++) a[i][j] = d;
for (int i = 0; i < d; i++) {
int tim = d, j = i;
while (tim--) {
j++;
if (j >= d) j -= d;
int di = i + d - j + 1;
if (i >= j) di = i - j + 1;
a[j][i] = min(a[((j - 1) % d + d) % d][i], max(py[j], di));
// cout << j << ' ' << i << ' ' << a[j][i] << '\n';
}
}
for (int i = 0; i < d; i++) {
vector<vector<int>> cnt(d << 1);
vector<int> ll(d, 0), rr(d, 0), hv;
int len = d;
for (int j = 0; j < d; j++) if (!que[j].empty()) cnt[que[j].front()].push_back(j);
// for (int j = 0; j < (d << 1); j++) {
// cout << "pt " << j << '\n';
// for (auto& xx : cnt[j]) cout << xx << ' ';
// cout << '\n';
// }
for (int j = 0; j < d; j++) if (sy[j]) hv.push_back(j);
for (int j = 0; j < (int) hv.size(); j++) {
int k = (j + 1) % (int) hv.size();
rr[hv[j]] = hv[k]; ll[hv[k]] = hv[j];
len = min(len, a[hv[k]][hv[j]]);
}
// cout << len << '\n';
int lb = i + px[i] - 1;
// cout << "new\n";
for (int j = i; j <= i + d - 1; j++) {
// cout << "testing: " << i << ' ' << j << '\n';
for (auto& ys : cnt[j]) {
int L = ll[ys], R = rr[ys];
ll[R] = L; rr[L] = R;
len = min(len, a[R][L]);
// cout << "del: " << ys << '\n';
// cout << "len: " << len << '\n';
// ans = min(ans, (max(lb, j) - i + 1) * len);
// cout << i << ' ' << j << ' ' << j - i + 1 << ' ' << len << '\n';
}
ans = min(ans, (max(lb, j) - i + 1) * len);
}
for (int j = 0; j < d; j++) {
while (!que[j].empty() && que[j].front() == i) {
que[j].pop();
que[j].push(i + d);
}
}
}
cout << ans << '\n';
}
/*
4 1 5
1 4
4 1
1 1
4 4
0 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... |