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 N=305;
const int INF=INT_MAX;
int n,r,c;
int x[N],y[N];
int calc(int xl,int xr){
vector<int> xs;
// for(int i=0;i<n;i++){
// xs.emplace_back(max(1,x[i]-xl));
// xs.emplace_back(max(1,x[i]-xl-1));
// xs.emplace_back(min(r,x[i]+xr+1));
// }
// xs.emplace_back(1);
// xs.emplace_back(r);
// sort(xs.begin(),xs.end());
// xs.erase(unique(xs.begin(),xs.end()),xs.end());
// int xn=xs.size();
int xn=r;
vector<vector<pair<int,int>>> event(xn);
for(int i=0;i<n;i++){
// int xi=lower_bound(xs.begin(),xs.end(),max(1,x[i]-xl))-xs.begin();
// int xj=upper_bound(xs.begin(),xs.end(),min(r,x[i]+xr))-xs.begin();
int xi=max(1,x[i]-xl);
int xj=min(r,x[i]+xr);
event[xi-1].emplace_back(y[i],1);
if(xj<xn){
event[xj].emplace_back(y[i],0);
}
}
int mxa=0,mxb=0,sum=0;
multiset<int> pts,dif;
for(auto &e:event){
for(auto [x,t]:e){
if(t){
auto it=pts.emplace(x);
if(next(it)!=pts.end()&&it!=pts.begin()){
dif.erase(dif.find(*next(it)-*prev(it)));
}
if(next(it)!=pts.end()){
dif.emplace(*next(it)-*it);
}
if(it!=pts.begin()){
dif.emplace(*it-*prev(it));
}
}else{
auto it=pts.find(x);
assert(it!=pts.end());
if(next(it)!=pts.end()){
dif.erase(dif.find(*next(it)-*it));
}
if(it!=pts.begin()){
dif.erase(dif.find(*it-*prev(it)));
}
if(next(it)!=pts.end()&&it!=pts.begin()){
dif.emplace(*next(it)-*prev(it));
}
pts.erase(it);
}
}
if(pts.empty())return INF;
mxa=max(mxa,*pts.begin()-1);
mxb=max(mxb,c-*pts.rbegin());
if(!dif.empty()){
sum=max(sum,*dif.rbegin()-1);
}
}
return xl+xr+max(sum,mxa+mxb);
}
int main(){
cin.tie(nullptr)->sync_with_stdio(false);
cin >> r >> c >> n;
for(int i=0;i<n;i++){
cin >> x[i] >> y[i];
}
// vector<int> xs;
// for(int i=0;i<n;i++){
// xs.emplace_back(x[i]-1);
// xs.emplace_back(r-x[i]);
// for(int j=0;j<i;j++){
// int dx=abs(x[i]-x[j])-1;
// if(dx>=0)xs.emplace_back(dx);
// }
// }
// sort(xs.begin(),xs.end());
// xs.erase(unique(xs.begin(),xs.end()),xs.end());
int ans=INF;
// for(int xl:xs){
// for(int xr:xs){
// ans=min(ans,calc(xl,xr));
// }
// }
for(int xl=0;xl<=r;xl++){
for(int xr=0;xr<=r;xr++){
ans=min(ans,calc(xl,xr));
}
}
cout << ans;
}
# | 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... |