#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 303;
int s[N], e[N];
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int r, c;
cin >> r >> c;
int n;
cin >> n;
for (int i = 1;i <= n;i++) cin >> s[i] >> e[i];
set<int> up, down;
for (int i = 1;i <= n;i++) {
int all = s[i] - 1;
up.insert(all);
all = r - s[i];
down.insert(all);
}
for (int i = 1;i <= n;i++) {
for (int j = i + 1;j <= n;j++) {
if (abs(s[i] - s[j]) > 1) {
up.insert(abs(s[i] - s[j]) - 1);
down.insert(abs(s[i] - s[j]) - 1);
}
if (abs(s[i] - s[j]) >= 1) {
up.insert(abs(s[i] - s[j]));
down.insert(abs(s[i] - s[j]));
}
}
}
// iterate for all O(n ^ 2)
ll mn = LLONG_MAX;
for (auto& u : up) {
for (auto& d : down) {
bool ch = 1;
int mx = 0;
int dl = 0, dr = 0;
multiset<int> ms;
vector<pair<int, int>> event;
for (int i = 1;i <= n;i++) {
event.push_back({ max(1, s[i] - u), e[i] });
event.push_back({ min(r + 1, s[i] + d + 1), -e[i] });
}
ms.insert(0);
ms.insert(c + 1);
sort(event.begin(), event.end());
int sz = event.size();
if (event[0].first != 1) continue;
for (int i = 0, j;i < sz;i = j) {
if (event[i].first == r + 1) break;
for (j = i;j < sz && event[i].first == event[j].first;j++) {
int v = event[j].second;
if (v < 0) ms.erase(ms.find(-v));
else ms.insert(v);
}
for (j = i;j < sz && event[i].first == event[j].first;j++) {
int v = abs(event[j].second);
int l = *(--ms.lower_bound(abs(v)));
int r = *(ms.upper_bound(abs(v)));
if (ms.find(v) == ms.end())
mx = max(mx, r - l - 1);
else
mx = max({ mx, v - l - 1, r - v - 1 });
}
if ((int)ms.size() == 2) {
ch = 0;
break;
}
int fl = *(ms.upper_bound(0));
int fr = *(--ms.lower_bound(c + 1));
dl = max(dl, fl - 1);
dr = max(dr, c - fr);
}
if (ch) {
mn = min(mn, 0ll + u + d + max(dl + dr, mx));
//cout << u << ' ' << d << ' ' << dl << ' ' << dr << ' ' << mx << '\n';
}
}
}
cout << mn << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
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 |
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 |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
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 |
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 |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
10 ms |
344 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
11 ms |
468 KB |
Output is correct |
22 |
Correct |
42 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
75 ms |
348 KB |
Output is correct |
25 |
Correct |
53 ms |
344 KB |
Output is correct |
26 |
Correct |
118 ms |
348 KB |
Output is correct |
27 |
Correct |
121 ms |
344 KB |
Output is correct |
28 |
Correct |
72 ms |
344 KB |
Output is correct |
29 |
Correct |
113 ms |
468 KB |
Output is correct |
30 |
Correct |
119 ms |
348 KB |
Output is correct |
31 |
Correct |
126 ms |
344 KB |
Output is correct |
32 |
Correct |
123 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
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 |
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 |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
10 ms |
344 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
11 ms |
468 KB |
Output is correct |
22 |
Correct |
42 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
75 ms |
348 KB |
Output is correct |
25 |
Correct |
53 ms |
344 KB |
Output is correct |
26 |
Correct |
118 ms |
348 KB |
Output is correct |
27 |
Correct |
121 ms |
344 KB |
Output is correct |
28 |
Correct |
72 ms |
344 KB |
Output is correct |
29 |
Correct |
113 ms |
468 KB |
Output is correct |
30 |
Correct |
119 ms |
348 KB |
Output is correct |
31 |
Correct |
126 ms |
344 KB |
Output is correct |
32 |
Correct |
123 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
96 ms |
468 KB |
Output is correct |
35 |
Correct |
131 ms |
344 KB |
Output is correct |
36 |
Correct |
130 ms |
472 KB |
Output is correct |
37 |
Correct |
105 ms |
468 KB |
Output is correct |
38 |
Correct |
128 ms |
472 KB |
Output is correct |
39 |
Correct |
128 ms |
344 KB |
Output is correct |
40 |
Correct |
135 ms |
348 KB |
Output is correct |
41 |
Correct |
96 ms |
344 KB |
Output is correct |
42 |
Correct |
121 ms |
344 KB |
Output is correct |
43 |
Correct |
134 ms |
344 KB |
Output is correct |
44 |
Correct |
130 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
348 KB |
Output is correct |
2 |
Correct |
941 ms |
492 KB |
Output is correct |
3 |
Correct |
1022 ms |
344 KB |
Output is correct |
4 |
Correct |
1176 ms |
492 KB |
Output is correct |
5 |
Correct |
1211 ms |
348 KB |
Output is correct |
6 |
Correct |
102 ms |
452 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
836 ms |
348 KB |
Output is correct |
9 |
Correct |
251 ms |
344 KB |
Output is correct |
10 |
Correct |
1036 ms |
492 KB |
Output is correct |
11 |
Correct |
1221 ms |
488 KB |
Output is correct |
12 |
Correct |
1102 ms |
348 KB |
Output is correct |
13 |
Correct |
65 ms |
448 KB |
Output is correct |
14 |
Correct |
302 ms |
344 KB |
Output is correct |
15 |
Correct |
13 ms |
348 KB |
Output is correct |
16 |
Correct |
1194 ms |
488 KB |
Output is correct |
17 |
Incorrect |
1096 ms |
344 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
131 ms |
348 KB |
Output is correct |
2 |
Correct |
941 ms |
492 KB |
Output is correct |
3 |
Correct |
1022 ms |
344 KB |
Output is correct |
4 |
Correct |
1176 ms |
492 KB |
Output is correct |
5 |
Correct |
1211 ms |
348 KB |
Output is correct |
6 |
Correct |
102 ms |
452 KB |
Output is correct |
7 |
Correct |
3 ms |
344 KB |
Output is correct |
8 |
Correct |
836 ms |
348 KB |
Output is correct |
9 |
Correct |
251 ms |
344 KB |
Output is correct |
10 |
Correct |
1036 ms |
492 KB |
Output is correct |
11 |
Correct |
1221 ms |
488 KB |
Output is correct |
12 |
Correct |
1102 ms |
348 KB |
Output is correct |
13 |
Correct |
65 ms |
448 KB |
Output is correct |
14 |
Correct |
302 ms |
344 KB |
Output is correct |
15 |
Correct |
13 ms |
348 KB |
Output is correct |
16 |
Correct |
1194 ms |
488 KB |
Output is correct |
17 |
Incorrect |
1096 ms |
344 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
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 |
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 |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
10 ms |
344 KB |
Output is correct |
19 |
Correct |
4 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
11 ms |
468 KB |
Output is correct |
22 |
Correct |
42 ms |
468 KB |
Output is correct |
23 |
Correct |
2 ms |
348 KB |
Output is correct |
24 |
Correct |
75 ms |
348 KB |
Output is correct |
25 |
Correct |
53 ms |
344 KB |
Output is correct |
26 |
Correct |
118 ms |
348 KB |
Output is correct |
27 |
Correct |
121 ms |
344 KB |
Output is correct |
28 |
Correct |
72 ms |
344 KB |
Output is correct |
29 |
Correct |
113 ms |
468 KB |
Output is correct |
30 |
Correct |
119 ms |
348 KB |
Output is correct |
31 |
Correct |
126 ms |
344 KB |
Output is correct |
32 |
Correct |
123 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
96 ms |
468 KB |
Output is correct |
35 |
Correct |
131 ms |
344 KB |
Output is correct |
36 |
Correct |
130 ms |
472 KB |
Output is correct |
37 |
Correct |
105 ms |
468 KB |
Output is correct |
38 |
Correct |
128 ms |
472 KB |
Output is correct |
39 |
Correct |
128 ms |
344 KB |
Output is correct |
40 |
Correct |
135 ms |
348 KB |
Output is correct |
41 |
Correct |
96 ms |
344 KB |
Output is correct |
42 |
Correct |
121 ms |
344 KB |
Output is correct |
43 |
Correct |
134 ms |
344 KB |
Output is correct |
44 |
Correct |
130 ms |
348 KB |
Output is correct |
45 |
Correct |
131 ms |
348 KB |
Output is correct |
46 |
Correct |
941 ms |
492 KB |
Output is correct |
47 |
Correct |
1022 ms |
344 KB |
Output is correct |
48 |
Correct |
1176 ms |
492 KB |
Output is correct |
49 |
Correct |
1211 ms |
348 KB |
Output is correct |
50 |
Correct |
102 ms |
452 KB |
Output is correct |
51 |
Correct |
3 ms |
344 KB |
Output is correct |
52 |
Correct |
836 ms |
348 KB |
Output is correct |
53 |
Correct |
251 ms |
344 KB |
Output is correct |
54 |
Correct |
1036 ms |
492 KB |
Output is correct |
55 |
Correct |
1221 ms |
488 KB |
Output is correct |
56 |
Correct |
1102 ms |
348 KB |
Output is correct |
57 |
Correct |
65 ms |
448 KB |
Output is correct |
58 |
Correct |
302 ms |
344 KB |
Output is correct |
59 |
Correct |
13 ms |
348 KB |
Output is correct |
60 |
Correct |
1194 ms |
488 KB |
Output is correct |
61 |
Incorrect |
1096 ms |
344 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |