#include <cstdio>
#include <climits>
#include <vector>
#include <algorithm>
typedef long long i64;
const int N = 300 + 10;
i64 r, c;
int n;
struct Point {
i64 x, y;
Point() {}
Point(i64 _x, i64 _y): x(_x), y(_y) {}
inline bool operator< (const Point &rhs) const {
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
} p[N];
struct Info {
i64 a, b, c;
Info() { a = b = c = 0; }
Info(i64 _a, i64 _b, i64 _c): a(_a), b(_b), c(_c) {}
} f[N][N];
void init(int l) {
std::vector<i64> cur;
for (int r = l; r <= n; ++r) {
int temp = cur.size();
cur.push_back(p[r].y);
std::inplace_merge(cur.begin(), cur.begin() + temp, cur.end());
f[l][r].a = cur[0] - 1;
f[l][r].b = c - cur.back();
for (int i = 1; i < cur.size(); ++i) f[l][r].c = std::max(f[l][r].c, cur[i] - cur[i - 1] - 1);
}
}
class Queue {
std::pair<int, i64> q[4 * N];
int f, r;
public:
void clear() {
f = 1;
r = 0;
}
void trim(int s) {
while (f <= r && q[f].first < s) ++f;
}
void push(int x, i64 y) {
while (f <= r && q[r].second <= y) --r;
q[++r] = std::make_pair(x, y);
}
inline i64 front() const { return q[f].second; }
};
i64 solve(int len) {
i64 res = INT_MAX;
static Info info[4 * N];
static i64 pool[4 * N];
int tot = 0;
for (int i = 1; i <= n; ++i) {
pool[++tot] = p[i].x;
pool[++tot] = p[i].x + len;
pool[++tot] = p[i].x + len + 1;
pool[++tot] = p[i].x + r - 1;
}
std::sort(pool + 1, pool + tot + 1);
tot = std::unique(pool + 1, pool + tot + 1) - pool - 1;
for (int i = 1, l = 1, r = 1; i <= tot; ++i) {
while (l <= n && !(p[l].x <= pool[i] && pool[i] <= p[l].x + len)) ++l;
for (r = std::max(l, r); r <= n && p[r].x <= pool[i] && pool[i] <= p[r].x + len;) ++r;
info[i] = (l > n ? Info(INT_MAX, INT_MAX, INT_MAX) : f[l][r - 1]);
}
static Queue q[3];
q[0].clear(), q[1].clear(), q[2].clear();
for (int i = 1, j = 1; i <= tot; ++i) {
i64 s = pool[i];
q[0].trim(i), q[1].trim(i), q[2].trim(i);
for (; j <= tot && pool[j] <= s + r - 1; ++j) q[0].push(j, info[j].a), q[1].push(j, info[j].b), q[2].push(j, info[j].c);
res = std::min(res, std::max(q[0].front() + q[1].front(), q[2].front()));
}
return res;
}
int main() {
scanf("%lld%lld%d", &r, &c, &n);
for (int i = 1; i <= n; ++i) scanf("%lld%lld", &p[i].x, &p[i].y);
std::sort(p + 1, p + n + 1);
std::vector<i64> cur;
for (int i = 1; i <= n; ++i) init(i);
for (int i = 1; i <= n; ++i)
for (int j = 1; j < i; ++j)
cur.push_back(std::max(p[i].x - p[j].x - 1, 0LL));
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= n; ++j)
cur.push_back(p[i].x - 1 + r - p[j].x);
for (int i = 1; i <= n; ++i) cur.push_back(p[i].x - 1), cur.push_back(r - p[i].x);
cur.push_back(0);
std::sort(cur.begin(), cur.end());
cur.erase(std::unique(cur.begin(), cur.end()), cur.end());
i64 ans = INT_MAX;
for (int i = 0; i < cur.size(); ++i) ans = std::min(ans, cur[i] + solve(cur[i]));
printf("%lld\n", ans);
return 0;
}
Compilation message
cultivation.cpp: In function 'void init(int)':
cultivation.cpp:36:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 1; i < cur.size(); ++i) f[l][r].c = std::max(f[l][r].c, cur[i] - cur[i - 1] - 1);
^
cultivation.cpp: In function 'int main()':
cultivation.cpp:104:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < cur.size(); ++i) ans = std::min(ans, cur[i] + solve(cur[i]));
^
cultivation.cpp:88:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%d", &r, &c, &n);
^
cultivation.cpp:89:67: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i = 1; i <= n; ++i) scanf("%lld%lld", &p[i].x, &p[i].y);
^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3540 KB |
Output is correct |
2 |
Correct |
0 ms |
3540 KB |
Output is correct |
3 |
Correct |
0 ms |
3540 KB |
Output is correct |
4 |
Correct |
0 ms |
3540 KB |
Output is correct |
5 |
Correct |
0 ms |
3540 KB |
Output is correct |
6 |
Correct |
0 ms |
3540 KB |
Output is correct |
7 |
Correct |
0 ms |
3540 KB |
Output is correct |
8 |
Correct |
0 ms |
3540 KB |
Output is correct |
9 |
Correct |
0 ms |
3540 KB |
Output is correct |
10 |
Correct |
0 ms |
3540 KB |
Output is correct |
11 |
Correct |
0 ms |
3540 KB |
Output is correct |
12 |
Correct |
0 ms |
3540 KB |
Output is correct |
13 |
Correct |
0 ms |
3540 KB |
Output is correct |
14 |
Correct |
0 ms |
3540 KB |
Output is correct |
15 |
Correct |
0 ms |
3540 KB |
Output is correct |
16 |
Correct |
0 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3540 KB |
Output is correct |
2 |
Correct |
0 ms |
3540 KB |
Output is correct |
3 |
Correct |
0 ms |
3540 KB |
Output is correct |
4 |
Correct |
0 ms |
3540 KB |
Output is correct |
5 |
Correct |
0 ms |
3540 KB |
Output is correct |
6 |
Correct |
0 ms |
3540 KB |
Output is correct |
7 |
Correct |
0 ms |
3540 KB |
Output is correct |
8 |
Correct |
0 ms |
3540 KB |
Output is correct |
9 |
Correct |
0 ms |
3540 KB |
Output is correct |
10 |
Correct |
0 ms |
3540 KB |
Output is correct |
11 |
Correct |
0 ms |
3540 KB |
Output is correct |
12 |
Correct |
0 ms |
3540 KB |
Output is correct |
13 |
Correct |
0 ms |
3540 KB |
Output is correct |
14 |
Correct |
0 ms |
3540 KB |
Output is correct |
15 |
Correct |
0 ms |
3540 KB |
Output is correct |
16 |
Correct |
0 ms |
3540 KB |
Output is correct |
17 |
Correct |
0 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3680 KB |
Output is correct |
19 |
Correct |
0 ms |
3540 KB |
Output is correct |
20 |
Correct |
0 ms |
3540 KB |
Output is correct |
21 |
Correct |
0 ms |
3680 KB |
Output is correct |
22 |
Correct |
6 ms |
4008 KB |
Output is correct |
23 |
Correct |
0 ms |
3540 KB |
Output is correct |
24 |
Correct |
13 ms |
4392 KB |
Output is correct |
25 |
Correct |
6 ms |
4392 KB |
Output is correct |
26 |
Correct |
19 ms |
6696 KB |
Output is correct |
27 |
Correct |
16 ms |
6696 KB |
Output is correct |
28 |
Correct |
6 ms |
4392 KB |
Output is correct |
29 |
Correct |
16 ms |
6696 KB |
Output is correct |
30 |
Correct |
19 ms |
6696 KB |
Output is correct |
31 |
Correct |
19 ms |
6696 KB |
Output is correct |
32 |
Correct |
19 ms |
6696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3540 KB |
Output is correct |
2 |
Correct |
0 ms |
3540 KB |
Output is correct |
3 |
Correct |
0 ms |
3540 KB |
Output is correct |
4 |
Correct |
0 ms |
3540 KB |
Output is correct |
5 |
Correct |
0 ms |
3540 KB |
Output is correct |
6 |
Correct |
0 ms |
3540 KB |
Output is correct |
7 |
Correct |
0 ms |
3540 KB |
Output is correct |
8 |
Correct |
0 ms |
3540 KB |
Output is correct |
9 |
Correct |
0 ms |
3540 KB |
Output is correct |
10 |
Correct |
0 ms |
3540 KB |
Output is correct |
11 |
Correct |
0 ms |
3540 KB |
Output is correct |
12 |
Correct |
0 ms |
3540 KB |
Output is correct |
13 |
Correct |
0 ms |
3540 KB |
Output is correct |
14 |
Correct |
0 ms |
3540 KB |
Output is correct |
15 |
Correct |
0 ms |
3540 KB |
Output is correct |
16 |
Correct |
0 ms |
3540 KB |
Output is correct |
17 |
Correct |
0 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3680 KB |
Output is correct |
19 |
Correct |
0 ms |
3540 KB |
Output is correct |
20 |
Correct |
0 ms |
3540 KB |
Output is correct |
21 |
Correct |
0 ms |
3680 KB |
Output is correct |
22 |
Correct |
6 ms |
4008 KB |
Output is correct |
23 |
Correct |
0 ms |
3540 KB |
Output is correct |
24 |
Correct |
13 ms |
4392 KB |
Output is correct |
25 |
Correct |
6 ms |
4392 KB |
Output is correct |
26 |
Correct |
19 ms |
6696 KB |
Output is correct |
27 |
Correct |
16 ms |
6696 KB |
Output is correct |
28 |
Correct |
6 ms |
4392 KB |
Output is correct |
29 |
Correct |
16 ms |
6696 KB |
Output is correct |
30 |
Correct |
19 ms |
6696 KB |
Output is correct |
31 |
Correct |
19 ms |
6696 KB |
Output is correct |
32 |
Correct |
19 ms |
6696 KB |
Output is correct |
33 |
Correct |
13 ms |
6696 KB |
Output is correct |
34 |
Correct |
16 ms |
6696 KB |
Output is correct |
35 |
Correct |
19 ms |
6696 KB |
Output is correct |
36 |
Correct |
19 ms |
6696 KB |
Output is correct |
37 |
Correct |
16 ms |
6696 KB |
Output is correct |
38 |
Correct |
23 ms |
6696 KB |
Output is correct |
39 |
Correct |
19 ms |
6696 KB |
Output is correct |
40 |
Correct |
16 ms |
6696 KB |
Output is correct |
41 |
Correct |
19 ms |
6696 KB |
Output is correct |
42 |
Correct |
16 ms |
6696 KB |
Output is correct |
43 |
Correct |
19 ms |
6696 KB |
Output is correct |
44 |
Correct |
19 ms |
6696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3540 KB |
Output is correct |
2 |
Correct |
3 ms |
3540 KB |
Output is correct |
3 |
Correct |
3 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
0 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
3 ms |
3540 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
0 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3540 KB |
Output is correct |
16 |
Correct |
3 ms |
3540 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
3 ms |
3540 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3540 KB |
Output is correct |
2 |
Correct |
3 ms |
3540 KB |
Output is correct |
3 |
Correct |
3 ms |
3540 KB |
Output is correct |
4 |
Correct |
3 ms |
3540 KB |
Output is correct |
5 |
Correct |
3 ms |
3540 KB |
Output is correct |
6 |
Correct |
3 ms |
3540 KB |
Output is correct |
7 |
Correct |
0 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3540 KB |
Output is correct |
9 |
Correct |
3 ms |
3540 KB |
Output is correct |
10 |
Correct |
3 ms |
3540 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
3 ms |
3540 KB |
Output is correct |
13 |
Correct |
0 ms |
3540 KB |
Output is correct |
14 |
Correct |
3 ms |
3540 KB |
Output is correct |
15 |
Correct |
3 ms |
3540 KB |
Output is correct |
16 |
Correct |
3 ms |
3540 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3540 KB |
Output is correct |
19 |
Correct |
3 ms |
3540 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
163 ms |
3812 KB |
Output is correct |
22 |
Correct |
266 ms |
3812 KB |
Output is correct |
23 |
Correct |
263 ms |
3812 KB |
Output is correct |
24 |
Correct |
246 ms |
3812 KB |
Output is correct |
25 |
Correct |
263 ms |
3812 KB |
Output is correct |
26 |
Correct |
96 ms |
3812 KB |
Output is correct |
27 |
Correct |
263 ms |
3812 KB |
Output is correct |
28 |
Correct |
279 ms |
3812 KB |
Output is correct |
29 |
Correct |
249 ms |
3812 KB |
Output is correct |
30 |
Correct |
266 ms |
3812 KB |
Output is correct |
31 |
Correct |
266 ms |
3812 KB |
Output is correct |
32 |
Correct |
246 ms |
3812 KB |
Output is correct |
33 |
Correct |
259 ms |
3812 KB |
Output is correct |
34 |
Correct |
263 ms |
3812 KB |
Output is correct |
35 |
Correct |
263 ms |
3812 KB |
Output is correct |
36 |
Correct |
249 ms |
3812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
3540 KB |
Output is correct |
2 |
Correct |
0 ms |
3540 KB |
Output is correct |
3 |
Correct |
0 ms |
3540 KB |
Output is correct |
4 |
Correct |
0 ms |
3540 KB |
Output is correct |
5 |
Correct |
0 ms |
3540 KB |
Output is correct |
6 |
Correct |
0 ms |
3540 KB |
Output is correct |
7 |
Correct |
0 ms |
3540 KB |
Output is correct |
8 |
Correct |
0 ms |
3540 KB |
Output is correct |
9 |
Correct |
0 ms |
3540 KB |
Output is correct |
10 |
Correct |
0 ms |
3540 KB |
Output is correct |
11 |
Correct |
0 ms |
3540 KB |
Output is correct |
12 |
Correct |
0 ms |
3540 KB |
Output is correct |
13 |
Correct |
0 ms |
3540 KB |
Output is correct |
14 |
Correct |
0 ms |
3540 KB |
Output is correct |
15 |
Correct |
0 ms |
3540 KB |
Output is correct |
16 |
Correct |
0 ms |
3540 KB |
Output is correct |
17 |
Correct |
0 ms |
3540 KB |
Output is correct |
18 |
Correct |
3 ms |
3680 KB |
Output is correct |
19 |
Correct |
0 ms |
3540 KB |
Output is correct |
20 |
Correct |
0 ms |
3540 KB |
Output is correct |
21 |
Correct |
0 ms |
3680 KB |
Output is correct |
22 |
Correct |
6 ms |
4008 KB |
Output is correct |
23 |
Correct |
0 ms |
3540 KB |
Output is correct |
24 |
Correct |
13 ms |
4392 KB |
Output is correct |
25 |
Correct |
6 ms |
4392 KB |
Output is correct |
26 |
Correct |
19 ms |
6696 KB |
Output is correct |
27 |
Correct |
16 ms |
6696 KB |
Output is correct |
28 |
Correct |
6 ms |
4392 KB |
Output is correct |
29 |
Correct |
16 ms |
6696 KB |
Output is correct |
30 |
Correct |
19 ms |
6696 KB |
Output is correct |
31 |
Correct |
19 ms |
6696 KB |
Output is correct |
32 |
Correct |
19 ms |
6696 KB |
Output is correct |
33 |
Correct |
13 ms |
6696 KB |
Output is correct |
34 |
Correct |
16 ms |
6696 KB |
Output is correct |
35 |
Correct |
19 ms |
6696 KB |
Output is correct |
36 |
Correct |
19 ms |
6696 KB |
Output is correct |
37 |
Correct |
16 ms |
6696 KB |
Output is correct |
38 |
Correct |
23 ms |
6696 KB |
Output is correct |
39 |
Correct |
19 ms |
6696 KB |
Output is correct |
40 |
Correct |
16 ms |
6696 KB |
Output is correct |
41 |
Correct |
19 ms |
6696 KB |
Output is correct |
42 |
Correct |
16 ms |
6696 KB |
Output is correct |
43 |
Correct |
19 ms |
6696 KB |
Output is correct |
44 |
Correct |
19 ms |
6696 KB |
Output is correct |
45 |
Correct |
3 ms |
3540 KB |
Output is correct |
46 |
Correct |
3 ms |
3540 KB |
Output is correct |
47 |
Correct |
3 ms |
3540 KB |
Output is correct |
48 |
Correct |
3 ms |
3540 KB |
Output is correct |
49 |
Correct |
3 ms |
3540 KB |
Output is correct |
50 |
Correct |
3 ms |
3540 KB |
Output is correct |
51 |
Correct |
0 ms |
3540 KB |
Output is correct |
52 |
Correct |
3 ms |
3540 KB |
Output is correct |
53 |
Correct |
3 ms |
3540 KB |
Output is correct |
54 |
Correct |
3 ms |
3540 KB |
Output is correct |
55 |
Correct |
3 ms |
3540 KB |
Output is correct |
56 |
Correct |
3 ms |
3540 KB |
Output is correct |
57 |
Correct |
0 ms |
3540 KB |
Output is correct |
58 |
Correct |
3 ms |
3540 KB |
Output is correct |
59 |
Correct |
3 ms |
3540 KB |
Output is correct |
60 |
Correct |
3 ms |
3540 KB |
Output is correct |
61 |
Correct |
3 ms |
3540 KB |
Output is correct |
62 |
Correct |
3 ms |
3540 KB |
Output is correct |
63 |
Correct |
3 ms |
3540 KB |
Output is correct |
64 |
Correct |
3 ms |
3540 KB |
Output is correct |
65 |
Correct |
163 ms |
3812 KB |
Output is correct |
66 |
Correct |
266 ms |
3812 KB |
Output is correct |
67 |
Correct |
263 ms |
3812 KB |
Output is correct |
68 |
Correct |
246 ms |
3812 KB |
Output is correct |
69 |
Correct |
263 ms |
3812 KB |
Output is correct |
70 |
Correct |
96 ms |
3812 KB |
Output is correct |
71 |
Correct |
263 ms |
3812 KB |
Output is correct |
72 |
Correct |
279 ms |
3812 KB |
Output is correct |
73 |
Correct |
249 ms |
3812 KB |
Output is correct |
74 |
Correct |
266 ms |
3812 KB |
Output is correct |
75 |
Correct |
266 ms |
3812 KB |
Output is correct |
76 |
Correct |
246 ms |
3812 KB |
Output is correct |
77 |
Correct |
259 ms |
3812 KB |
Output is correct |
78 |
Correct |
263 ms |
3812 KB |
Output is correct |
79 |
Correct |
263 ms |
3812 KB |
Output is correct |
80 |
Correct |
249 ms |
3812 KB |
Output is correct |
81 |
Execution timed out |
2000 ms |
5160 KB |
Execution timed out |
82 |
Halted |
0 ms |
0 KB |
- |