#include "bits/stdc++.h"
using namespace std;
#define ar array
#define int long long
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 mn_pos = -1;
{
auto check = [&](int m){
vector<ar<int, 2>> s(n);
for(int i=0;i<n;i++) s[i] = {x[i], x[i] + m - 1};
sort(s.begin(), s.end());
int l = -1, r = -1, mx = 0;
for(auto x : s){
if(r + 1 < x[0]){
mx = max(mx, r - l + 1);
l = x[0], r = x[1];
} else {
r = max(r, x[1]);
}
} mx = max(mx, r - l + 1);
return (mx >= R);
};
int l = 1, r = R;
while(l < r){
int m = (l + r) >> 1;
if(check(m)) r = m;
else l = m + 1;
}
mn_pos = l - 1;
}
vector<int> tot;
tot.push_back(mn_pos);
tot.push_back(R - 1);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(abs(x[i] - x[j]) - 1 >= mn_pos) tot.push_back(abs(x[i] - x[j]) - 1);
}
}
sort(tot.begin(), tot.end());
tot.erase(unique(tot.begin(), tot.end()), tot.end());
int res = R + C - 2;
auto check = [&](int a, int add){
vector<ar<int, 2>> s;
for(int i=0;i<n;i++){
s.push_back({x[i], y[i]});
s.push_back({x[i] + a + 1, -y[i]});
}
multiset<int> ss, bet;
sort(s.begin(), s.end());
//~ for(auto x : s) cout<<x[0]<<" "<<x[1]<<endl;
//~ cout<<endl;
int m = s.size();
vector<ar<int, 5>> tot;
//~ cout<<"here"<<endl;
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 {
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++;
}
int t = 0;
if(j < m){
if((s[i][1] > 0) && (s[j][1] < 0)) t = 1;
if((s[i][1] < 0) && (s[j][1] > 0)) t = -1;
}
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();
//~ for(auto x : ss) cout<<x<<" ";
//~ cout<<endl;
//~ for(auto x : bet) cout<<x<<" ";
//~ cout<<endl;
//~ cout<<s[i][0]<<" "<<left<<" "<<right<<" "<<l_r<<endl;
tot.push_back({s[i][0], left, right, l_r, t});
i = j;
}
//~ for(auto x : tot){
//~ cout<<x[0]<<" "<<x[1]<<" "<<x[2]<<" "<<x[3]<<" "<<x[4]<<"\n";
//~ }
//~ int r = 0, mn = C;
m = tot.size();
for(int i=0;i<m;i++){
int A = 0, B = 0, C = 0, cc = 0;
for(int j=i+1;j<m;j++){
A = max(A, tot[j-1][1]);
B = max(B, tot[j-1][2]);
C = max(C, tot[j-1][3]);
cc += tot[j-1][4];
if(tot[j][0] - tot[i][0] >= R){
res = min(res, a + max(A + B, C));
} else if(cc * add >= R - (tot[j][0] - tot[i][0])){
res = min(res, a + max(A + B, C) + (R - (tot[j][0] - tot[i][0]) + cc - 1) / cc);
}
if(add) assert(cc < 2);
}
}
//~ multiset<int> A, B, C;
//~ for(int l=0;l<m;l++){
//~ while(r<m && tot[r][0] - tot[l][0] < R){
//~ A.insert(tot[r][1]);
//~ B.insert(tot[r][2]);
//~ C.insert(tot[r][3]);
//~ r++;
//~ } if(r == m) break;
//~ mn = min(mn, max(*--A.end() + *--B.end(), *--C.end()));
//~ A.erase(A.find(tot[l][1]));
//~ B.erase(B.find(tot[l][2]));
//~ C.erase(C.find(tot[l][3]));
//~ }
//~ res = min(res, a + mn);
};
//~ cout<<"here"<<endl;
for(int i=0;i<(int)tot.size();i++){
check(tot[i], (i+1 < (int)tot.size() ? tot[i+1] - tot[i] - 1 : R));
}
cout<<res<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Runtime error |
1 ms |
468 KB |
Execution killed with signal 6 |
4 |
Halted |
0 ms |
0 KB |
- |