#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 2e9;
ll n, m; int k;
ll X[302], Y[302];
int rx[302], ry[302], sx, sy;
ll ans = LLONG_MAX;
struct Interval{
ll l, r; /// 높이 구간
ll ldist, rdist, mdist; /// 최소 길이
Interval(){}
Interval(ll l, ll r, ll ldist, ll rdist, ll mdist): l(l), r(r), ldist(ldist), rdist(rdist), mdist(mdist){}
bool operator<(const Interval &nxt)const{
return l<nxt.l;
}
};
pair<ll, int> dqL[1002], dqR[1002], dqLen[1002];
int l1, l2, r1, r2, len1, len2;
vector<Interval> info;
ll udlength[500002], udsize;
ll A[302][302], B[302][302], C[302][302];
pair<ll, ll> arr[302];
pair<ll, ll> ableGroup(ll ud, int s, int e){
ll L = max(X[e] - ud, s==1 ? -INF : X[s-1] + 1);
ll R = min(X[s], e==k ? INF : X[e+1] - ud - 1);
if(L>R) return make_pair(-INF, -INF);
return make_pair(L, R);
}
int main(){
scanf("%lld %lld %d", &n, &m, &k);
// n=m=1000000000, k=300;
for(int i=1; i<=k; i++){
scanf("%lld %lld", &arr[i].first, &arr[i].second);
// X[i] = Y[i] = i*i+i+2;
}
sort(arr+1, arr+k+1);
for(int i=1; i<=k; i++) X[i] = arr[i].first, Y[i] = arr[i].second;
for(int i=1; i<=k; i++){
vector<ll> vec (1, Y[i]);
A[i][i] = Y[i] - 1;
B[i][i] = m - Y[i];
C[i][i] = 0;
for(int j=i+1; j<=k; j++){
auto it = lower_bound(vec.begin(), vec.end(), Y[j]);
if(!(it != vec.end() && *it == Y[j])) vec.insert(it, Y[j]);
A[i][j] = vec.front() - 1;
B[i][j] = m - vec.back();
C[i][j] = 0;
for(int k=0; k<(int)vec.size()-1; k++) C[i][j] = max(C[i][j], vec[k+1] - vec[k] - 1);
}
}
udlength[udsize++] = n-1;
for(int i=1; i<=k; i++){
udlength[udsize++] = X[i] - 1;
udlength[udsize++] = n - X[i];
for(int j=1; j<=k; j++){
if(X[i] < X[j]) udlength[udsize++] = X[j] - X[i] - 1;
udlength[udsize++] = X[i] + n - X[j] - 1;
}
}
sort(udlength, udlength+udsize);
udsize = unique(udlength, udlength+udsize) - udlength;
vector<int> xls, xrs;
for(int i=1; i<=k; i++){
int j = i;
while(j<k && X[i] == X[j+1]) j++;
xls.push_back(i), xrs.push_back(j);
i=j;
}
for(int ui=0; ui<udsize; ui++){ /// 위 - 아래 간격
ll ud = udlength[ui];
if(ud<0) continue;
bool bad = 0;
for(int i=1; i<k; i++){
if(X[i+1] - X[i] - 1 > ud) {bad = 1; break;}
}
if(bad) continue;
info.clear();
int e = 0;
for(int s=0; s<(int)xls.size(); s++){
while(ableGroup(ud, xls[s], xrs[e]) == make_pair(-INF, -INF)){
e++;
}
while(1){
pair<ll, ll> p = ableGroup(ud, xls[s], xrs[e]);
info.push_back(Interval(p.first, p.second, A[xls[s]][xrs[e]], B[xls[s]][xrs[e]], C[xls[s]][xrs[e]]));
if(e+1<(int)xls.size() && ableGroup(ud, xls[s], xrs[e+1]) != make_pair(-INF, -INF)) e++;
else break;
}
while(e<=s){
e++;
}
}
int j = 0;
l1 = l2 = r1 = r2 = len1 = len2 = 0;
dqL[l2++] = make_pair(info[0].ldist, 0);
dqR[r2++] = make_pair(info[0].rdist, 0);
dqLen[len2++] = make_pair(info[0].mdist, 0);
for(int i=0; i<(int)info.size(); i++){
if(l1!=l2 && dqL[l1].second < i) l1++;
if(r1!=r2 && dqR[r1].second < i) r1++;
if(len1!=len2 && dqLen[len1].second < i) len1++;
while(j < (int)info.size() && info[j].r - info[i].l + 1 < n){
j++;
while(l1!=l2 && dqL[l2-1].first <= info[j].ldist) l2--;
dqL[l2++] = make_pair(info[j].ldist, j);
while(r1!=r2 && dqR[r2-1].first <= info[j].rdist) r2--;
dqR[r2++] = make_pair(info[j].rdist, j);
while(len1!=len2 && dqLen[len2-1].first <= info[j].mdist) len2--;
dqLen[len2++] = make_pair(info[j].mdist, j);
}
if(j == (int)info.size()) break;
ll minL = dqL[l1].first, minR = dqR[r1].first, len = dqLen[len1].first;
ans = min(ans, ud + max(len, minL+minR));
}
}
printf("%lld", ans);
}
Compilation message
cultivation.cpp: In function 'int main()':
cultivation.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%lld %lld %d", &n, &m, &k);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
43 | scanf("%lld %lld", &arr[i].first, &arr[i].second);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
2 ms |
1460 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
1620 KB |
Output is correct |
26 |
Correct |
6 ms |
3412 KB |
Output is correct |
27 |
Correct |
6 ms |
3512 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
6 ms |
3540 KB |
Output is correct |
30 |
Correct |
6 ms |
3540 KB |
Output is correct |
31 |
Correct |
6 ms |
3512 KB |
Output is correct |
32 |
Correct |
6 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
2 ms |
1460 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
1620 KB |
Output is correct |
26 |
Correct |
6 ms |
3412 KB |
Output is correct |
27 |
Correct |
6 ms |
3512 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
6 ms |
3540 KB |
Output is correct |
30 |
Correct |
6 ms |
3540 KB |
Output is correct |
31 |
Correct |
6 ms |
3512 KB |
Output is correct |
32 |
Correct |
6 ms |
3540 KB |
Output is correct |
33 |
Correct |
9 ms |
3316 KB |
Output is correct |
34 |
Correct |
9 ms |
3412 KB |
Output is correct |
35 |
Correct |
12 ms |
3500 KB |
Output is correct |
36 |
Correct |
10 ms |
3536 KB |
Output is correct |
37 |
Correct |
10 ms |
3412 KB |
Output is correct |
38 |
Correct |
10 ms |
3524 KB |
Output is correct |
39 |
Correct |
10 ms |
3412 KB |
Output is correct |
40 |
Correct |
10 ms |
3504 KB |
Output is correct |
41 |
Correct |
8 ms |
3284 KB |
Output is correct |
42 |
Correct |
9 ms |
3468 KB |
Output is correct |
43 |
Correct |
10 ms |
3412 KB |
Output is correct |
44 |
Correct |
10 ms |
3540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
0 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
1 ms |
468 KB |
Output is correct |
21 |
Correct |
37 ms |
1108 KB |
Output is correct |
22 |
Correct |
64 ms |
1108 KB |
Output is correct |
23 |
Correct |
76 ms |
1164 KB |
Output is correct |
24 |
Correct |
44 ms |
1108 KB |
Output is correct |
25 |
Correct |
67 ms |
1108 KB |
Output is correct |
26 |
Correct |
21 ms |
1108 KB |
Output is correct |
27 |
Correct |
65 ms |
1164 KB |
Output is correct |
28 |
Correct |
63 ms |
1172 KB |
Output is correct |
29 |
Correct |
55 ms |
1172 KB |
Output is correct |
30 |
Correct |
67 ms |
1164 KB |
Output is correct |
31 |
Correct |
76 ms |
1176 KB |
Output is correct |
32 |
Correct |
66 ms |
1108 KB |
Output is correct |
33 |
Correct |
59 ms |
1172 KB |
Output is correct |
34 |
Correct |
62 ms |
1164 KB |
Output is correct |
35 |
Correct |
72 ms |
1168 KB |
Output is correct |
36 |
Correct |
70 ms |
1164 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
852 KB |
Output is correct |
19 |
Correct |
1 ms |
468 KB |
Output is correct |
20 |
Correct |
0 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
724 KB |
Output is correct |
22 |
Correct |
2 ms |
1460 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
3 ms |
2132 KB |
Output is correct |
25 |
Correct |
2 ms |
1620 KB |
Output is correct |
26 |
Correct |
6 ms |
3412 KB |
Output is correct |
27 |
Correct |
6 ms |
3512 KB |
Output is correct |
28 |
Correct |
3 ms |
2132 KB |
Output is correct |
29 |
Correct |
6 ms |
3540 KB |
Output is correct |
30 |
Correct |
6 ms |
3540 KB |
Output is correct |
31 |
Correct |
6 ms |
3512 KB |
Output is correct |
32 |
Correct |
6 ms |
3540 KB |
Output is correct |
33 |
Correct |
9 ms |
3316 KB |
Output is correct |
34 |
Correct |
9 ms |
3412 KB |
Output is correct |
35 |
Correct |
12 ms |
3500 KB |
Output is correct |
36 |
Correct |
10 ms |
3536 KB |
Output is correct |
37 |
Correct |
10 ms |
3412 KB |
Output is correct |
38 |
Correct |
10 ms |
3524 KB |
Output is correct |
39 |
Correct |
10 ms |
3412 KB |
Output is correct |
40 |
Correct |
10 ms |
3504 KB |
Output is correct |
41 |
Correct |
8 ms |
3284 KB |
Output is correct |
42 |
Correct |
9 ms |
3468 KB |
Output is correct |
43 |
Correct |
10 ms |
3412 KB |
Output is correct |
44 |
Correct |
10 ms |
3540 KB |
Output is correct |
45 |
Correct |
1 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
468 KB |
Output is correct |
47 |
Correct |
1 ms |
468 KB |
Output is correct |
48 |
Correct |
1 ms |
468 KB |
Output is correct |
49 |
Correct |
2 ms |
468 KB |
Output is correct |
50 |
Correct |
1 ms |
468 KB |
Output is correct |
51 |
Correct |
0 ms |
468 KB |
Output is correct |
52 |
Correct |
1 ms |
468 KB |
Output is correct |
53 |
Correct |
1 ms |
468 KB |
Output is correct |
54 |
Correct |
2 ms |
468 KB |
Output is correct |
55 |
Correct |
1 ms |
468 KB |
Output is correct |
56 |
Correct |
2 ms |
468 KB |
Output is correct |
57 |
Correct |
1 ms |
468 KB |
Output is correct |
58 |
Correct |
1 ms |
468 KB |
Output is correct |
59 |
Correct |
1 ms |
468 KB |
Output is correct |
60 |
Correct |
2 ms |
468 KB |
Output is correct |
61 |
Correct |
1 ms |
468 KB |
Output is correct |
62 |
Correct |
1 ms |
468 KB |
Output is correct |
63 |
Correct |
1 ms |
468 KB |
Output is correct |
64 |
Correct |
1 ms |
468 KB |
Output is correct |
65 |
Correct |
37 ms |
1108 KB |
Output is correct |
66 |
Correct |
64 ms |
1108 KB |
Output is correct |
67 |
Correct |
76 ms |
1164 KB |
Output is correct |
68 |
Correct |
44 ms |
1108 KB |
Output is correct |
69 |
Correct |
67 ms |
1108 KB |
Output is correct |
70 |
Correct |
21 ms |
1108 KB |
Output is correct |
71 |
Correct |
65 ms |
1164 KB |
Output is correct |
72 |
Correct |
63 ms |
1172 KB |
Output is correct |
73 |
Correct |
55 ms |
1172 KB |
Output is correct |
74 |
Correct |
67 ms |
1164 KB |
Output is correct |
75 |
Correct |
76 ms |
1176 KB |
Output is correct |
76 |
Correct |
66 ms |
1108 KB |
Output is correct |
77 |
Correct |
59 ms |
1172 KB |
Output is correct |
78 |
Correct |
62 ms |
1164 KB |
Output is correct |
79 |
Correct |
72 ms |
1168 KB |
Output is correct |
80 |
Correct |
70 ms |
1164 KB |
Output is correct |
81 |
Correct |
687 ms |
2496 KB |
Output is correct |
82 |
Correct |
672 ms |
2516 KB |
Output is correct |
83 |
Correct |
785 ms |
2872 KB |
Output is correct |
84 |
Correct |
976 ms |
2880 KB |
Output is correct |
85 |
Correct |
1757 ms |
3588 KB |
Output is correct |
86 |
Correct |
1770 ms |
3588 KB |
Output is correct |
87 |
Correct |
1071 ms |
3576 KB |
Output is correct |
88 |
Correct |
1387 ms |
3592 KB |
Output is correct |
89 |
Correct |
1721 ms |
3588 KB |
Output is correct |
90 |
Correct |
1192 ms |
3172 KB |
Output is correct |
91 |
Correct |
1707 ms |
3660 KB |
Output is correct |
92 |
Correct |
1726 ms |
3588 KB |
Output is correct |
93 |
Correct |
1284 ms |
3584 KB |
Output is correct |
94 |
Correct |
1751 ms |
3592 KB |
Output is correct |
95 |
Correct |
1722 ms |
3588 KB |
Output is correct |
96 |
Correct |
1584 ms |
3592 KB |
Output is correct |
97 |
Correct |
1780 ms |
3596 KB |
Output is correct |
98 |
Correct |
10 ms |
3396 KB |
Output is correct |