#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 solve(vector<pll> p, ll H1, ll H2) {
vector<pll> upd;
for (ll i=0; i<p.size(); i++) {
upd.push_back({p[i].first - H1, p[i].second});
upd.push_back({p[i].first + H2 + 1, -p[i].second});
}
sort(upd.begin(), upd.end());
vector<ll> cur;
vector<array<ll,4>> save;
for (ll i=0; i<upd.size(); i++) {
if (upd[i].second > 0) {
cur.push_back(upd[i].second);
sort(cur.begin(), cur.end());
}
else {
bool flag = false;
for (ll j=0; j<cur.size(); j++) {
if (cur[j] == -upd[i].second) {
cur.erase(cur.begin() + j);
flag = true;
break;
}
}
assert(flag);
}
if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
if (cur.empty()) {
save.push_back({upd[i].first, inf, inf, inf});
continue;
}
ll l = cur[0] - 1, r = C - cur.back();
ll sum = l + r;
for (ll j=0; j+1<cur.size(); j++) {
sum = max(sum, cur[j+1] - cur[j] - 1);
}
save.push_back({upd[i].first, sum, l, r});
}
ll ans = inf;
for (ll i=0; i<save.size(); i++) {
ll l = save[i][2], r = save[i][3];
ll sum = save[i][1];
for (ll j=i+1; j<save.size(); j++) {
if (save[j][0] > save[i][0] + R - 1) break;
l = max(l, save[j][2]);
r = max(r, save[j][3]);
sum = max(sum, save[j][1]);
}
ans = min(ans, max(l+r, sum));
}
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());
ll ans = inf;
for (ll i=0; i<=R-1; i++) {
for (ll j=0; j<=R-1; j++) {
ans = min(ans, i + j + solve(p, i, j));
}
}
cout << ans << '\n';
}
Compilation message
cultivation.cpp: In function 'll solve(std::vector<std::pair<long long int, long long int> >, ll, ll)':
cultivation.cpp:15:19: 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]
15 | for (ll i=0; i<p.size(); i++) {
| ~^~~~~~~~~
cultivation.cpp:22:19: 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]
22 | for (ll i=0; i<upd.size(); i++) {
| ~^~~~~~~~~~~
cultivation.cpp:29:27: 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]
29 | for (ll j=0; j<cur.size(); j++) {
| ~^~~~~~~~~~~
cultivation.cpp:38:19: 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]
38 | if (i + 1 != upd.size() && upd[i].first == upd[i+1].first) continue;
| ~~~~~~^~~~~~~~~~~~~
cultivation.cpp:45:25: 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]
45 | for (ll j=0; j+1<cur.size(); j++) {
| ~~~^~~~~~~~~~~
cultivation.cpp:51:19: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for (ll i=0; i<save.size(); i++) {
| ~^~~~~~~~~~~~
cultivation.cpp:54:25: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::array<long long int, 4> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
54 | for (ll j=i+1; j<save.size(); j++) {
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
0 ms |
348 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
0 ms |
348 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 |
348 KB |
Output is correct |
18 |
Correct |
10 ms |
468 KB |
Output is correct |
19 |
Correct |
5 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
14 ms |
456 KB |
Output is correct |
22 |
Correct |
104 ms |
348 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
379 ms |
460 KB |
Output is correct |
25 |
Correct |
181 ms |
348 KB |
Output is correct |
26 |
Correct |
972 ms |
468 KB |
Output is correct |
27 |
Correct |
953 ms |
348 KB |
Output is correct |
28 |
Correct |
387 ms |
344 KB |
Output is correct |
29 |
Correct |
846 ms |
348 KB |
Output is correct |
30 |
Correct |
942 ms |
472 KB |
Output is correct |
31 |
Correct |
955 ms |
344 KB |
Output is correct |
32 |
Correct |
1002 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
0 ms |
348 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 |
348 KB |
Output is correct |
18 |
Correct |
10 ms |
468 KB |
Output is correct |
19 |
Correct |
5 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
14 ms |
456 KB |
Output is correct |
22 |
Correct |
104 ms |
348 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
379 ms |
460 KB |
Output is correct |
25 |
Correct |
181 ms |
348 KB |
Output is correct |
26 |
Correct |
972 ms |
468 KB |
Output is correct |
27 |
Correct |
953 ms |
348 KB |
Output is correct |
28 |
Correct |
387 ms |
344 KB |
Output is correct |
29 |
Correct |
846 ms |
348 KB |
Output is correct |
30 |
Correct |
942 ms |
472 KB |
Output is correct |
31 |
Correct |
955 ms |
344 KB |
Output is correct |
32 |
Correct |
1002 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
344 KB |
Output is correct |
34 |
Correct |
417 ms |
472 KB |
Output is correct |
35 |
Correct |
906 ms |
480 KB |
Output is correct |
36 |
Correct |
817 ms |
596 KB |
Output is correct |
37 |
Correct |
616 ms |
476 KB |
Output is correct |
38 |
Correct |
859 ms |
348 KB |
Output is correct |
39 |
Correct |
817 ms |
472 KB |
Output is correct |
40 |
Correct |
896 ms |
480 KB |
Output is correct |
41 |
Correct |
561 ms |
472 KB |
Output is correct |
42 |
Correct |
638 ms |
344 KB |
Output is correct |
43 |
Correct |
779 ms |
480 KB |
Output is correct |
44 |
Correct |
935 ms |
472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2044 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2044 ms |
344 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 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 |
0 ms |
348 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 |
348 KB |
Output is correct |
18 |
Correct |
10 ms |
468 KB |
Output is correct |
19 |
Correct |
5 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
14 ms |
456 KB |
Output is correct |
22 |
Correct |
104 ms |
348 KB |
Output is correct |
23 |
Correct |
7 ms |
344 KB |
Output is correct |
24 |
Correct |
379 ms |
460 KB |
Output is correct |
25 |
Correct |
181 ms |
348 KB |
Output is correct |
26 |
Correct |
972 ms |
468 KB |
Output is correct |
27 |
Correct |
953 ms |
348 KB |
Output is correct |
28 |
Correct |
387 ms |
344 KB |
Output is correct |
29 |
Correct |
846 ms |
348 KB |
Output is correct |
30 |
Correct |
942 ms |
472 KB |
Output is correct |
31 |
Correct |
955 ms |
344 KB |
Output is correct |
32 |
Correct |
1002 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
344 KB |
Output is correct |
34 |
Correct |
417 ms |
472 KB |
Output is correct |
35 |
Correct |
906 ms |
480 KB |
Output is correct |
36 |
Correct |
817 ms |
596 KB |
Output is correct |
37 |
Correct |
616 ms |
476 KB |
Output is correct |
38 |
Correct |
859 ms |
348 KB |
Output is correct |
39 |
Correct |
817 ms |
472 KB |
Output is correct |
40 |
Correct |
896 ms |
480 KB |
Output is correct |
41 |
Correct |
561 ms |
472 KB |
Output is correct |
42 |
Correct |
638 ms |
344 KB |
Output is correct |
43 |
Correct |
779 ms |
480 KB |
Output is correct |
44 |
Correct |
935 ms |
472 KB |
Output is correct |
45 |
Execution timed out |
2044 ms |
344 KB |
Time limit exceeded |
46 |
Halted |
0 ms |
0 KB |
- |