#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
#define x first
#define y second
ll H, W, N, F[3][305][305];
pair<ll, ll> A[305];
struct Data{
ll l, r, c[3];
};
ll Add[605], Del[605];
ll Solve(ll len){
for(ll i = 0; i <= 2 * N; i++){
Add[i] = Del[i] = 0;
}
vector<ll> Num;
A[N + 1].x = 1e18;
for(ll i = 1, j = 1; j <= N;){
if(A[i].x < A[j].x + len + 1){
Num.push_back(A[i].x);
i++;
} else {
Num.push_back(A[j].x + len + 1);
j++;
}
}
Num.erase(unique(Num.begin(), Num.end()), Num.end());
vector<Data> V(Num.size());
for(ll i = 1, p = 0, q = 0; i <= N; i++){
while(p + 1 < Num.size() && !(Num[p] <= A[i].x && A[i].x < Num[p + 1])) p++;
while(q + 1 < Num.size() && !(Num[q] <= A[i].x + len && A[i].x + len < Num[q + 1])) q++;
Add[p]++; Del[q]++;
}
ll s = 1, e = 0;
for(ll i = 0; i < V.size(); i++){
e += Add[i];
V[i].l = Num[i], V[i].r = (i + 1 == V.size() ? INF : Num[i + 1] - 1);
for(ll j = 0; j < 3; j++) V[i].c[j] = F[j][s][e];
s += Del[i];
}
deque<ll> dq[3];
ll Res = 1e18;
for(ll i = 0, j = 0; i < V.size(); i++){
if(V[i].l + H - 1 >= V.back().l) break;
for(ll k = 0; k < 3; k++){
while(!dq[k].empty() && dq[k].front() < i) dq[k].pop_front();
}
while(j < V.size() && V[j].l <= V[i].l + H - 1){
for(ll k = 0; k < 3; k++){
while(!dq[k].empty() && V[dq[k].back()].c[k] <= V[j].c[k]) dq[k].pop_back();
dq[k].push_back(j);
}
j++;
}
Res = min(Res, max(V[dq[0].front()].c[0] + V[dq[1].front()].c[1], V[dq[2].front()].c[2]));
}
return Res;
}
int main(){
cin.tie(0) -> sync_with_stdio(0);
cin >> H >> W >> N;
vector<ll> Cand;
for(ll i = 1; i <= N; i++){
cin >> A[i].x >> A[i].y;
Cand.push_back(A[i].x - 1);
Cand.push_back(H - A[i].x);
}
sort(A + 1, A + N + 1);
for(ll i = 1; i <= N; i++){
set<ll> S;
for(ll j = i; j <= N; j++){
S.insert(A[j].y);
F[0][i][j] = *S.begin() - 1, F[1][i][j] = W - *prev(S.end());
for(auto it = S.begin(); ; it++){
if(next(it) == S.end()) break;
F[2][i][j] = max(F[2][i][j], *next(it) - *it - 1);
}
}
}
ll mn = H - A[N].x + A[1].x - 1;
for(ll i = 2; i <= N; i++){
mn = max(mn, A[i].x - A[i - 1].x - 1);
}
ll Ans = 1e18;
for(ll i = 1; i <= N; i++){
for(ll j = 1; j <= N; j++){
Cand.push_back(A[j].x - A[i].x - 1);
Cand.push_back(A[j].x - 1 + H - A[i].x);
}
}
sort(Cand.begin(), Cand.end());
Cand.erase(unique(Cand.begin(), Cand.end()), Cand.end());
for(auto i : Cand){
if(i < mn) continue;
Ans = min(Ans, i + Solve(i));
}
cout << Ans;
}
Compilation message
cultivation.cpp: In function 'll Solve(ll)':
cultivation.cpp:35:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | while(p + 1 < Num.size() && !(Num[p] <= A[i].x && A[i].x < Num[p + 1])) p++;
| ~~~~~~^~~~~~~~~~~~
cultivation.cpp:36:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | while(q + 1 < Num.size() && !(Num[q] <= A[i].x + len && A[i].x + len < Num[q + 1])) q++;
| ~~~~~~^~~~~~~~~~~~
cultivation.cpp:40:21: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for(ll i = 0; i < V.size(); i++){
| ~~^~~~~~~~~~
cultivation.cpp:42:42: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
42 | V[i].l = Num[i], V[i].r = (i + 1 == V.size() ? INF : Num[i + 1] - 1);
| ~~~~~~^~~~~~~~~~~
cultivation.cpp:48:28: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
48 | for(ll i = 0, j = 0; i < V.size(); i++){
| ~~^~~~~~~~~~
cultivation.cpp:53:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<Data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | while(j < V.size() && V[j].l <= V[i].l + H - 1){
| ~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
9 ms |
3036 KB |
Output is correct |
25 |
Correct |
5 ms |
2012 KB |
Output is correct |
26 |
Correct |
18 ms |
4800 KB |
Output is correct |
27 |
Correct |
19 ms |
4824 KB |
Output is correct |
28 |
Correct |
9 ms |
3036 KB |
Output is correct |
29 |
Correct |
20 ms |
4812 KB |
Output is correct |
30 |
Correct |
19 ms |
4824 KB |
Output is correct |
31 |
Correct |
19 ms |
4824 KB |
Output is correct |
32 |
Correct |
29 ms |
4824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
9 ms |
3036 KB |
Output is correct |
25 |
Correct |
5 ms |
2012 KB |
Output is correct |
26 |
Correct |
18 ms |
4800 KB |
Output is correct |
27 |
Correct |
19 ms |
4824 KB |
Output is correct |
28 |
Correct |
9 ms |
3036 KB |
Output is correct |
29 |
Correct |
20 ms |
4812 KB |
Output is correct |
30 |
Correct |
19 ms |
4824 KB |
Output is correct |
31 |
Correct |
19 ms |
4824 KB |
Output is correct |
32 |
Correct |
29 ms |
4824 KB |
Output is correct |
33 |
Correct |
44 ms |
4824 KB |
Output is correct |
34 |
Correct |
45 ms |
4824 KB |
Output is correct |
35 |
Correct |
46 ms |
4824 KB |
Output is correct |
36 |
Correct |
47 ms |
4828 KB |
Output is correct |
37 |
Correct |
47 ms |
4828 KB |
Output is correct |
38 |
Correct |
46 ms |
4828 KB |
Output is correct |
39 |
Correct |
49 ms |
4828 KB |
Output is correct |
40 |
Correct |
49 ms |
4828 KB |
Output is correct |
41 |
Correct |
49 ms |
4832 KB |
Output is correct |
42 |
Correct |
39 ms |
4824 KB |
Output is correct |
43 |
Correct |
45 ms |
4620 KB |
Output is correct |
44 |
Correct |
37 ms |
4572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
520 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
2 ms |
600 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
2 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
520 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
42 ms |
1372 KB |
Output is correct |
22 |
Correct |
68 ms |
1624 KB |
Output is correct |
23 |
Correct |
47 ms |
1504 KB |
Output is correct |
24 |
Correct |
45 ms |
1504 KB |
Output is correct |
25 |
Correct |
67 ms |
1500 KB |
Output is correct |
26 |
Correct |
25 ms |
1504 KB |
Output is correct |
27 |
Correct |
72 ms |
1372 KB |
Output is correct |
28 |
Correct |
68 ms |
1504 KB |
Output is correct |
29 |
Correct |
57 ms |
1504 KB |
Output is correct |
30 |
Correct |
67 ms |
1488 KB |
Output is correct |
31 |
Correct |
68 ms |
1500 KB |
Output is correct |
32 |
Correct |
66 ms |
1504 KB |
Output is correct |
33 |
Correct |
54 ms |
1504 KB |
Output is correct |
34 |
Correct |
66 ms |
1504 KB |
Output is correct |
35 |
Correct |
67 ms |
1504 KB |
Output is correct |
36 |
Correct |
48 ms |
1504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
604 KB |
Output is correct |
18 |
Correct |
1 ms |
1132 KB |
Output is correct |
19 |
Correct |
1 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
3 ms |
1628 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
9 ms |
3036 KB |
Output is correct |
25 |
Correct |
5 ms |
2012 KB |
Output is correct |
26 |
Correct |
18 ms |
4800 KB |
Output is correct |
27 |
Correct |
19 ms |
4824 KB |
Output is correct |
28 |
Correct |
9 ms |
3036 KB |
Output is correct |
29 |
Correct |
20 ms |
4812 KB |
Output is correct |
30 |
Correct |
19 ms |
4824 KB |
Output is correct |
31 |
Correct |
19 ms |
4824 KB |
Output is correct |
32 |
Correct |
29 ms |
4824 KB |
Output is correct |
33 |
Correct |
44 ms |
4824 KB |
Output is correct |
34 |
Correct |
45 ms |
4824 KB |
Output is correct |
35 |
Correct |
46 ms |
4824 KB |
Output is correct |
36 |
Correct |
47 ms |
4828 KB |
Output is correct |
37 |
Correct |
47 ms |
4828 KB |
Output is correct |
38 |
Correct |
46 ms |
4828 KB |
Output is correct |
39 |
Correct |
49 ms |
4828 KB |
Output is correct |
40 |
Correct |
49 ms |
4828 KB |
Output is correct |
41 |
Correct |
49 ms |
4832 KB |
Output is correct |
42 |
Correct |
39 ms |
4824 KB |
Output is correct |
43 |
Correct |
45 ms |
4620 KB |
Output is correct |
44 |
Correct |
37 ms |
4572 KB |
Output is correct |
45 |
Correct |
1 ms |
600 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
2 ms |
604 KB |
Output is correct |
49 |
Correct |
2 ms |
600 KB |
Output is correct |
50 |
Correct |
1 ms |
600 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
1 ms |
604 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
2 ms |
604 KB |
Output is correct |
56 |
Correct |
2 ms |
604 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
2 ms |
600 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
2 ms |
604 KB |
Output is correct |
61 |
Correct |
2 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
2 ms |
520 KB |
Output is correct |
64 |
Correct |
2 ms |
604 KB |
Output is correct |
65 |
Correct |
42 ms |
1372 KB |
Output is correct |
66 |
Correct |
68 ms |
1624 KB |
Output is correct |
67 |
Correct |
47 ms |
1504 KB |
Output is correct |
68 |
Correct |
45 ms |
1504 KB |
Output is correct |
69 |
Correct |
67 ms |
1500 KB |
Output is correct |
70 |
Correct |
25 ms |
1504 KB |
Output is correct |
71 |
Correct |
72 ms |
1372 KB |
Output is correct |
72 |
Correct |
68 ms |
1504 KB |
Output is correct |
73 |
Correct |
57 ms |
1504 KB |
Output is correct |
74 |
Correct |
67 ms |
1488 KB |
Output is correct |
75 |
Correct |
68 ms |
1500 KB |
Output is correct |
76 |
Correct |
66 ms |
1504 KB |
Output is correct |
77 |
Correct |
54 ms |
1504 KB |
Output is correct |
78 |
Correct |
66 ms |
1504 KB |
Output is correct |
79 |
Correct |
67 ms |
1504 KB |
Output is correct |
80 |
Correct |
48 ms |
1504 KB |
Output is correct |
81 |
Correct |
713 ms |
3036 KB |
Output is correct |
82 |
Correct |
722 ms |
3040 KB |
Output is correct |
83 |
Correct |
833 ms |
3296 KB |
Output is correct |
84 |
Correct |
1136 ms |
3240 KB |
Output is correct |
85 |
Correct |
1873 ms |
4828 KB |
Output is correct |
86 |
Correct |
1834 ms |
4824 KB |
Output is correct |
87 |
Correct |
1291 ms |
4816 KB |
Output is correct |
88 |
Correct |
1385 ms |
4836 KB |
Output is correct |
89 |
Correct |
1444 ms |
4736 KB |
Output is correct |
90 |
Correct |
965 ms |
4508 KB |
Output is correct |
91 |
Correct |
1819 ms |
4828 KB |
Output is correct |
92 |
Correct |
1890 ms |
4824 KB |
Output is correct |
93 |
Correct |
1379 ms |
4828 KB |
Output is correct |
94 |
Correct |
1898 ms |
4824 KB |
Output is correct |
95 |
Correct |
1876 ms |
4724 KB |
Output is correct |
96 |
Correct |
1752 ms |
4836 KB |
Output is correct |
97 |
Correct |
1863 ms |
4828 KB |
Output is correct |
98 |
Correct |
43 ms |
4824 KB |
Output is correct |