This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define upmin(a,b) (a)=min((a),(b))
#define upmax(a,b) (a)=max((a),(b))
#define INF (0x3f3f3f3f)
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int MAXL = 90055;
vector<int> CHX[305], CHU[305], CHD[305], CHS[305];
int DU[MAXL], DD[MAXL], DS[MAXL];
vector<int> HLX, BulX, XV;
int A[305], B[305];
int H, W, N, K, L, Ans = INF*2;
void makeChain(vector<pii> &EV, vector<int> &X, vector<int> &U, vector<int> &D, vector<int> &S) {
sorv(EV);
multiset<int> YV, DV;
YV.insert(0); YV.insert(H+1);
DV.insert(H+1);
for(int s = 0, e, n = sz(EV), px; s < n; s = e) {
px = EV[s].first;
for(e = s+1; e < n && EV[e].first == px; e++);
for(int oi = s, y; oi < e; oi++) {
y = EV[oi].second;
auto it = YV.insert(y);
if(YV.begin() != it && YV.end() != next(it))
DV.erase(DV.find(*next(it) - *prev(it)));
if(YV.begin() != it) DV.insert(y - *prev(it));
if(YV.end() != next(it)) DV.insert(*next(it) - y);
}
X.eb(px);
S.eb(*prev(DV.end())-1);
U.eb(*next(YV.begin())-1);
D.eb(H-*prev(prev(YV.end())));
}
}
int main() {
ios::sync_with_stdio(false);
cin >> H >> W >> N;
for(int i = 1; i <= N; i++) {
cin >> A[i] >> B[i];
HLX.eb(B[i]-1);
if(1 < B[i]) BulX.eb(B[i]-1);
}
sorv(HLX); univ(HLX);
sorv(BulX); univ(BulX);
K = sz(BulX); BulX.eb(W);
for(int idx = 0, x; idx <= K; idx++) {
x = BulX[idx];
vector<pii> EV;
for(int i = 1, t; i <= N; i++) {
t = x - B[i];
if(0 <= t) {
EV.eb(t, A[i]);
XV.eb(t);
}
}
makeChain(EV, CHX[idx], CHU[idx], CHD[idx], CHS[idx]);
}
sorv(XV); univ(XV); L = sz(XV);
for(int hlxi = sz(HLX), buli = K-1, lx; hlxi--;) {
lx = HLX[hlxi];
for(; 0 <= buli && lx < BulX[buli]; buli--) {
for(int i = 0, j = 0, n = sz(CHX[buli]), x; i < L; i++) {
x = XV[i];
if(j < n && CHX[buli][j] <= x) j++;
if(j) {
upmax(DU[i], CHU[buli][j-1]);
upmax(DD[i], CHD[buli][j-1]);
upmax(DS[i], CHS[buli][j-1]);
} else DU[i] = DD[i] = DS[i] = INF;
}
}
for(int i = 0, j = 0, n = sz(CHX[K]), rx, du, dd, ds; i < L || j < n;) {
rx = INF;
if(i < L) rx = XV[i]-lx;
if(j < n) upmin(rx, CHX[K][j]);
if(rx < 0) { i++; continue; }
if(i < L && XV[i]-lx == rx) i++;
if(j < n && CHX[K][j] == rx) j++;
if(!i || !j) continue;
du = max(DU[i-1], CHU[K][j-1]);
dd = max(DD[i-1], CHD[K][j-1]);
ds = max(DS[i-1], CHS[K][j-1]);
ll ret = ll(max(ds, du + dd)) + lx + rx;
if(ret < Ans) Ans = ret;
}
}
cout << Ans << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |