#pragma GCC optimize ("Ofast","unroll-loops")
#include <bits/stdc++.h>
#define pb push_back
#define eb emplace_back
#define MP make_pair
#define F first
#define S second
#define setpre(a) cout.precision(a),cout<<fixed;
#define ALL(a) a.begin(),a.end()
#define MEM(a,b) memset(a,b,sizeof a)
#define Tie ios::sync_with_stdio(0),cin.tie(0);
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
const int MAXN = 310;
const ll INF = 1LL << 61;
ll R, C, n, L[MAXN][MAXN], L_lim[MAXN][MAXN], R_lim[MAXN][MAXN], dis[MAXN][MAXN], ans = INF;
ll Ur[MAXN], Dr[MAXN], Ul[MAXN], Dl[MAXN];
vector<pll> v;
int main() {Tie
cin >> R >> C >> n;
v.resize(n);
ll rest = 0;
for(pll &i : v)
cin >> i.F >> i.S, rest = max(rest, i.F);
vector<pll> tv = v;
sort(ALL(tv));
ll minm = 0;
for(int i = 0; i + 1 < n; i++)
minm = max(minm, tv[i + 1].F - tv[i].F - 1);
for(int i = 0; i < n; i++) {
Dl[i] = Dr[i] = Ul[i] = Ur[i] = INF;
{
ll uln = 0;
ll urn = R + 1;
ll dln = 0;
ll drn = R + 1;
for(int j = 0; j < n; j++) {
if(i == j) continue;
if(v[i].S < v[j].S) {
if(v[i].F < v[j].F) {
drn = min(drn, v[j].F);
} else if(v[i].F > v[j].F) {
dln = max(dln, v[j].F);
} else {
Dl[i] = Dr[i] = -1;
}
} else if(v[i].S > v[j].S) {
if(v[i].F < v[j].F) {
urn = min(urn, v[j].F);
} else if(v[i].F > v[j].F) {
uln = max(uln, v[j].F);
} else {
Ul[i] = Ur[i] = -1;
}
}
}
if(Dl[i] != -1) {
if(dln != 0) {
Dr[i] = drn - dln - 1;
} else {
Dr[i] = INF;
}
if(drn != R + 1) {
Dl[i] = drn - dln - 1;
} else {
Dl[i] = INF;
}
}
if(Ul[i] != -1) {
if(uln != 0) {
Ur[i] = urn - uln - 1;
} else {
Ur[i] = INF;
}
if(urn != R + 1) {
Ul[i] = urn - uln - 1;
} else {
Ul[i] = INF;
}
}
}
for(int j = i + 1; j < n; j++) {
ll l = min(v[i].F, v[j].F);
ll r = max(v[i].F, v[j].F);
ll u = min(v[i].S, v[j].S);
ll d = max(v[i].S, v[j].S);
L[i][j] = r - l;
dis[i][j] = d - u - 1;
ll ln = 0;
ll rn = R + 1;
for(int k = 0; k < n; k++) {
if(u < v[k].S && v[k].S < d) {
if(l <= v[k].F && v[k].F <= r)
L_lim[i][j] = R_lim[i][j] = -1;
else if(v[k].F < l)
ln = max(ln, v[k].F);
else if(v[k].F > l)
rn = min(rn, v[k].F);
}
}
if(L_lim[i][j] == -1) continue;
if(rn != R + 1)
L_lim[i][j] = rn - ln - 1;
else
L_lim[i][j] = INF;
if(ln != 0)
R_lim[i][j] = rn - ln - 1;
else
R_lim[i][j] = INF;
}
}
vector<ll> Lm, Rm;
for(int i = 0; i < n; i++)
Lm.pb(v[i].F - 1);
sort(ALL(Lm)), Lm.resize(unique(ALL(Lm)) - Lm.begin());
sort(ALL(Rm)), Rm.resize(unique(ALL(Rm)) - Rm.begin());
for(ll lm : Lm) {
map<ll, ll> m, dm, um;
m[0] = 1;
dm[0] = 1;
um[0] = 1;
vector<pair<ll, pll>> T;
for(int j = 0; j < n; j++) {
T.pb(MP(R - v[j].F, MP(0, 0)));
}
T.pb(MP(minm - lm, MP(0, 0)));
for(int i = 0; i < n; i++) {
if(Dr[i] != -1 && Dl[i] > lm) {
dm[C - v[i].S]++;
if(Dl[i] == Dr[i] && Dr[i] != INF)
T.pb(MP(Dr[i] - lm, MP(1, C - v[i].S)));
else if(Dr[i] != INF)
T.pb(MP(Dr[i], MP(1, C - v[i].S)));
}
if(Ur[i] != -1 && Ul[i] > lm) {
um[v[i].S - 1]++;
if(Ul[i] == Ur[i] && Ur[i] != INF)
T.pb(MP(Ur[i] - lm, MP(2, v[i].S - 1)));
else if(Ur[i] != INF)
T.pb(MP(Ur[i], MP(2, v[i].S - 1)));
}
for(int j = i + 1; j < n; j++) {
if(L_lim[i][j] <= lm) continue;
if(dis[i][j] <= 0) continue;
T.pb(MP(L[i][j] - lm, MP(0, dis[i][j])));
if(L_lim[i][j] == R_lim[i][j] && R_lim[i][j] != INF)
T.pb(MP(R_lim[i][j] - lm, MP(0, -dis[i][j])));
else if(R_lim[i][j] != INF)
T.pb(MP(R_lim[i][j], MP(0, -dis[i][j])));
}
}
sort(ALL(T));
// cout << lm << '\n';
// for(auto i : T) cout << i.S.F << ' ' << i.F << ' ' << i.S.S << '\n';
int flag = 0, top = 0;
while(top < (int)T.size()) {
if(flag || T[top].F <= R - rest) {
do {
if(T[top].S.F == 0) {
if(T[top].S.S < 0) {
m[-T[top].S.S]--;
} else if(T[top].S.S > 0) {
m[T[top].S.S]++;
}
} else if(T[top].S.F == 1) {
dm[T[top].S.S]--;
} else {
um[T[top].S.S]--;
}
top++;
} while(top < (int)T.size() && (T[top].F == T[top - 1].F || T[top].F <= R - rest));
}
flag = 1;
while(m.rbegin() -> S == 0) {
auto p = m.end(); p--;
m.erase(p);
}
while(dm.rbegin() -> S == 0) {
auto p = dm.end(); p--;
dm.erase(p);
}
while(um.rbegin() -> S == 0) {
auto p = um.end(); p--;
um.erase(p);
}
ll rm = 0;
if(top - 1 >= 0)
rm = max(rm, T[top - 1].F);
// cout << lm << ' ' << rm << ' ' << m.rbegin() -> F << ' ' << dm.rbegin() -> F << ' ' << um.rbegin() -> F << '\n';
if(lm + rm >= minm && rm >= R - rest) {
ll tans = lm + rm + max(m.rbegin() -> F, dm.rbegin() -> F + um.rbegin() -> F);
ans = min(ans, tans);
}
}
}
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
716 KB |
Output is correct |
22 |
Correct |
8 ms |
1484 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
25 ms |
2288 KB |
Output is correct |
25 |
Correct |
12 ms |
1876 KB |
Output is correct |
26 |
Correct |
72 ms |
3276 KB |
Output is correct |
27 |
Correct |
73 ms |
3244 KB |
Output is correct |
28 |
Correct |
25 ms |
2284 KB |
Output is correct |
29 |
Correct |
73 ms |
3372 KB |
Output is correct |
30 |
Correct |
77 ms |
3260 KB |
Output is correct |
31 |
Correct |
81 ms |
3256 KB |
Output is correct |
32 |
Correct |
74 ms |
3316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
716 KB |
Output is correct |
22 |
Correct |
8 ms |
1484 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
25 ms |
2288 KB |
Output is correct |
25 |
Correct |
12 ms |
1876 KB |
Output is correct |
26 |
Correct |
72 ms |
3276 KB |
Output is correct |
27 |
Correct |
73 ms |
3244 KB |
Output is correct |
28 |
Correct |
25 ms |
2284 KB |
Output is correct |
29 |
Correct |
73 ms |
3372 KB |
Output is correct |
30 |
Correct |
77 ms |
3260 KB |
Output is correct |
31 |
Correct |
81 ms |
3256 KB |
Output is correct |
32 |
Correct |
74 ms |
3316 KB |
Output is correct |
33 |
Correct |
56 ms |
3232 KB |
Output is correct |
34 |
Correct |
50 ms |
3224 KB |
Output is correct |
35 |
Correct |
81 ms |
3396 KB |
Output is correct |
36 |
Correct |
89 ms |
3348 KB |
Output is correct |
37 |
Correct |
88 ms |
3420 KB |
Output is correct |
38 |
Correct |
88 ms |
3408 KB |
Output is correct |
39 |
Correct |
85 ms |
3396 KB |
Output is correct |
40 |
Correct |
87 ms |
3336 KB |
Output is correct |
41 |
Correct |
61 ms |
3208 KB |
Output is correct |
42 |
Correct |
71 ms |
3268 KB |
Output is correct |
43 |
Correct |
86 ms |
3336 KB |
Output is correct |
44 |
Correct |
83 ms |
3424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
460 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
1 ms |
460 KB |
Output is correct |
11 |
Correct |
1 ms |
588 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
1 ms |
588 KB |
Output is correct |
18 |
Correct |
1 ms |
460 KB |
Output is correct |
19 |
Correct |
1 ms |
460 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
14 ms |
1356 KB |
Output is correct |
22 |
Correct |
17 ms |
1400 KB |
Output is correct |
23 |
Correct |
12 ms |
1340 KB |
Output is correct |
24 |
Correct |
11 ms |
1332 KB |
Output is correct |
25 |
Correct |
16 ms |
1356 KB |
Output is correct |
26 |
Correct |
11 ms |
1356 KB |
Output is correct |
27 |
Correct |
10 ms |
1348 KB |
Output is correct |
28 |
Correct |
16 ms |
1356 KB |
Output is correct |
29 |
Correct |
16 ms |
1356 KB |
Output is correct |
30 |
Correct |
16 ms |
1356 KB |
Output is correct |
31 |
Correct |
17 ms |
1400 KB |
Output is correct |
32 |
Correct |
15 ms |
1396 KB |
Output is correct |
33 |
Correct |
11 ms |
1228 KB |
Output is correct |
34 |
Correct |
14 ms |
1356 KB |
Output is correct |
35 |
Correct |
14 ms |
1356 KB |
Output is correct |
36 |
Correct |
12 ms |
1228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
0 ms |
332 KB |
Output is correct |
15 |
Correct |
0 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
460 KB |
Output is correct |
18 |
Correct |
2 ms |
972 KB |
Output is correct |
19 |
Correct |
1 ms |
588 KB |
Output is correct |
20 |
Correct |
1 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
716 KB |
Output is correct |
22 |
Correct |
8 ms |
1484 KB |
Output is correct |
23 |
Correct |
1 ms |
588 KB |
Output is correct |
24 |
Correct |
25 ms |
2288 KB |
Output is correct |
25 |
Correct |
12 ms |
1876 KB |
Output is correct |
26 |
Correct |
72 ms |
3276 KB |
Output is correct |
27 |
Correct |
73 ms |
3244 KB |
Output is correct |
28 |
Correct |
25 ms |
2284 KB |
Output is correct |
29 |
Correct |
73 ms |
3372 KB |
Output is correct |
30 |
Correct |
77 ms |
3260 KB |
Output is correct |
31 |
Correct |
81 ms |
3256 KB |
Output is correct |
32 |
Correct |
74 ms |
3316 KB |
Output is correct |
33 |
Correct |
56 ms |
3232 KB |
Output is correct |
34 |
Correct |
50 ms |
3224 KB |
Output is correct |
35 |
Correct |
81 ms |
3396 KB |
Output is correct |
36 |
Correct |
89 ms |
3348 KB |
Output is correct |
37 |
Correct |
88 ms |
3420 KB |
Output is correct |
38 |
Correct |
88 ms |
3408 KB |
Output is correct |
39 |
Correct |
85 ms |
3396 KB |
Output is correct |
40 |
Correct |
87 ms |
3336 KB |
Output is correct |
41 |
Correct |
61 ms |
3208 KB |
Output is correct |
42 |
Correct |
71 ms |
3268 KB |
Output is correct |
43 |
Correct |
86 ms |
3336 KB |
Output is correct |
44 |
Correct |
83 ms |
3424 KB |
Output is correct |
45 |
Correct |
1 ms |
460 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
460 KB |
Output is correct |
48 |
Correct |
1 ms |
460 KB |
Output is correct |
49 |
Correct |
1 ms |
460 KB |
Output is correct |
50 |
Correct |
1 ms |
460 KB |
Output is correct |
51 |
Correct |
1 ms |
460 KB |
Output is correct |
52 |
Correct |
1 ms |
460 KB |
Output is correct |
53 |
Correct |
1 ms |
460 KB |
Output is correct |
54 |
Correct |
1 ms |
460 KB |
Output is correct |
55 |
Correct |
1 ms |
588 KB |
Output is correct |
56 |
Correct |
1 ms |
460 KB |
Output is correct |
57 |
Correct |
1 ms |
460 KB |
Output is correct |
58 |
Correct |
1 ms |
460 KB |
Output is correct |
59 |
Correct |
1 ms |
460 KB |
Output is correct |
60 |
Correct |
2 ms |
460 KB |
Output is correct |
61 |
Correct |
1 ms |
588 KB |
Output is correct |
62 |
Correct |
1 ms |
460 KB |
Output is correct |
63 |
Correct |
1 ms |
460 KB |
Output is correct |
64 |
Correct |
1 ms |
460 KB |
Output is correct |
65 |
Correct |
14 ms |
1356 KB |
Output is correct |
66 |
Correct |
17 ms |
1400 KB |
Output is correct |
67 |
Correct |
12 ms |
1340 KB |
Output is correct |
68 |
Correct |
11 ms |
1332 KB |
Output is correct |
69 |
Correct |
16 ms |
1356 KB |
Output is correct |
70 |
Correct |
11 ms |
1356 KB |
Output is correct |
71 |
Correct |
10 ms |
1348 KB |
Output is correct |
72 |
Correct |
16 ms |
1356 KB |
Output is correct |
73 |
Correct |
16 ms |
1356 KB |
Output is correct |
74 |
Correct |
16 ms |
1356 KB |
Output is correct |
75 |
Correct |
17 ms |
1400 KB |
Output is correct |
76 |
Correct |
15 ms |
1396 KB |
Output is correct |
77 |
Correct |
11 ms |
1228 KB |
Output is correct |
78 |
Correct |
14 ms |
1356 KB |
Output is correct |
79 |
Correct |
14 ms |
1356 KB |
Output is correct |
80 |
Correct |
12 ms |
1228 KB |
Output is correct |
81 |
Correct |
100 ms |
2680 KB |
Output is correct |
82 |
Correct |
97 ms |
2556 KB |
Output is correct |
83 |
Correct |
137 ms |
2988 KB |
Output is correct |
84 |
Correct |
71 ms |
2760 KB |
Output is correct |
85 |
Correct |
208 ms |
3576 KB |
Output is correct |
86 |
Correct |
210 ms |
3512 KB |
Output is correct |
87 |
Correct |
192 ms |
3572 KB |
Output is correct |
88 |
Correct |
157 ms |
3392 KB |
Output is correct |
89 |
Correct |
175 ms |
3400 KB |
Output is correct |
90 |
Correct |
109 ms |
3012 KB |
Output is correct |
91 |
Correct |
215 ms |
3396 KB |
Output is correct |
92 |
Correct |
222 ms |
3604 KB |
Output is correct |
93 |
Correct |
186 ms |
3356 KB |
Output is correct |
94 |
Correct |
165 ms |
3380 KB |
Output is correct |
95 |
Correct |
210 ms |
3736 KB |
Output is correct |
96 |
Correct |
216 ms |
3436 KB |
Output is correct |
97 |
Correct |
179 ms |
3544 KB |
Output is correct |
98 |
Correct |
65 ms |
3268 KB |
Output is correct |