#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 660
#define MAXS 20
#define INF 20000000000020
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
ll H, W;
int N;
pll arr[MAX];
ll ans = INF;
int st[MAX * 2], en[MAX * 2];
ll U[MAX], D[MAX], UD[MAX];
ll A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
typedef pair<ll, int> pli;
pli up[MAX * 2], down[MAX * 2], su[MAX * 2];
ll ys[MAX * 2];
bool test(ll d) {
if (d <= 0) return false;
int i, j;
int p1, p2;
p1 = p2 = 1;
int X = 0;
while (p1 <= N || p2 <= N) {
ll x;
if (p1 > N) x = arr[p2].second + d;
else if (p2 > N) x = arr[p1].second;
else x = min(arr[p1].second, arr[p2].second + d);
ys[X++] = x;
while (p1 <= N && arr[p1].second == x) p1++, st[X - 1]++;
while (p2 <= N && arr[p2].second + d == x) p2++, en[X - 1]++;
}
int il, ir;
il = ir = 0;
for (i = 0; i < X; i++) {
ir += st[i];
il += en[i];
if (il == ir) {
UD[i] = INF;
continue;
}
U[i] = A[il + 1][ir];
D[i] = B[il + 1][ir];
UD[i] = C[il + 1][ir];
}
int ul, dl, udl;
int ur, dr, udr;
ul = dl = udl = 0;
ur = dr = udr = 0;
ll mn = INF;
j = 0;
for (i = 0; i < X - 1; i++) {
while (ul != ur && up[ur].first <= U[i]) ur--;
up[++ur] = pli(U[i], i);
while (dl != dr && down[dr].first <= D[i]) dr--;
down[++dr] = pli(D[i], i);
while (udl != udr && su[udr].first <= UD[i]) udr--;
su[++udr] = pli(UD[i], i);
while (j + 1 <= i && ys[i + 1] - ys[j + 1] >= W) j++;
while (ul != ur && up[ul + 1].second < j) ul++;
while (dl != dr && down[dl + 1].second < j) dl++;
while (udl != udr && su[udl + 1].second < j) udl++;
if (ys[i + 1] - ys[j] >= W) mn = min(mn, max(0ll + up[ul + 1].first + down[dl + 1].first, (ll)su[udl + 1].first));
}
for (i = 0; i < X; i++) U[i] = D[i] = UD[i] = 0, st[i] = en[i] = 0;
if (mn >= INF) return false;
ans = min(ans, mn + d - 1);
return true;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> H >> W >> N;
int i, j, k;
vector<ll> ls;
ls.push_back(W);
for (i = 1; i <= N; i++) cin >> arr[i].first >> arr[i].second;
for (i = 1; i <= N; i++) {
for (j = i + 1; j <= N; j++) ls.push_back(W - abs(arr[j].second - arr[i].second));
for (j = i + 1; j <= N; j++) ls.push_back(abs(arr[j].second - arr[i].second));
for (j = i + 1; j <= N; j++) ls.push_back(abs(arr[j].second - arr[i].second) + W);
ls.push_back(arr[i].second);
ls.push_back(W - arr[i].second + 1);
}
sort(arr + 1, arr + N + 1, [&](pii p1, pii p2) {return p1.second < p2.second; });
for (i = 1; i <= N; i++) {
vector<ll> v;
A[i][i] = arr[i].first - 1;
B[i][i] = H - arr[i].first;
v.push_back(arr[i].first);
for (j = i + 1; j <= N; j++) {
v.insert(lower_bound(v.begin(), v.end(), arr[j].first), arr[j].first);
for (k = 0; k + 1 < v.size(); k++) C[i][j] = max(C[i][j], v[k + 1] - v[k] - 1);
A[i][j] = v[0] - 1;
B[i][j] = H - v.back();
}
}
sort(ls.begin(), ls.end());
ls.erase(unique(ls.begin(), ls.end()), ls.end());
reverse(ls.begin(), ls.end());
for (auto v : ls) if (!test(v)) break;
cout << ans << ln;
}
Compilation message
cultivation.cpp: In function 'int main()':
cultivation.cpp:102:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for (k = 0; k + 1 < v.size(); k++) C[i][j] = max(C[i][j], v[k + 1] - v[k] - 1);
| ~~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
400 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
400 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
3 ms |
2264 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
3796 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
14 ms |
5860 KB |
Output is correct |
27 |
Correct |
16 ms |
5948 KB |
Output is correct |
28 |
Correct |
6 ms |
3796 KB |
Output is correct |
29 |
Correct |
14 ms |
5876 KB |
Output is correct |
30 |
Correct |
14 ms |
5964 KB |
Output is correct |
31 |
Correct |
14 ms |
5916 KB |
Output is correct |
32 |
Correct |
15 ms |
5944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
400 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
3 ms |
2264 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
3796 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
14 ms |
5860 KB |
Output is correct |
27 |
Correct |
16 ms |
5948 KB |
Output is correct |
28 |
Correct |
6 ms |
3796 KB |
Output is correct |
29 |
Correct |
14 ms |
5876 KB |
Output is correct |
30 |
Correct |
14 ms |
5964 KB |
Output is correct |
31 |
Correct |
14 ms |
5916 KB |
Output is correct |
32 |
Correct |
15 ms |
5944 KB |
Output is correct |
33 |
Correct |
1431 ms |
5964 KB |
Output is correct |
34 |
Correct |
1425 ms |
5964 KB |
Output is correct |
35 |
Correct |
34 ms |
5964 KB |
Output is correct |
36 |
Correct |
1561 ms |
5952 KB |
Output is correct |
37 |
Correct |
1590 ms |
5964 KB |
Output is correct |
38 |
Correct |
1594 ms |
5904 KB |
Output is correct |
39 |
Correct |
1134 ms |
5956 KB |
Output is correct |
40 |
Correct |
1558 ms |
5956 KB |
Output is correct |
41 |
Correct |
1064 ms |
5988 KB |
Output is correct |
42 |
Correct |
63 ms |
5964 KB |
Output is correct |
43 |
Correct |
1607 ms |
5952 KB |
Output is correct |
44 |
Correct |
245 ms |
5964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
480 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
480 KB |
Output is correct |
2 |
Correct |
1 ms |
596 KB |
Output is correct |
3 |
Correct |
1 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
596 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
596 KB |
Output is correct |
8 |
Correct |
1 ms |
596 KB |
Output is correct |
9 |
Correct |
1 ms |
692 KB |
Output is correct |
10 |
Correct |
2 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
596 KB |
Output is correct |
12 |
Correct |
2 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
596 KB |
Output is correct |
14 |
Correct |
1 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
1 ms |
596 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
46 ms |
1876 KB |
Output is correct |
22 |
Correct |
64 ms |
1928 KB |
Output is correct |
23 |
Correct |
42 ms |
1928 KB |
Output is correct |
24 |
Correct |
43 ms |
1924 KB |
Output is correct |
25 |
Correct |
64 ms |
1876 KB |
Output is correct |
26 |
Correct |
15 ms |
1876 KB |
Output is correct |
27 |
Correct |
71 ms |
1876 KB |
Output is correct |
28 |
Correct |
69 ms |
1928 KB |
Output is correct |
29 |
Correct |
62 ms |
1876 KB |
Output is correct |
30 |
Correct |
64 ms |
1932 KB |
Output is correct |
31 |
Correct |
68 ms |
1940 KB |
Output is correct |
32 |
Correct |
60 ms |
1876 KB |
Output is correct |
33 |
Correct |
67 ms |
1924 KB |
Output is correct |
34 |
Correct |
48 ms |
1936 KB |
Output is correct |
35 |
Correct |
54 ms |
1912 KB |
Output is correct |
36 |
Correct |
63 ms |
1876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
320 KB |
Output is correct |
5 |
Correct |
0 ms |
400 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
304 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
1236 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
980 KB |
Output is correct |
22 |
Correct |
3 ms |
2264 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
6 ms |
3796 KB |
Output is correct |
25 |
Correct |
4 ms |
2772 KB |
Output is correct |
26 |
Correct |
14 ms |
5860 KB |
Output is correct |
27 |
Correct |
16 ms |
5948 KB |
Output is correct |
28 |
Correct |
6 ms |
3796 KB |
Output is correct |
29 |
Correct |
14 ms |
5876 KB |
Output is correct |
30 |
Correct |
14 ms |
5964 KB |
Output is correct |
31 |
Correct |
14 ms |
5916 KB |
Output is correct |
32 |
Correct |
15 ms |
5944 KB |
Output is correct |
33 |
Correct |
1431 ms |
5964 KB |
Output is correct |
34 |
Correct |
1425 ms |
5964 KB |
Output is correct |
35 |
Correct |
34 ms |
5964 KB |
Output is correct |
36 |
Correct |
1561 ms |
5952 KB |
Output is correct |
37 |
Correct |
1590 ms |
5964 KB |
Output is correct |
38 |
Correct |
1594 ms |
5904 KB |
Output is correct |
39 |
Correct |
1134 ms |
5956 KB |
Output is correct |
40 |
Correct |
1558 ms |
5956 KB |
Output is correct |
41 |
Correct |
1064 ms |
5988 KB |
Output is correct |
42 |
Correct |
63 ms |
5964 KB |
Output is correct |
43 |
Correct |
1607 ms |
5952 KB |
Output is correct |
44 |
Correct |
245 ms |
5964 KB |
Output is correct |
45 |
Correct |
1 ms |
480 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
1 ms |
596 KB |
Output is correct |
48 |
Correct |
2 ms |
596 KB |
Output is correct |
49 |
Correct |
2 ms |
596 KB |
Output is correct |
50 |
Correct |
1 ms |
596 KB |
Output is correct |
51 |
Correct |
1 ms |
596 KB |
Output is correct |
52 |
Correct |
1 ms |
596 KB |
Output is correct |
53 |
Correct |
1 ms |
692 KB |
Output is correct |
54 |
Correct |
2 ms |
596 KB |
Output is correct |
55 |
Correct |
1 ms |
596 KB |
Output is correct |
56 |
Correct |
2 ms |
596 KB |
Output is correct |
57 |
Correct |
1 ms |
596 KB |
Output is correct |
58 |
Correct |
1 ms |
596 KB |
Output is correct |
59 |
Correct |
1 ms |
596 KB |
Output is correct |
60 |
Correct |
1 ms |
596 KB |
Output is correct |
61 |
Correct |
2 ms |
596 KB |
Output is correct |
62 |
Correct |
2 ms |
596 KB |
Output is correct |
63 |
Correct |
2 ms |
596 KB |
Output is correct |
64 |
Correct |
1 ms |
596 KB |
Output is correct |
65 |
Correct |
46 ms |
1876 KB |
Output is correct |
66 |
Correct |
64 ms |
1928 KB |
Output is correct |
67 |
Correct |
42 ms |
1928 KB |
Output is correct |
68 |
Correct |
43 ms |
1924 KB |
Output is correct |
69 |
Correct |
64 ms |
1876 KB |
Output is correct |
70 |
Correct |
15 ms |
1876 KB |
Output is correct |
71 |
Correct |
71 ms |
1876 KB |
Output is correct |
72 |
Correct |
69 ms |
1928 KB |
Output is correct |
73 |
Correct |
62 ms |
1876 KB |
Output is correct |
74 |
Correct |
64 ms |
1932 KB |
Output is correct |
75 |
Correct |
68 ms |
1940 KB |
Output is correct |
76 |
Correct |
60 ms |
1876 KB |
Output is correct |
77 |
Correct |
67 ms |
1924 KB |
Output is correct |
78 |
Correct |
48 ms |
1936 KB |
Output is correct |
79 |
Correct |
54 ms |
1912 KB |
Output is correct |
80 |
Correct |
63 ms |
1876 KB |
Output is correct |
81 |
Correct |
672 ms |
4168 KB |
Output is correct |
82 |
Correct |
679 ms |
4176 KB |
Output is correct |
83 |
Correct |
880 ms |
4816 KB |
Output is correct |
84 |
Correct |
997 ms |
4876 KB |
Output is correct |
85 |
Correct |
1665 ms |
5932 KB |
Output is correct |
86 |
Correct |
1629 ms |
5972 KB |
Output is correct |
87 |
Correct |
1110 ms |
5956 KB |
Output is correct |
88 |
Correct |
1092 ms |
5948 KB |
Output is correct |
89 |
Correct |
1150 ms |
5960 KB |
Output is correct |
90 |
Correct |
1103 ms |
5304 KB |
Output is correct |
91 |
Correct |
1371 ms |
5944 KB |
Output is correct |
92 |
Correct |
1750 ms |
5944 KB |
Output is correct |
93 |
Correct |
1117 ms |
5936 KB |
Output is correct |
94 |
Correct |
1137 ms |
5948 KB |
Output is correct |
95 |
Correct |
1794 ms |
5968 KB |
Output is correct |
96 |
Correct |
1484 ms |
5996 KB |
Output is correct |
97 |
Correct |
829 ms |
5960 KB |
Output is correct |
98 |
Correct |
87 ms |
5960 KB |
Output is correct |