#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define ll long long
void ckmn(ll&a,ll b){a=min(a,b);}
void ckmx(int&a,int b){a=max(a,b);}
const int N=305;
const int inf=1e9+7;
int sz[N],SWX[N][N],SWU[N][N],SWD[N][N],SWR[N][N],x[N],y[N],n,H,W;
int L[N],lsz,T[N],tsz,SM[N*N],U[N*N],D[N*N],R[N*N],ssz,esz;
pii evs[N];
void unq(int*a,int&n){sort(a+1,a+1+n);n=unique(a+1,a+1+n)-a-1;}
int main(){
scanf("%i %i %i",&H,&W,&n);
for(int i=1;i<=n;i++){
scanf("%i %i",&x[i],&y[i]);
L[++lsz]=y[i]-1;
if(y[i]>1)T[++tsz]=y[i]-1;
}
T[++tsz]=W;unq(T,tsz);
for(int i=1;i<=tsz;i++){
esz=0;
for(int j=1;j<=n;j++){
if(y[j]<=T[i]){
SM[++ssz]=T[i]-y[j];
evs[++esz]={T[i]-y[j],x[j]};
}
}
multiset<int> dif,pts;
pts.insert(0);pts.insert(H+1);
dif.insert(H+1);
sort(evs+1,evs+1+esz);
for(int j=1,k;j<=esz;j=k){
for(k=j;k<=esz&&evs[j].first==evs[k].first;k++){
auto it=pts.insert(evs[k].second);
dif.erase(dif.find(*next(it)-*prev(it)));
dif.insert(*next(it)-*it);
dif.insert(*it-*prev(it));
}
sz[i]++;
SWX[i][sz[i]]=evs[j].first;
SWU[i][sz[i]]=H-*prev(prev(pts.end()));
SWD[i][sz[i]]=*next(pts.begin())-1;
SWR[i][sz[i]]=*dif.rbegin()-1;
}
}
unq(L,lsz);
unq(SM,ssz);
ll ans=inf*2;
for(int i=lsz,j=tsz-1;i>=1;i--){
for(;j>=1&&L[i]<T[j];j--){
for(int k=1,o=1;k<=ssz;k++){
while(o<=sz[j]&&SWX[j][o]<=SM[k])o++;
if(o>1)ckmx(U[k],SWU[j][o-1]),ckmx(D[k],SWD[j][o-1]),ckmx(R[k],SWR[j][o-1]);
else U[k]=inf,D[k]=inf,R[k]=inf;
}
}
auto Try=[&](int k,int o){
int r=max(SM[k]-L[i],SWX[tsz][o]);
int _d=max(D[k],SWD[tsz][o]);
int _u=max(U[k],SWU[tsz][o]);
int _r=max(R[k],SWR[tsz][o]);
ckmn(ans,(ll)max(_r,_u+_d)+r+L[i]);
};
for(int k=1,o=1;k<=ssz;k++){
for(;o<=sz[tsz]&&SWX[tsz][o]<=SM[k]-L[i];o++)
if(k>1)Try(k-1,o);
if(o>1)Try(k,o-1);
if(k==ssz)for(;o<=sz[tsz];o++)Try(k,o);
}
}
printf("%lld\n",ans);
return 0;
}
Compilation message
cultivation.cpp: In function 'int main()':
cultivation.cpp:14:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
14 | scanf("%i %i %i",&H,&W,&n);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
cultivation.cpp:16:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
16 | scanf("%i %i",&x[i],&y[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
4 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
4 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
640 KB |
Output is correct |
33 |
Correct |
108 ms |
2680 KB |
Output is correct |
34 |
Correct |
108 ms |
2552 KB |
Output is correct |
35 |
Correct |
21 ms |
1664 KB |
Output is correct |
36 |
Correct |
114 ms |
2572 KB |
Output is correct |
37 |
Correct |
113 ms |
2560 KB |
Output is correct |
38 |
Correct |
112 ms |
2552 KB |
Output is correct |
39 |
Correct |
97 ms |
2432 KB |
Output is correct |
40 |
Correct |
119 ms |
2560 KB |
Output is correct |
41 |
Correct |
98 ms |
2552 KB |
Output is correct |
42 |
Correct |
22 ms |
1656 KB |
Output is correct |
43 |
Correct |
136 ms |
2552 KB |
Output is correct |
44 |
Correct |
29 ms |
1400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
1 ms |
512 KB |
Output is correct |
17 |
Correct |
1 ms |
512 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
512 KB |
Output is correct |
20 |
Correct |
1 ms |
512 KB |
Output is correct |
21 |
Correct |
6 ms |
896 KB |
Output is correct |
22 |
Correct |
7 ms |
896 KB |
Output is correct |
23 |
Correct |
7 ms |
896 KB |
Output is correct |
24 |
Correct |
6 ms |
896 KB |
Output is correct |
25 |
Correct |
7 ms |
896 KB |
Output is correct |
26 |
Correct |
3 ms |
640 KB |
Output is correct |
27 |
Correct |
8 ms |
896 KB |
Output is correct |
28 |
Correct |
7 ms |
896 KB |
Output is correct |
29 |
Correct |
7 ms |
896 KB |
Output is correct |
30 |
Correct |
7 ms |
896 KB |
Output is correct |
31 |
Correct |
7 ms |
896 KB |
Output is correct |
32 |
Correct |
7 ms |
896 KB |
Output is correct |
33 |
Correct |
6 ms |
896 KB |
Output is correct |
34 |
Correct |
8 ms |
896 KB |
Output is correct |
35 |
Correct |
6 ms |
896 KB |
Output is correct |
36 |
Correct |
7 ms |
896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
0 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
0 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
1 ms |
512 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
512 KB |
Output is correct |
22 |
Correct |
2 ms |
512 KB |
Output is correct |
23 |
Correct |
1 ms |
512 KB |
Output is correct |
24 |
Correct |
3 ms |
640 KB |
Output is correct |
25 |
Correct |
2 ms |
512 KB |
Output is correct |
26 |
Correct |
4 ms |
640 KB |
Output is correct |
27 |
Correct |
4 ms |
640 KB |
Output is correct |
28 |
Correct |
3 ms |
512 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
4 ms |
640 KB |
Output is correct |
32 |
Correct |
4 ms |
640 KB |
Output is correct |
33 |
Correct |
108 ms |
2680 KB |
Output is correct |
34 |
Correct |
108 ms |
2552 KB |
Output is correct |
35 |
Correct |
21 ms |
1664 KB |
Output is correct |
36 |
Correct |
114 ms |
2572 KB |
Output is correct |
37 |
Correct |
113 ms |
2560 KB |
Output is correct |
38 |
Correct |
112 ms |
2552 KB |
Output is correct |
39 |
Correct |
97 ms |
2432 KB |
Output is correct |
40 |
Correct |
119 ms |
2560 KB |
Output is correct |
41 |
Correct |
98 ms |
2552 KB |
Output is correct |
42 |
Correct |
22 ms |
1656 KB |
Output is correct |
43 |
Correct |
136 ms |
2552 KB |
Output is correct |
44 |
Correct |
29 ms |
1400 KB |
Output is correct |
45 |
Correct |
1 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
512 KB |
Output is correct |
47 |
Correct |
1 ms |
512 KB |
Output is correct |
48 |
Correct |
1 ms |
512 KB |
Output is correct |
49 |
Correct |
1 ms |
512 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
1 ms |
512 KB |
Output is correct |
52 |
Correct |
1 ms |
512 KB |
Output is correct |
53 |
Correct |
1 ms |
512 KB |
Output is correct |
54 |
Correct |
1 ms |
512 KB |
Output is correct |
55 |
Correct |
1 ms |
512 KB |
Output is correct |
56 |
Correct |
1 ms |
512 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
1 ms |
512 KB |
Output is correct |
59 |
Correct |
1 ms |
512 KB |
Output is correct |
60 |
Correct |
1 ms |
512 KB |
Output is correct |
61 |
Correct |
1 ms |
512 KB |
Output is correct |
62 |
Correct |
1 ms |
512 KB |
Output is correct |
63 |
Correct |
1 ms |
512 KB |
Output is correct |
64 |
Correct |
1 ms |
512 KB |
Output is correct |
65 |
Correct |
6 ms |
896 KB |
Output is correct |
66 |
Correct |
7 ms |
896 KB |
Output is correct |
67 |
Correct |
7 ms |
896 KB |
Output is correct |
68 |
Correct |
6 ms |
896 KB |
Output is correct |
69 |
Correct |
7 ms |
896 KB |
Output is correct |
70 |
Correct |
3 ms |
640 KB |
Output is correct |
71 |
Correct |
8 ms |
896 KB |
Output is correct |
72 |
Correct |
7 ms |
896 KB |
Output is correct |
73 |
Correct |
7 ms |
896 KB |
Output is correct |
74 |
Correct |
7 ms |
896 KB |
Output is correct |
75 |
Correct |
7 ms |
896 KB |
Output is correct |
76 |
Correct |
7 ms |
896 KB |
Output is correct |
77 |
Correct |
6 ms |
896 KB |
Output is correct |
78 |
Correct |
8 ms |
896 KB |
Output is correct |
79 |
Correct |
6 ms |
896 KB |
Output is correct |
80 |
Correct |
7 ms |
896 KB |
Output is correct |
81 |
Correct |
50 ms |
1792 KB |
Output is correct |
82 |
Correct |
49 ms |
1792 KB |
Output is correct |
83 |
Correct |
65 ms |
2048 KB |
Output is correct |
84 |
Correct |
66 ms |
2048 KB |
Output is correct |
85 |
Correct |
117 ms |
2552 KB |
Output is correct |
86 |
Correct |
114 ms |
2560 KB |
Output is correct |
87 |
Correct |
81 ms |
2176 KB |
Output is correct |
88 |
Correct |
110 ms |
2560 KB |
Output is correct |
89 |
Correct |
100 ms |
2560 KB |
Output is correct |
90 |
Correct |
81 ms |
2176 KB |
Output is correct |
91 |
Correct |
98 ms |
2296 KB |
Output is correct |
92 |
Correct |
117 ms |
2556 KB |
Output is correct |
93 |
Correct |
91 ms |
2552 KB |
Output is correct |
94 |
Correct |
90 ms |
2560 KB |
Output is correct |
95 |
Correct |
116 ms |
2552 KB |
Output is correct |
96 |
Correct |
108 ms |
2552 KB |
Output is correct |
97 |
Correct |
73 ms |
2296 KB |
Output is correct |
98 |
Correct |
32 ms |
2048 KB |
Output is correct |