#include <cstdio>
#include <climits>
#include <vector>
#include <algorithm>
typedef long long i64;
const int N = 300 + 10;
int r, c, n;
struct Point {
int x, y;
inline bool operator< (const Point &rhs) const {
return x < rhs.x || (x == rhs.x && y < rhs.y);
}
} p[N];
struct Info {
int a, b, c;
Info() { a = b = c = 0; }
Info(int _a, int _b, int _c): a(_a), b(_b), c(_c) {}
} f[N][N];
void init(int l) {
static int cur[N];
int tot = 0;
for (int r = l; r <= n; ++r) {
int k = std::lower_bound(cur, cur + tot, p[r].y) - cur;
cur[tot] = p[r].y;
std::rotate(cur + k, cur + tot, cur + tot + 1);
f[l][r].a = cur[0] - 1;
f[l][r].b = c - cur[tot++];
for (int i = 1; i < tot; ++i) f[l][r].c = std::max(f[l][r].c, cur[i] - cur[i - 1] - 1);
}
}
class Queue {
std::pair<int, int> 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, int y) {
while (f <= r && q[r].second <= y) --r;
q[++r] = std::make_pair(x, y);
}
inline int front() const { return q[f].second; }
};
int ans;
void solve(int len) {
static Info info[4 * N];
static i64 pool[4 * N], temp[4 * N];
for (int i = 1; i <= n; ++i) {
pool[i] = p[i].x;
pool[i + n] = p[i].x + len;
pool[i + 2 * n] = p[i].x + len + 1;
}
std::merge(pool + 1, pool + n + 1, pool + n + 1, pool + 2 * n + 1, temp + 1);
std::copy(pool + 2 * n + 1, pool + 3 * n + 1, temp + 2 * n);
std::merge(temp + 1, temp + 2 * n + 1, temp + 2 * n + 1, temp + 3 * n + 1, pool + 1);
int tot = std::unique(pool + 1, pool + 3 * n + 1) - pool - 1;
for (int i = 1, l = 1, r = 1; i <= tot; ++i) {
while (l <= n && !(p[l].x <= pool[i] && pool[i] <= (i64)p[l].x + len)) ++l;
for (r = std::max(l, r); r <= n && p[r].x <= pool[i] && pool[i] <= (i64)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);
i64 cur = std::max(q[0].front() + q[1].front(), q[2].front());
if (cur >= INT_MAX) break;
ans = std::min<i64>(ans, cur + len);
}
}
int main() {
scanf("%d%d%d", &r, &c, &n);
for (int i = 1; i <= n; ++i) scanf("%d%d", &p[i].x, &p[i].y);
std::sort(p + 1, p + n + 1);
std::vector<int> 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, 0));
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());
ans = INT_MAX;
for (int i = 0; i < cur.size(); ++i) solve(cur[i]);
printf("%d\n", ans);
return 0;
}
Compilation message
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) solve(cur[i]);
^
cultivation.cpp:88:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &r, &c, &n);
^
cultivation.cpp:89:63: 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("%d%d", &p[i].x, &p[i].y);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Incorrect |
0 ms |
2376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Incorrect |
0 ms |
2376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Incorrect |
0 ms |
2376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Correct |
0 ms |
2376 KB |
Output is correct |
3 |
Correct |
3 ms |
2376 KB |
Output is correct |
4 |
Incorrect |
0 ms |
2376 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Correct |
0 ms |
2376 KB |
Output is correct |
3 |
Correct |
3 ms |
2376 KB |
Output is correct |
4 |
Incorrect |
0 ms |
2376 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2376 KB |
Output is correct |
2 |
Incorrect |
0 ms |
2376 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |