#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Point{
ll x, y;
Point(){}
Point(ll x, ll y): x(x), y(y){}
bool operator<(const Point &r)const{
return make_pair(x, y) < make_pair(r.x, r.y);
}
};
struct Query{
ll x, y; bool in;
Query(){}
Query(ll x, ll y, bool in): x(x), y(y), in(in){}
bool operator<(const Query &r)const{
if(x!=r.x) return x<r.x;
return in<r.in;
}
};
struct State{
ll x, l, r, mid;
State(){}
State(ll x, ll l, ll r, ll mid): x(x), l(l), r(r), mid(mid){}
};
ll n, m; int k;
Point arr[302];
ll ans = LLONG_MAX;
ll maxDiff;
void testInterval(ll d){
if(d<maxDiff) return;
multiset<ll> mst;
multiset<ll> diff;
mst.insert(0);
mst.insert(m+1);
diff.insert(m+1);
vector<Query> vec;
for(int i=1; i<=k; i++){
vec.push_back(Query(arr[i].x, arr[i].y, 1));
vec.push_back(Query(arr[i].x+d+1, arr[i].y, 0));
}
sort(vec.begin(), vec.end());
vector<State> st;
for(int i=0; i<(int)vec.size(); i++){
if(vec[i].in == 1){
auto it = mst.lower_bound(vec[i].y);
diff.erase(diff.find(*it - *prev(it)));
diff.insert(*it - vec[i].y);
diff.insert(vec[i].y - *prev(it));
mst.insert(vec[i].y);
}
else{
auto it = mst.lower_bound(vec[i].y);
diff.erase(diff.find(*next(it) - vec[i].y));
diff.erase(diff.find(vec[i].y - *prev(it)));
diff.insert(*next(it) - *prev(it));
mst.erase(it);
}
if(i+1 == (int)vec.size() || vec[i].x != vec[i+1].x){
if(*diff.rbegin() == m+1) st.push_back(State(vec[i].x, 1e10, 1e10, 1e10));
else st.push_back(State(vec[i].x, *next(mst.begin()) - 1, m - *prev(prev(mst.end())),
*diff.rbegin() - 1));
}
}
// printf("diff %lld\n", d);
for(int i=0; i<(int)st.size(); i++){
// printf("%lld: %lld %lld %lld\n", st[i].x, st[i].l, st[i].r, st[i].mid);
// if(arr[1].x + d < vec[i].x) break;
int j = i;
while(j < (int)st.size()-1 && st[j+1].x - st[i].x < n) j++;
ll maxL = 0, maxR = 0, maxD = 0;
for(int u=i; u<=j; u++){
maxL = max(maxL, st[u].l);
maxR = max(maxR, st[u].r);
maxD = max(maxD, st[u].mid);
}
ans = min(ans, d + maxL + maxR + max(0LL, maxD - maxL - maxR));
}
}
int main(){
scanf("%lld %lld %d", &n, &m, &k);
for(int i=1; i<=k; i++){
scanf("%d %d", &arr[i].x, &arr[i].y);
}
sort(arr+1, arr+k+1);
arr[k+1].x = n+1;
for(int i=1; i<=k+1; i++) maxDiff = max(maxDiff, arr[i].x - arr[i-1].x);
maxDiff--;
testInterval(maxDiff);
for(int i=0; i<=k+1; i++){
for(int j=i+1; j<=k+1; j++){
testInterval(arr[j].x - arr[i].x - 1);
testInterval(arr[j].x - arr[i].x - 2);
}
}
for(int i=1; i<=k; i++){
for(int j=i; j<=k; j++){
testInterval(arr[i].x - 1 + n - arr[j].x);
}
}
for(int i=1; i<=k; i++) arr[i].x = n+1-arr[i].x, arr[i].y = m+1-arr[i].y;
reverse(arr+1, arr+k+1);
testInterval(maxDiff);
for(int i=0; i<=k+1; i++){
for(int j=i+1; j<=k+1; j++){
testInterval(arr[j].x - arr[i].x - 1);
testInterval(arr[j].x - arr[i].x - 2);
}
}
for(int i=1; i<=k; i++){
for(int j=i; j<=k; j++){
testInterval(arr[i].x - 1 + n - arr[j].x);
}
}
printf("%lld", ans);
}
Compilation message
cultivation.cpp: In function 'int main()':
cultivation.cpp:100:17: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'll*' {aka 'long long int*'} [-Wformat=]
100 | scanf("%d %d", &arr[i].x, &arr[i].y);
| ~^ ~~~~~~~~~
| | |
| int* ll* {aka long long int*}
| %lld
cultivation.cpp:100:20: warning: format '%d' expects argument of type 'int*', but argument 3 has type 'll*' {aka 'long long int*'} [-Wformat=]
100 | scanf("%d %d", &arr[i].x, &arr[i].y);
| ~^ ~~~~~~~~~
| | |
| int* ll* {aka long long int*}
| %lld
cultivation.cpp:98:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
98 | scanf("%lld %lld %d", &n, &m, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:100:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
100 | scanf("%d %d", &arr[i].x, &arr[i].y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
3 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
3 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
13 ms |
296 KB |
Output is correct |
18 |
Correct |
544 ms |
304 KB |
Output is correct |
19 |
Correct |
37 ms |
284 KB |
Output is correct |
20 |
Correct |
10 ms |
204 KB |
Output is correct |
21 |
Correct |
238 ms |
288 KB |
Output is correct |
22 |
Execution timed out |
2069 ms |
332 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
3 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
13 ms |
296 KB |
Output is correct |
18 |
Correct |
544 ms |
304 KB |
Output is correct |
19 |
Correct |
37 ms |
284 KB |
Output is correct |
20 |
Correct |
10 ms |
204 KB |
Output is correct |
21 |
Correct |
238 ms |
288 KB |
Output is correct |
22 |
Execution timed out |
2069 ms |
332 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
204 KB |
Output is correct |
2 |
Correct |
30 ms |
204 KB |
Output is correct |
3 |
Correct |
29 ms |
204 KB |
Output is correct |
4 |
Incorrect |
28 ms |
304 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
204 KB |
Output is correct |
2 |
Correct |
30 ms |
204 KB |
Output is correct |
3 |
Correct |
29 ms |
204 KB |
Output is correct |
4 |
Incorrect |
28 ms |
304 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
3 ms |
292 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
13 ms |
296 KB |
Output is correct |
18 |
Correct |
544 ms |
304 KB |
Output is correct |
19 |
Correct |
37 ms |
284 KB |
Output is correct |
20 |
Correct |
10 ms |
204 KB |
Output is correct |
21 |
Correct |
238 ms |
288 KB |
Output is correct |
22 |
Execution timed out |
2069 ms |
332 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |