#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll inf = 2e18;
ll R, C;
ll N;
ll mxl[310][310], mxr[310][310], mxsum[310][310];
ll solve(vector<pll> p, ll H) {
vector<pll> upd;
ll j = 0;
for (ll i=0; i<p.size(); i++) {
while (j < p.size() && p[j].first + H <= p[i].first) {
upd.push_back({p[j].first + H, -p[j].second});
j ++;
}
upd.push_back(p[i]);
}
while (j < p.size()) {
upd.push_back({p[j].first + H, -p[j].second});
j ++;
}
vector<array<ll,4>> save;
ll idx1 = 0, idx2 = -1;
for (ll i=0; i<upd.size(); i++) {
if (upd[i].second > 0) idx2 ++;
if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
while (idx1 < N && p[idx1].first + H <= upd[i].first) idx1 ++;
if (idx1 > idx2) {
save.push_back({upd[i].first, inf, inf, inf});
continue;
}
save.push_back({upd[i].first, mxsum[idx1][idx2], mxl[idx1][idx2], mxr[idx1][idx2]});
}
ll ans = inf;
deque<ll> dq[3];
for (ll i=(ll)save.size()-1; i>=0; i--) {
for (ll j=0; j<3; j++) {
while (!dq[j].empty() && save[dq[j].back()][j+1] <= save[i][j+1]) {
dq[j].pop_back();
}
while (!dq[j].empty() && save[dq[j].front()][0] > save[i][0] + R - 1) {
dq[j].pop_front();
}
dq[j].push_back(i);
}
ans = min(ans, max(save[dq[0].front()][1], save[dq[1].front()][2] + save[dq[2].front()][3]));
}
return ans;
}
int main() {
ios_base :: sync_with_stdio(false); cin.tie(NULL);
cin >> R >> C;
cin >> N;
vector<pll> p(N);
for (ll i=0; i<N; i++) {
cin >> p[i].first >> p[i].second;
}
sort(p.begin(), p.end());
for (ll i=0; i<N; i++) {
multiset<ll> s1, s2;
for (ll j=i; j<N; j++) {
if (!s1.empty()) {
auto rit = s1.lower_bound(p[j].second);
if (rit != s1.begin()) {
auto lit = prev(rit);
if (rit != s1.end()) {
s2.erase(s2.find(*rit - *lit));
}
s2.insert(p[j].second - *lit);
}
if (rit != s1.end()) {
s2.insert(*rit - p[j].second);
}
}
s1.insert(p[j].second);
mxl[i][j] = *s1.begin() - 1, mxr[i][j] = C - *s1.rbegin();
mxsum[i][j] = mxl[i][j] + mxr[i][j];
if (!s2.empty()) mxsum[i][j] = *s2.rbegin() - 1;
}
}
vector<ll> cand, U, D;
for (ll i=0; i<N; i++) {
U.push_back(p[i].first - 1);
D.push_back(R - p[i].first);
for (ll j=i+1; j<N; j++) {
cand.push_back(abs(p[i].first - p[j].first));
}
}
for (auto &u : U) {
for (auto &d : D) {
cand.push_back(u + d + 1);
}
}
sort(cand.begin(), cand.end());
cand.erase(unique(cand.begin(), cand.end()), cand.end());
ll ans = inf;
for (auto &H : cand) {
if (H == 0) continue;
ans = min(ans, H - 1 + solve(p, H));
}
cout << ans << '\n';
}
Compilation message
cultivation.cpp: In function 'll solve(std::vector<std::pair<long long int, long long int> >, ll)':
cultivation.cpp:18:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for (ll i=0; i<p.size(); i++) {
| ~^~~~~~~~~
cultivation.cpp:19:12: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
19 | while (j < p.size() && p[j].first + H <= p[i].first) {
| ~~^~~~~~~~~~
cultivation.cpp:25:11: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | while (j < p.size()) {
| ~~^~~~~~~~~~
cultivation.cpp:31:16: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | for (ll i=0; i<upd.size(); i++) {
| ~^~~~~~~~~~~
cultivation.cpp:33:13: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
| ~~~~~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2508 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2508 KB |
Output is correct |
13 |
Correct |
0 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2508 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2508 KB |
Output is correct |
13 |
Correct |
0 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2464 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
8 ms |
3300 KB |
Output is correct |
25 |
Correct |
5 ms |
3300 KB |
Output is correct |
26 |
Correct |
16 ms |
4816 KB |
Output is correct |
27 |
Correct |
14 ms |
4824 KB |
Output is correct |
28 |
Correct |
7 ms |
3300 KB |
Output is correct |
29 |
Correct |
14 ms |
4852 KB |
Output is correct |
30 |
Correct |
21 ms |
4852 KB |
Output is correct |
31 |
Correct |
16 ms |
4868 KB |
Output is correct |
32 |
Correct |
21 ms |
4808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2508 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2508 KB |
Output is correct |
13 |
Correct |
0 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2464 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
8 ms |
3300 KB |
Output is correct |
25 |
Correct |
5 ms |
3300 KB |
Output is correct |
26 |
Correct |
16 ms |
4816 KB |
Output is correct |
27 |
Correct |
14 ms |
4824 KB |
Output is correct |
28 |
Correct |
7 ms |
3300 KB |
Output is correct |
29 |
Correct |
14 ms |
4852 KB |
Output is correct |
30 |
Correct |
21 ms |
4852 KB |
Output is correct |
31 |
Correct |
16 ms |
4868 KB |
Output is correct |
32 |
Correct |
21 ms |
4808 KB |
Output is correct |
33 |
Correct |
12 ms |
4816 KB |
Output is correct |
34 |
Correct |
11 ms |
4880 KB |
Output is correct |
35 |
Correct |
17 ms |
4812 KB |
Output is correct |
36 |
Correct |
17 ms |
4816 KB |
Output is correct |
37 |
Correct |
18 ms |
4816 KB |
Output is correct |
38 |
Correct |
21 ms |
4816 KB |
Output is correct |
39 |
Correct |
16 ms |
4752 KB |
Output is correct |
40 |
Correct |
16 ms |
4820 KB |
Output is correct |
41 |
Correct |
12 ms |
4824 KB |
Output is correct |
42 |
Correct |
20 ms |
4812 KB |
Output is correct |
43 |
Correct |
17 ms |
4816 KB |
Output is correct |
44 |
Correct |
15 ms |
4816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2508 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2528 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
4 ms |
2392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
2 ms |
2396 KB |
Output is correct |
3 |
Correct |
2 ms |
2396 KB |
Output is correct |
4 |
Correct |
2 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2508 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
2 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
2 ms |
2392 KB |
Output is correct |
11 |
Correct |
2 ms |
2396 KB |
Output is correct |
12 |
Correct |
2 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2528 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
2 ms |
2396 KB |
Output is correct |
17 |
Correct |
2 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2516 KB |
Output is correct |
20 |
Correct |
4 ms |
2392 KB |
Output is correct |
21 |
Correct |
62 ms |
2908 KB |
Output is correct |
22 |
Correct |
94 ms |
2904 KB |
Output is correct |
23 |
Correct |
114 ms |
3160 KB |
Output is correct |
24 |
Correct |
106 ms |
2904 KB |
Output is correct |
25 |
Correct |
107 ms |
2908 KB |
Output is correct |
26 |
Correct |
34 ms |
2904 KB |
Output is correct |
27 |
Correct |
102 ms |
2908 KB |
Output is correct |
28 |
Correct |
105 ms |
2904 KB |
Output is correct |
29 |
Correct |
104 ms |
2916 KB |
Output is correct |
30 |
Correct |
112 ms |
2904 KB |
Output is correct |
31 |
Correct |
101 ms |
2908 KB |
Output is correct |
32 |
Correct |
89 ms |
2908 KB |
Output is correct |
33 |
Correct |
111 ms |
2912 KB |
Output is correct |
34 |
Correct |
107 ms |
3160 KB |
Output is correct |
35 |
Correct |
114 ms |
2908 KB |
Output is correct |
36 |
Correct |
94 ms |
2904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2508 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2508 KB |
Output is correct |
13 |
Correct |
0 ms |
2392 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
0 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2464 KB |
Output is correct |
21 |
Correct |
1 ms |
2652 KB |
Output is correct |
22 |
Correct |
3 ms |
2908 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
8 ms |
3300 KB |
Output is correct |
25 |
Correct |
5 ms |
3300 KB |
Output is correct |
26 |
Correct |
16 ms |
4816 KB |
Output is correct |
27 |
Correct |
14 ms |
4824 KB |
Output is correct |
28 |
Correct |
7 ms |
3300 KB |
Output is correct |
29 |
Correct |
14 ms |
4852 KB |
Output is correct |
30 |
Correct |
21 ms |
4852 KB |
Output is correct |
31 |
Correct |
16 ms |
4868 KB |
Output is correct |
32 |
Correct |
21 ms |
4808 KB |
Output is correct |
33 |
Correct |
12 ms |
4816 KB |
Output is correct |
34 |
Correct |
11 ms |
4880 KB |
Output is correct |
35 |
Correct |
17 ms |
4812 KB |
Output is correct |
36 |
Correct |
17 ms |
4816 KB |
Output is correct |
37 |
Correct |
18 ms |
4816 KB |
Output is correct |
38 |
Correct |
21 ms |
4816 KB |
Output is correct |
39 |
Correct |
16 ms |
4752 KB |
Output is correct |
40 |
Correct |
16 ms |
4820 KB |
Output is correct |
41 |
Correct |
12 ms |
4824 KB |
Output is correct |
42 |
Correct |
20 ms |
4812 KB |
Output is correct |
43 |
Correct |
17 ms |
4816 KB |
Output is correct |
44 |
Correct |
15 ms |
4816 KB |
Output is correct |
45 |
Correct |
1 ms |
2392 KB |
Output is correct |
46 |
Correct |
2 ms |
2396 KB |
Output is correct |
47 |
Correct |
2 ms |
2396 KB |
Output is correct |
48 |
Correct |
2 ms |
2396 KB |
Output is correct |
49 |
Correct |
2 ms |
2508 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
2 ms |
2396 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
2 ms |
2392 KB |
Output is correct |
55 |
Correct |
2 ms |
2396 KB |
Output is correct |
56 |
Correct |
2 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2528 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
2 ms |
2396 KB |
Output is correct |
61 |
Correct |
2 ms |
2396 KB |
Output is correct |
62 |
Correct |
1 ms |
2396 KB |
Output is correct |
63 |
Correct |
2 ms |
2516 KB |
Output is correct |
64 |
Correct |
4 ms |
2392 KB |
Output is correct |
65 |
Correct |
62 ms |
2908 KB |
Output is correct |
66 |
Correct |
94 ms |
2904 KB |
Output is correct |
67 |
Correct |
114 ms |
3160 KB |
Output is correct |
68 |
Correct |
106 ms |
2904 KB |
Output is correct |
69 |
Correct |
107 ms |
2908 KB |
Output is correct |
70 |
Correct |
34 ms |
2904 KB |
Output is correct |
71 |
Correct |
102 ms |
2908 KB |
Output is correct |
72 |
Correct |
105 ms |
2904 KB |
Output is correct |
73 |
Correct |
104 ms |
2916 KB |
Output is correct |
74 |
Correct |
112 ms |
2904 KB |
Output is correct |
75 |
Correct |
101 ms |
2908 KB |
Output is correct |
76 |
Correct |
89 ms |
2908 KB |
Output is correct |
77 |
Correct |
111 ms |
2912 KB |
Output is correct |
78 |
Correct |
107 ms |
3160 KB |
Output is correct |
79 |
Correct |
114 ms |
2908 KB |
Output is correct |
80 |
Correct |
94 ms |
2904 KB |
Output is correct |
81 |
Correct |
1246 ms |
3808 KB |
Output is correct |
82 |
Correct |
1223 ms |
3804 KB |
Output is correct |
83 |
Correct |
1462 ms |
3812 KB |
Output is correct |
84 |
Correct |
1950 ms |
3808 KB |
Output is correct |
85 |
Execution timed out |
2045 ms |
4684 KB |
Time limit exceeded |
86 |
Halted |
0 ms |
0 KB |
- |