#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#ifdef local
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define pary(a...) danb(#a, a)
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : ")\033[0m\n")));
}
template <typename T> void danb(const char *s, T L, T R) {
std::cerr << "\033[1;32m[ " << s << " ] = [ ";
for (int f = 0; L != R; ++L)
std::cerr << (f++ ? ", " : "") << *L;
std::cerr << " ]\033[0m\n";
}
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#define pary(...) ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define get_pos(u,x) int(lower_bound(all(u),x)-begin(u))
using namespace std;
using ll = int_fast64_t;
using ld = long double;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const int mod = 1000000007;
const int inf = 1e9;
const ll INF = 1e18;
const int maxn = 5025;
template <typename T> void sort_uni(T &v) { sort(all(v)), v.erase(unique(all(v)), v.end()); }
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
int R, C, N;
cin >> R >> C >> N;
vector<pair<int,int>> pts(N);
for (auto &[x, y]: pts) cin >> x >> y;
auto solve = [&]() {
sort(all(pts));
vector<int> dx{0};
for (int i = 0; i < N; i++) {
for (int j = 0; j < i; j++) {
dx.emplace_back(pts[i].first - pts[j].first - 1);
}
dx.emplace_back(pts[i].first - 1);
dx.emplace_back(R - pts[i].first);
}
sort_uni(dx);
int mina = pts[0].first - 1;
int minb = R - pts[N-1].first;
int minsum = 0;
for (int i = 1; i < N; i++) minsum = max(minsum, pts[i].first - pts[i-1].first - 1);
int res = 2e9;
for (int a: dx) {
if (a < mina) continue;
for (int b: dx) {
if (b < minb) continue;
if (a + b < minsum) continue;
vector<pair<int,int>> evt;
for (auto [x, y]: pts) evt.emplace_back(x-a, y);
for (auto [x, y]: pts) evt.emplace_back(x+b+1, -y);
inplace_merge(evt.begin(), evt.begin() + N, evt.end());
multiset<int> posy;
multiset<int> diff;
auto add = [&](int val) {
debug("ADD", val);
pary(all(posy));
pary(all(diff));
auto it = posy.insert(val);
if (it != posy.begin() && next(it) != posy.end())
diff.erase(diff.find(*next(it) - *prev(it)));
if (it != posy.begin())
diff.insert(val - *prev(it));
if (next(it) != posy.end())
diff.insert(*next(it) - val);
debug("ADD - finish", val);
pary(all(posy));
pary(all(diff));
};
auto remove = [&](int val) {
debug("REM", val);
pary(all(posy));
pary(all(diff));
auto it = posy.find(val);
if (it != posy.begin())
diff.erase(diff.find(val - *prev(it)));
if (next(it) != posy.end())
diff.erase(diff.find(*next(it) - val));
if (it != posy.begin() && next(it) != posy.end())
diff.insert(*next(it) - *prev(it));
posy.erase(it);
debug("REM - finish", val);
pary(all(posy));
pary(all(diff));
};
int ans = 0;
for (int i = 0, j = 0; i < N*2; i = j) {
for (j = i; j < N*2; j++) {
if (evt[j].first != evt[i].first) break;
int y = evt[j].second;
if (y < 0) {
remove(-y);
} else {
add(y);
}
}
if (j == N*2) continue;
int l = evt[i].first;
int r = evt[j].first;
if (r <= 1 || l > R) continue;
assert (posy.size() > 0);
ans = max(ans, (*posy.begin() - 1) + (C - *prev(posy.end())));
if (diff.size())
ans = max(ans, *prev(diff.end()) - 1);
}
debug(a, b, ans);
res = min(res, a + b + ans);
}
}
return res;
};
int res = solve();
for (auto &[x, y]: pts) swap(x, y);
cout << min(res, solve()) << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
324 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |