#include <bits/stdc++.h>
using namespace std;
using lint = long long;
using pi = array<lint, 2>;
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
map<pi, lint> mp;
lint solve(lint r, lint c, lint dl, lint dr, vector<pi> a) {
if (mp.count({dl, dr}))
return mp[pi{dl, dr}];
vector<array<lint, 3>> event;
for (auto &[x, y] : a) {
int l = max(0ll, x - dl);
int e = min(r * 1ll, x + 1 + dr);
event.push_back({l, y, +1});
event.push_back({e, y, -1});
}
sort(all(event));
if (event[0][0] != 0 || event.back()[0] != r)
return mp[pi{dl, dr}] = 1e18;
lint lmax = 0, rmax = 0, lrmax = 0;
multiset<lint> s;
for (int i = 0; i < sz(event);) {
int j = i;
while (j < sz(event) && event[i][0] == event[j][0]) {
if (event[j][2] == +1)
s.insert(event[j][1]);
else
s.erase(s.find(event[j][1]));
j++;
}
if (j == sz(event))
break;
if (sz(s) == 0)
return mp[pi{dl, dr}] = 1e18;
lmax = max(lmax, *s.begin());
rmax = max(rmax, c - 1 - *s.rbegin());
{
auto it = s.begin();
while (next(it) != s.end()) {
lrmax = max(lrmax, *next(it) - *it - 1);
it++;
}
}
i = j;
}
return mp[pi{dl, dr}] = max(lmax + rmax, lrmax);
}
// n^6
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, r, c;
cin >> r >> c >> n;
vector<pi> a(n);
for (auto &[x, y] : a)
cin >> x >> y, x--, y--;
sort(all(a));
lint ans = 1e18;
vector<lint> gaps = {0};
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (a[j][0] > a[i][0])
gaps.push_back(a[j][0] - a[i][0] - 1);
}
}
sort(all(gaps));
gaps.resize(unique(all(gaps)) - gaps.begin());
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans = min(ans, a[i][0] + r - 1 - a[j][0] + solve(r, c, a[i][0], r - 1 - a[j][0], a));
}
}
for (int i = 0; i < n; i++) {
for (auto &dy : gaps) {
ans = min(ans, a[i][0] + dy + solve(r, c, a[i][0], dy, a));
ans = min(ans, dy + r - 1 - a[i][0] + solve(r, c, dy, r - 1 - a[i][0], a));
if (a[i][0] <= dy)
ans = min(ans, dy + solve(r, c, a[i][0], dy - a[i][0], a));
if (r - 1 - a[i][0] <= dy)
ans = min(ans, dy + solve(r, c, dy - (r - 1 - a[i][0]), r - 1 - a[i][0], a));
}
}
cout << ans << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
15 ms |
632 KB |
Output is correct |
19 |
Correct |
7 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
24 ms |
352 KB |
Output is correct |
22 |
Correct |
82 ms |
1584 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
166 ms |
616 KB |
Output is correct |
25 |
Correct |
109 ms |
524 KB |
Output is correct |
26 |
Correct |
246 ms |
856 KB |
Output is correct |
27 |
Correct |
246 ms |
928 KB |
Output is correct |
28 |
Correct |
151 ms |
608 KB |
Output is correct |
29 |
Correct |
222 ms |
856 KB |
Output is correct |
30 |
Correct |
244 ms |
984 KB |
Output is correct |
31 |
Correct |
244 ms |
856 KB |
Output is correct |
32 |
Correct |
248 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
15 ms |
632 KB |
Output is correct |
19 |
Correct |
7 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
24 ms |
352 KB |
Output is correct |
22 |
Correct |
82 ms |
1584 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
166 ms |
616 KB |
Output is correct |
25 |
Correct |
109 ms |
524 KB |
Output is correct |
26 |
Correct |
246 ms |
856 KB |
Output is correct |
27 |
Correct |
246 ms |
928 KB |
Output is correct |
28 |
Correct |
151 ms |
608 KB |
Output is correct |
29 |
Correct |
222 ms |
856 KB |
Output is correct |
30 |
Correct |
244 ms |
984 KB |
Output is correct |
31 |
Correct |
244 ms |
856 KB |
Output is correct |
32 |
Correct |
248 ms |
856 KB |
Output is correct |
33 |
Correct |
11 ms |
644 KB |
Output is correct |
34 |
Correct |
202 ms |
984 KB |
Output is correct |
35 |
Correct |
241 ms |
856 KB |
Output is correct |
36 |
Correct |
238 ms |
984 KB |
Output is correct |
37 |
Correct |
184 ms |
984 KB |
Output is correct |
38 |
Correct |
243 ms |
984 KB |
Output is correct |
39 |
Correct |
235 ms |
984 KB |
Output is correct |
40 |
Correct |
253 ms |
856 KB |
Output is correct |
41 |
Correct |
128 ms |
672 KB |
Output is correct |
42 |
Correct |
194 ms |
984 KB |
Output is correct |
43 |
Correct |
225 ms |
984 KB |
Output is correct |
44 |
Correct |
244 ms |
984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
716 KB |
Output is correct |
2 |
Correct |
116 ms |
1356 KB |
Output is correct |
3 |
Correct |
150 ms |
1520 KB |
Output is correct |
4 |
Correct |
127 ms |
1504 KB |
Output is correct |
5 |
Correct |
137 ms |
1440 KB |
Output is correct |
6 |
Correct |
61 ms |
772 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
99 ms |
1368 KB |
Output is correct |
9 |
Correct |
59 ms |
876 KB |
Output is correct |
10 |
Correct |
132 ms |
1484 KB |
Output is correct |
11 |
Correct |
136 ms |
1428 KB |
Output is correct |
12 |
Correct |
132 ms |
1472 KB |
Output is correct |
13 |
Correct |
26 ms |
388 KB |
Output is correct |
14 |
Correct |
46 ms |
784 KB |
Output is correct |
15 |
Correct |
9 ms |
448 KB |
Output is correct |
16 |
Correct |
138 ms |
1544 KB |
Output is correct |
17 |
Correct |
139 ms |
1556 KB |
Output is correct |
18 |
Correct |
15 ms |
724 KB |
Output is correct |
19 |
Correct |
130 ms |
1532 KB |
Output is correct |
20 |
Correct |
106 ms |
1492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
716 KB |
Output is correct |
2 |
Correct |
116 ms |
1356 KB |
Output is correct |
3 |
Correct |
150 ms |
1520 KB |
Output is correct |
4 |
Correct |
127 ms |
1504 KB |
Output is correct |
5 |
Correct |
137 ms |
1440 KB |
Output is correct |
6 |
Correct |
61 ms |
772 KB |
Output is correct |
7 |
Correct |
3 ms |
340 KB |
Output is correct |
8 |
Correct |
99 ms |
1368 KB |
Output is correct |
9 |
Correct |
59 ms |
876 KB |
Output is correct |
10 |
Correct |
132 ms |
1484 KB |
Output is correct |
11 |
Correct |
136 ms |
1428 KB |
Output is correct |
12 |
Correct |
132 ms |
1472 KB |
Output is correct |
13 |
Correct |
26 ms |
388 KB |
Output is correct |
14 |
Correct |
46 ms |
784 KB |
Output is correct |
15 |
Correct |
9 ms |
448 KB |
Output is correct |
16 |
Correct |
138 ms |
1544 KB |
Output is correct |
17 |
Correct |
139 ms |
1556 KB |
Output is correct |
18 |
Correct |
15 ms |
724 KB |
Output is correct |
19 |
Correct |
130 ms |
1532 KB |
Output is correct |
20 |
Correct |
106 ms |
1492 KB |
Output is correct |
21 |
Execution timed out |
2079 ms |
22736 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
2 ms |
340 KB |
Output is correct |
18 |
Correct |
15 ms |
632 KB |
Output is correct |
19 |
Correct |
7 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
24 ms |
352 KB |
Output is correct |
22 |
Correct |
82 ms |
1584 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
166 ms |
616 KB |
Output is correct |
25 |
Correct |
109 ms |
524 KB |
Output is correct |
26 |
Correct |
246 ms |
856 KB |
Output is correct |
27 |
Correct |
246 ms |
928 KB |
Output is correct |
28 |
Correct |
151 ms |
608 KB |
Output is correct |
29 |
Correct |
222 ms |
856 KB |
Output is correct |
30 |
Correct |
244 ms |
984 KB |
Output is correct |
31 |
Correct |
244 ms |
856 KB |
Output is correct |
32 |
Correct |
248 ms |
856 KB |
Output is correct |
33 |
Correct |
11 ms |
644 KB |
Output is correct |
34 |
Correct |
202 ms |
984 KB |
Output is correct |
35 |
Correct |
241 ms |
856 KB |
Output is correct |
36 |
Correct |
238 ms |
984 KB |
Output is correct |
37 |
Correct |
184 ms |
984 KB |
Output is correct |
38 |
Correct |
243 ms |
984 KB |
Output is correct |
39 |
Correct |
235 ms |
984 KB |
Output is correct |
40 |
Correct |
253 ms |
856 KB |
Output is correct |
41 |
Correct |
128 ms |
672 KB |
Output is correct |
42 |
Correct |
194 ms |
984 KB |
Output is correct |
43 |
Correct |
225 ms |
984 KB |
Output is correct |
44 |
Correct |
244 ms |
984 KB |
Output is correct |
45 |
Correct |
24 ms |
716 KB |
Output is correct |
46 |
Correct |
116 ms |
1356 KB |
Output is correct |
47 |
Correct |
150 ms |
1520 KB |
Output is correct |
48 |
Correct |
127 ms |
1504 KB |
Output is correct |
49 |
Correct |
137 ms |
1440 KB |
Output is correct |
50 |
Correct |
61 ms |
772 KB |
Output is correct |
51 |
Correct |
3 ms |
340 KB |
Output is correct |
52 |
Correct |
99 ms |
1368 KB |
Output is correct |
53 |
Correct |
59 ms |
876 KB |
Output is correct |
54 |
Correct |
132 ms |
1484 KB |
Output is correct |
55 |
Correct |
136 ms |
1428 KB |
Output is correct |
56 |
Correct |
132 ms |
1472 KB |
Output is correct |
57 |
Correct |
26 ms |
388 KB |
Output is correct |
58 |
Correct |
46 ms |
784 KB |
Output is correct |
59 |
Correct |
9 ms |
448 KB |
Output is correct |
60 |
Correct |
138 ms |
1544 KB |
Output is correct |
61 |
Correct |
139 ms |
1556 KB |
Output is correct |
62 |
Correct |
15 ms |
724 KB |
Output is correct |
63 |
Correct |
130 ms |
1532 KB |
Output is correct |
64 |
Correct |
106 ms |
1492 KB |
Output is correct |
65 |
Execution timed out |
2079 ms |
22736 KB |
Time limit exceeded |
66 |
Halted |
0 ms |
0 KB |
- |