#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=2e9+7;
const int LIM=307;
pair<ll,ll>T[LIM];
ll R, C, n;
vector<pair<ll,ll>>Ci, Di;
ll solveab(ll a, ll b) {
ll c=0, d=0, cpd=0, lst=1;
ll po=0, ko=Ci.size()-1;
while(po<ko) {
ll sr=(po+ko)/2;
if(Ci[sr].st>b) po=sr+1; else ko=sr;
}
c=Ci[po].nd;
set<pair<ll,ll>>A;
multiset<ll>B;
vector<pair<ll,ll>>V;
vector<ll>odw(n);
rep(i, n) V.pb({max(1ll, T[i].st-a), i});
rep(i, n) V.pb({min(R+1, T[i].st+b+1), i});
sort(all(V));
for(auto i : V) {
if(i.st!=lst) {
if(A.size()) {
auto it=A.begin();
auto p=*it;
//c=max(c, p.st-1);
it=A.end(); --it;
p=*it;
d=max(d, C-p.st);
}
if(B.size()) {
auto it=B.end(); --it;
ll p=*it;
cpd=max(cpd, p);
}
lst=i.st;
}
if(!odw[i.nd]) {
odw[i.nd]=1;
if(A.size()) {
ll lewo=0, prawo=C+1;
auto it=A.lower_bound({T[i.nd].nd, i.nd});
if(it!=A.end()) {
auto p=*it;
prawo=p.st;
}
if(it!=A.begin()) {
--it;
auto p=*it;
lewo=p.st;
}
if(lewo!=0 && prawo!=C+1) B.erase(B.find(prawo-lewo-1));
if(lewo!=0) B.insert(T[i.nd].nd-lewo-1);
if(prawo!=C+1) B.insert(prawo-T[i.nd].nd-1);
}
A.insert({T[i.nd].nd, i.nd});
} else {
A.erase({T[i.nd].nd, i.nd});
if(A.size()) {
ll lewo=0, prawo=C+1;
auto it=A.lower_bound({T[i.nd].nd, i.nd});
if(it!=A.end()) {
auto p=*it;
prawo=p.st;
}
if(it!=A.begin()) {
--it;
auto p=*it;
lewo=p.st;
}
if(lewo!=0 && prawo!=C+1) B.insert(prawo-lewo-1);
if(lewo!=0) B.erase(B.find(T[i.nd].nd-lewo-1));
if(prawo!=C+1) B.erase(B.find(prawo-T[i.nd].nd-1));
}
}
}
return a+b+max(c+d, cpd);
}
ll solvea(ll a) {
Ci.clear(); Di.clear();
vector<pair<ll,ll>>P;
rep(i, n) P.pb({T[i].nd, i});
sort(all(P));
rep(i, n) {
vector<pair<ll,ll>>Q;
rep(j, i+1) Q.pb({max(1ll, T[P[j].nd].st-a), T[P[j].nd].st});
sort(all(Q));
ll ma=1, mi=0;
for(auto j : Q) {
mi=max(mi, j.st-ma-1);
ma=max(ma, j.nd);
}
mi=max(mi, R-ma);
Ci.pb({mi, P[i].st-1});
}
vector<ll>A;
rep(i, n) A.pb(T[i].st);
sort(all(A));
ll b=R-A[n-1], apb=0;
rep(i, n-1) apb=max(apb, A[i+1]-A[i]-1);
ll ans=INF;
rep(i, n) {
ll x=R-T[i].st;
if(x>=b && a+x>=apb) ans=min(ans, solveab(a, x));
}
rep(i, n) rep(j, i) {
ll x=abs(T[i].st-T[j].st)-1;
if(x==-1 || x<apb) continue;
ll y=x-a;
if(y>=b) ans=min(ans, solveab(a, y));
}
return ans;
}
ll solve() {
ll ans=INF;
rep(i, n) ans=min(ans, solvea(T[i].st-1));
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> R >> C >> n;
rep(i, n) cin >> T[i].st >> T[i].nd;
ll ans=INF;
rep(i, 4) {
ans=min(ans, solve());
rep(j, n) T[j]={C-T[j].nd+1, T[j].st};
swap(R, C);
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |