이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define all(arr) (arr).begin(), (arr).end()
#define ll long long
#define ld long double
#define pb push_back
#define sz(x) (int)(x).size()
#define fi first
#define se second
#define endl '\n'
const int D = 5005;
int d;
int calc(int x){
x %= d;
if (x < 0) x += d;
return x;
}
struct linkedlist{
int n;
int maxval;
vector<int> nxt, prv, val, rollbacks;
int calcdist(int i){
if (nxt[i] > i) return val[nxt[i]] - val[i];
else return val[nxt[i]] - val[i] + d;
}
linkedlist(){};
linkedlist(vector<pair<int, int>>& a){
n = sz(a);
nxt.resize(n);
prv.resize(n);
val.resize(n);
iota(all(nxt), 1);
nxt[n - 1] = 0;
iota(all(prv), -1);
prv[0] = n - 1;
for (int i = 0; i < n; i++) val[i] = a[i].fi;
maxval = 0;
for (int i = 0; i < n; i++){
maxval = max(maxval, calcdist(i));
}
};
void del(int idx){
if (nxt[idx] == idx) return;
nxt[prv[idx]] = nxt[idx];
prv[nxt[idx]] = prv[idx];
maxval = max(maxval, calcdist(prv[idx]));
rollbacks.pb(idx);
}
void unroll(){
while (!rollbacks.empty()){
int v = rollbacks.back();
rollbacks.pop_back();
nxt[prv[v]] = v, prv[nxt[v]] = v;
}
}
int get(){
return maxval;
}
};
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
// cout.precision(20);
int n, m;
cin >> n >> m >> d;
vector<int> y(n + m), nodeid(n + m);
vector<pair<int, int>> sy(n + m);
vector<vector<int>> hasx(d);
vector<int> cnt(d);
for (int i = 0; i < n + m; i++){
int x;
cin >> x >> y[i];
if (i < n) cnt[x]++;
else hasx[x].pb(i);
sy[i] = {y[i], i};
}
sort(all(sy));
linkedlist ls(sy);
int startmaxval = ls.get();
for (int i = 0; i < n + m; i++) nodeid[sy[i].se] = i;
int ans = 1e9;
for (int start = 0; start < d; start++){
int acnt = n;
for (int finish = calc(start - 1);; finish = calc(finish - 1)){
if (acnt == 0){
ans = min(ans, max(1, d - calc(finish - start) - 1) * (d + 1 - ls.get()));
}
for (auto i : hasx[finish]){
ls.del(nodeid[i]);
}
acnt -= cnt[finish];
if (finish == start){
ans = min(ans, d * (d + 1 - ls.get()));
break;
}
}
ls.unroll();
ls.maxval = startmaxval;
}
cout << ans << endl;
}
# | 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... |