#include <bits/stdc++.h>
#include <cassert>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 1030
#define MAXS 20
#define INF 20000000000020
#define bb ' '
#define ln '\n'
#define Ln '\n'
#define MOD 1000000007
ll H, W;
int N;
pll arr[MAX];
ll ans = INF;
int st[MAX * 2], en[MAX * 2];
ll U[MAX], D[MAX], UD[MAX];
ll A[MAX][MAX], B[MAX][MAX], C[MAX][MAX];
typedef pair<ll, int> pli;
deque<pli> up, down, su;
bool test(ll d) {
if (d <= 0) return false;
vector<ll> ys;
int i, j;
for (i = 1; i <= N; i++) ys.push_back(arr[i].second), ys.push_back(arr[i].second + d);
sort(ys.begin(), ys.end());
ys.erase(unique(ys.begin(), ys.end()), ys.end());
int X = ys.size();
for (i = 0; i < X; i++) U[i] = D[i] = UD[i] = 0, st[i] = en[i] = 0;
for (i = 1; i <= N; i++) {
int l, r;
l = lower_bound(ys.begin(), ys.end(), arr[i].second) - ys.begin();
r = lower_bound(ys.begin(), ys.end(), arr[i].second + d) - ys.begin();
st[l]++;
en[r]++;
}
int il, ir;
il = ir = 0;
for (i = 0; i < X; i++) {
ir += st[i];
il += en[i];
if (il == ir) {
UD[i] = INF;
continue;
}
U[i] = A[il + 1][ir];
D[i] = B[il + 1][ir];
UD[i] = C[il + 1][ir];
}
up.clear();
down.clear();
su.clear();
ll mn = INF;
j = 0;
for (i = 0; i < X - 1; i++) {
while (up.size() && up.back().first <= U[i]) up.pop_back();
up.emplace_back(U[i], i);
while (down.size() && down.back().first <= D[i]) down.pop_back();
down.emplace_back(D[i], i);
while (su.size() && su.back().first <= UD[i]) su.pop_back();
su.emplace_back(UD[i], i);
while (j + 1 <= i && ys[i + 1] - ys[j + 1] >= W) j++;
while (up.size() && up.front().second < j) up.pop_front();
while (down.size() && down.front().second < j) down.pop_front();
while (su.size() && su.front().second < j) su.pop_front();
if (ys[i + 1] - ys[j] >= W) mn = min(mn, max(0ll + up.front().first + down.front().first, (ll)su.front().first));
}
if (mn >= INF) return false;
ans = min(ans, mn + d - 1);
return true;
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
cin >> H >> W >> N;
int i, j, k;
vector<ll> ls;
ls.push_back(W);
for (i = 1; i <= N; i++) cin >> arr[i].first >> arr[i].second;
for (i = 1; i <= N; i++) {
for (j = i + 1; j <= N; j++) ls.push_back(abs(arr[j].second - arr[i].second));
for (j = i + 1; j <= N; j++) ls.push_back(abs(arr[j].second - arr[i].second) + W);
ls.push_back(arr[i].second);
ls.push_back(W - arr[i].second + 1);
}
sort(arr + 1, arr + N + 1, [&](pii p1, pii p2) {return p1.second < p2.second; });
for (i = 1; i <= N; i++) {
vector<ll> v;
A[i][i] = arr[i].first - 1;
B[i][i] = H - arr[i].first;
v.push_back(arr[i].first);
for (j = i + 1; j <= N; j++) {
v.insert(lower_bound(v.begin(), v.end(), arr[j].first), arr[j].first);
for (k = 0; k + 1 < v.size(); k++) C[i][j] = max(C[i][j], v[k + 1] - v[k] - 1);
A[i][j] = v[0] - 1;
B[i][j] = H - v.back();
}
}
vector<ll> cpy;
for (auto v : ls) cpy.push_back(W - v);
for (auto v : cpy) ls.push_back(v);
sort(ls.begin(), ls.end());
ls.erase(unique(ls.begin(), ls.end()), ls.end());
reverse(ls.begin(), ls.end());
for (auto v : ls) if (!test(v)) break;
cout << ans << ln;
}
Compilation message
cultivation.cpp: In function 'int main()':
cultivation.cpp:99:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
99 | for (k = 0; k + 1 < v.size(); k++) C[i][j] = max(C[i][j], v[k + 1] - v[k] - 1);
| ~~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
624 KB |
Output is correct |
18 |
Correct |
2 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
2388 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
8 ms |
4688 KB |
Output is correct |
25 |
Correct |
5 ms |
3280 KB |
Output is correct |
26 |
Correct |
18 ms |
7960 KB |
Output is correct |
27 |
Correct |
18 ms |
8012 KB |
Output is correct |
28 |
Correct |
8 ms |
4688 KB |
Output is correct |
29 |
Correct |
18 ms |
8012 KB |
Output is correct |
30 |
Correct |
18 ms |
8012 KB |
Output is correct |
31 |
Correct |
22 ms |
7920 KB |
Output is correct |
32 |
Correct |
18 ms |
7980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
624 KB |
Output is correct |
18 |
Correct |
2 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
2388 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
8 ms |
4688 KB |
Output is correct |
25 |
Correct |
5 ms |
3280 KB |
Output is correct |
26 |
Correct |
18 ms |
7960 KB |
Output is correct |
27 |
Correct |
18 ms |
8012 KB |
Output is correct |
28 |
Correct |
8 ms |
4688 KB |
Output is correct |
29 |
Correct |
18 ms |
8012 KB |
Output is correct |
30 |
Correct |
18 ms |
8012 KB |
Output is correct |
31 |
Correct |
22 ms |
7920 KB |
Output is correct |
32 |
Correct |
18 ms |
7980 KB |
Output is correct |
33 |
Execution timed out |
2052 ms |
7884 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
708 KB |
Output is correct |
19 |
Correct |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
3 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
596 KB |
Output is correct |
5 |
Correct |
3 ms |
596 KB |
Output is correct |
6 |
Correct |
2 ms |
596 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
596 KB |
Output is correct |
9 |
Correct |
2 ms |
596 KB |
Output is correct |
10 |
Correct |
3 ms |
596 KB |
Output is correct |
11 |
Correct |
3 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
2 ms |
596 KB |
Output is correct |
14 |
Correct |
2 ms |
596 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
708 KB |
Output is correct |
19 |
Correct |
3 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
97 ms |
2132 KB |
Output is correct |
22 |
Correct |
155 ms |
2132 KB |
Output is correct |
23 |
Correct |
107 ms |
2132 KB |
Output is correct |
24 |
Correct |
103 ms |
2132 KB |
Output is correct |
25 |
Correct |
160 ms |
2132 KB |
Output is correct |
26 |
Correct |
39 ms |
2132 KB |
Output is correct |
27 |
Correct |
153 ms |
2132 KB |
Output is correct |
28 |
Correct |
170 ms |
2132 KB |
Output is correct |
29 |
Correct |
157 ms |
2132 KB |
Output is correct |
30 |
Correct |
161 ms |
2132 KB |
Output is correct |
31 |
Correct |
159 ms |
2132 KB |
Output is correct |
32 |
Correct |
155 ms |
2132 KB |
Output is correct |
33 |
Correct |
160 ms |
2132 KB |
Output is correct |
34 |
Correct |
130 ms |
2132 KB |
Output is correct |
35 |
Correct |
120 ms |
2132 KB |
Output is correct |
36 |
Correct |
160 ms |
2172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
468 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
624 KB |
Output is correct |
18 |
Correct |
2 ms |
1364 KB |
Output is correct |
19 |
Correct |
1 ms |
724 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
1108 KB |
Output is correct |
22 |
Correct |
3 ms |
2388 KB |
Output is correct |
23 |
Correct |
1 ms |
724 KB |
Output is correct |
24 |
Correct |
8 ms |
4688 KB |
Output is correct |
25 |
Correct |
5 ms |
3280 KB |
Output is correct |
26 |
Correct |
18 ms |
7960 KB |
Output is correct |
27 |
Correct |
18 ms |
8012 KB |
Output is correct |
28 |
Correct |
8 ms |
4688 KB |
Output is correct |
29 |
Correct |
18 ms |
8012 KB |
Output is correct |
30 |
Correct |
18 ms |
8012 KB |
Output is correct |
31 |
Correct |
22 ms |
7920 KB |
Output is correct |
32 |
Correct |
18 ms |
7980 KB |
Output is correct |
33 |
Execution timed out |
2052 ms |
7884 KB |
Time limit exceeded |
34 |
Halted |
0 ms |
0 KB |
- |