이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct DSU{
vector <int> pa,siz;
void init(int n){
for (int i=0; i<=n; i++){
pa.push_back(i);
siz.push_back(1);
}
}
int prt(int n){
if (pa[n]==n) return n;
return pa[n]=prt(pa[n]);
}
void unn(int u,int v){
u=prt(u); v=prt(v);
if (u==v) return;
if (siz[u]<siz[v]) swap(u,v);
pa[v]=u;
siz[u]+=siz[v];
}
int getsiz(int u){
return siz[prt(u)];
}
};
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
int n,m,d;
cin>>n>>m>>d;
int p[n+1],q[n+1],r[m+1],s[m+1];
for (int i=1; i<=n; i++) cin>>p[i]>>q[i];
for (int i=1; i<=m; i++) cin>>r[i]>>s[i];
int cost[d];
for (int i=0; i<d; i++) cost[i]=-1;
for (int i=1; i<=n; i++) cost[q[i]]=1e9;
for (int i=1; i<=m; i++) cost[s[i]]=max(cost[s[i]],r[i]%d+1);
bool done[d];
int ans=d*d;
set <int> f;
for (int i=1; i<=n; i++){
f.insert(p[i]);
f.insert(p[i]+d);
}
vector <int> ins[d];
for (int i=1; i<=m; i++) ins[r[i]].push_back(s[i]);
for (int i=0; i<d; i++){
if (i){
for (int j:ins[i-1]) cost[j]=i+d-1;
}
vector <pair <int,int> > v;
for (int j=0; j<d; j++) v.push_back({cost[j]-j,j});
sort(v.begin(),v.end());
int height=0;
auto it=f.lower_bound(i+d);
height=*(--it)-i+1;
DSU dsu;
dsu.init(d);
for (int j=0; j<d; j++) done[j]=0;
int gap=0;
ans=min(ans,height*d);
for (auto j:v){
height=max(height,j.first);
if (height>=d+10) break;
int prv=(j.second+d-1)%d;
if (done[prv]) dsu.unn(prv,j.second);
int nxt=(j.second+1)%d;
if (done[nxt]) dsu.unn(j.second,nxt);
gap=max(gap,dsu.getsiz(j.second));
done[j.second]=1;
ans=min(ans,height*(d-gap));
}
}
cout<<ans<<'\n';
}
# | 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... |