#include "bits/stdc++.h"
using namespace std;
#define ar array
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int R, C; cin>>R>>C;
int n; cin>>n;
vector<int> x(n), y(n);
for(int i=0;i<n;i++){
cin>>x[i]>>y[i];
}
int res = R + C;
auto check = [&](int a, int c){
vector<ar<int, 2>> s;
s.push_back({1, 0});
s.push_back({n + 1, 0});
for(int i=0;i<n;i++){
//~ x[i] - a, x[i] + c
int l = max(1, x[i] - a), r = min(R, x[i] + c);
s.push_back({l, y[i]});
s.push_back({r + 1, -y[i]});
}
sort(s.begin(), s.end());
multiset<int> ss, bet;
int m = s.size();
int A = 0, B = 0, C = 0;
for(int i=0;i<m;){
int j = i;
while(j<m && s[j][0] == s[i][0]){
if(s[j][1] > 0){
auto rx = ss.lower_bound(s[j][1]);
auto lx = rx;
if(lx != ss.begin() && rx != ss.end()){
lx--;
bet.erase(bet.find(*rx - *lx - 1));
lx++;
}
if(rx != ss.end()){
bet.insert(*rx - s[j][1] - 1);
} if(lx != ss.begin()){
lx--;
bet.insert(s[j][1] - *lx - 1);
lx++;
}
ss.insert(s[j][1]);
} else if(s[j][1] < 0) {
s[j][1] *= -1;
ss.erase(ss.find(s[j][1]));
auto rx = ss.lower_bound(s[j][1]);
auto lx = rx;
if(rx != ss.end()){
bet.erase(bet.find(*rx - s[j][1] - 1));
} if(lx != ss.begin()){ lx--;
bet.erase(bet.find(s[j][1] - *lx - 1));
lx++;
}
if(lx != ss.begin() && rx != ss.end()){
lx--;
bet.insert(*rx - *lx - 1);
lx++;
}
}
j++;
}
if(j < m && ss.empty()){
return;
}
int left = 0, right = 0, l_r = 0;
if(!ss.empty()) left = *ss.begin() - 1;
if(!ss.empty()) right = C - *--ss.end();
if(!bet.empty()) l_r = *--bet.end();
//~ tot.push_back({s[i][0], left, right, l_r});
A = max(A, left);
B = max(B, right);
C = max(C, l_r);
i = j;
}
//~ cout<<a<<" "<<c<<" "<<A<<" "<<B<<" "<<C<<endl;
res = min(res, a + c + max(A + B, C));
};
for(int a=0;a<R;a++){
for(int c=0;a+c<R;c++){
check(a, c);
}
}
cout<<res<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2081 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2081 ms |
212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |