#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
using namespace std;
// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr
// END NO SAD
template<class Fun>
class y_combinator_result {
Fun fun_;
public:
template<class T>
explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}
template<class ...Args>
decltype(auto) operator()(Args &&...args) {
return fun_(std::ref(*this), std::forward<Args>(args)...);
}
};
template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}
template<class T>
bool updmin(T& a, T b) {
if(b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool updmax(T& a, T b) {
if(b > a) {
a = b;
return true;
}
return false;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<vector<ll>> matrix;
void initcolumnsweep(vector<vector<int>>& columnsweep, const vector<pii>& v, int r, int c) {
int n = sz(v);
for(int i = 0; i < n; i++) {
set<int> dp;
dp.insert(-1);
dp.insert(c);
columnsweep[i].resize(n);
fill(all(columnsweep[i]), c-1);
multiset<int> intervals;
intervals.insert(c);
for(int j = i; j < n; j++) {
if(dp.count(v[j].s)) continue;
int lhs, rhs;
{
auto it = dp.ub(v[j].s);
rhs = *(it--);
lhs = *it;
}
dp.insert(v[j].s);
int amt = rhs - lhs - 1;
intervals.erase(intervals.find(amt));
intervals.insert(v[j].s - lhs - 1);
intervals.insert(rhs - v[j].s - 1);
int upneed = *dp.ub(-1);
int downneed; {
auto it = dp.rbegin(); it++;
downneed = c-1 - *it;
}
columnsweep[i][j] = max(upneed + downneed, *intervals.rbegin());
}
}
}
void solve() {
int r, c;
cin >> r >> c;
int n;
cin >> n;
vector<pii> v(n); // sorted in row major order
for(auto& x: v) {
cin >> x.f >> x.s;
x.f--; x.s--;
}
int maxdist = 0;
for(int i = 1; i < n; i++) updmax(maxdist, v[i].f - v[i-1].f - 1);
sort(all(v));
vector<vector<int>> columnsweep(n); // columnsweep[i][j] is the minimum number of cols needed to swap left/right
initcolumnsweep(columnsweep, v, r, c);
int ret = 2e9;
for(int upidx = 0; upidx < n; upidx++) {
for(int downidx = upidx; downidx < n; downidx++) {
int rowmove = v[downidx].f - v[upidx].f;
rowmove = max(rowmove, v[0].f);
rowmove = max(rowmove, r-1-v[n-1].f);
rowmove = max(rowmove, maxdist);
int rhs = 0;
for(int a = 0; a < n; a++) {
while(rhs < n && v[rhs].f - v[a].f <= rowmove) {
rhs++;
}
rhs--;
ret = min(ret, rowmove + columnsweep[a][rhs]);
if(rhs == n-1) break;
}
}
}
cout << ret << "\n";
}
// what would chika do
// are there edge cases (N=1?)
// are array sizes proper (scaled by proper constant, for example 2* for koosaga tree)
// integer overflow?
// DS reset properly between test cases
// are you doing geometry in floating points
// are you not using modint when you should
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Incorrect |
0 ms |
312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |