#include<bits/stdc++.h>
typedef long long ll;
#define inf (0x3f3f3f3f)
using namespace std;
vector<int>gx[305],gu[305],gd[305],gs[305];
int vu[90005],vd[90005],vs[90005];
vector<int>aux,aux2,v2;
int a[305],b[305];
int h,w,n,k,l,ans=inf*2;
void calc(vector<pair<int,int>>&auxi,vector<int>&x,vector<int>&u,vector<int>&d,vector<int>&s)
{
sort(auxi.begin(),auxi.end());
multiset<int> st1,st2;
st1.insert(0);
st1.insert(h+1);
st2.insert(h+1);
int i,j,n=auxi.size(),px,py,k;
for(i=0;i<n;i=j){
px=auxi[i].first;
for(j=i+1;j<n && auxi[j].first==px;j++);
for(k=i;k<j;k++){
py=auxi[k].second;
auto it=st1.insert(py);
if (st1.begin()!=it && st1.end()!=next(it))
st2.erase(st2.find(*next(it)-*prev(it)));
if (st1.begin()!=it)
st2.insert(py-*prev(it));
if (st1.end()!=next(it))
st2.insert(*next(it)-py);
}
x.push_back(px);
s.push_back(*prev(st2.end())-1);
u.push_back(*next(st1.begin())-1);
d.push_back(h-*prev(prev(st1.end())));
}
}
int main()
{
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d%d",&h,&w,&n);
int i;
for(i=1;i<=n;i++){
scanf("%d%d",&a[i],&b[i]);
aux.push_back(b[i]-1);
if (b[i]>1)
aux2.push_back(b[i]-1);
}
sort(aux.begin(),aux.end());
sort(aux2.begin(),aux2.end());
aux.erase(unique(aux.begin(),aux.end()),aux.end());
aux2.erase(unique(aux2.begin(),aux2.end()),aux2.end());
k=aux2.size();
aux2.push_back(w);
int x,j,num;
for(i=0;i<=k;i++){
x=aux2[i];
vector<pair<int,int>>auxi;
for(j=1;j<=n;j++){
num=x-b[j];
if (num>=0){
auxi.push_back(make_pair(num,a[j]));
v2.push_back(num);
}
}
calc(auxi,gx[i],gu[i],gd[i],gs[i]);
}
sort(v2.begin(),v2.end());
v2.erase(unique(v2.begin(),v2.end()),v2.end());
l=v2.size();
j=k-1;
int lx,i2,j2;
for(i=aux.size();i--;){
lx=aux[i];
for(;j>=0 && lx<aux2[j];j--){
j2=0;
int n=gx[j].size(),x;
for(i2=0;i2<l;i2++){
x=v2[i2];
if (j2<n && gx[j][j2]<=x)
j2++;
if (j2){
vu[i2]=max(vu[i2],gu[j][j2-1]);
vd[i2]=max(vd[i2],gd[j][j2-1]);
vs[i2]=max(vs[i2],gs[j][j2-1]);
}
else
vu[i2]=vd[i2]=vs[i2]=inf;
}
}
int n=gx[k].size(),rx,du,dd,ds;
for(i2=0,j2=0;i2<l || j2<n;){
rx=inf;
if (i2<l)
rx=v2[i2]-lx;
if (j2<n)
rx=min(rx,gx[k][j2]);
if (rx<0){
i2++;
continue;
}
if (i2<l && v2[i2]-lx==rx)
i2++;
if (j2<n && gx[k][j2]==rx)
j2++;
if (i2==0 || j2==0)
continue;
du=max(vu[i2-1],gu[k][j2-1]);
dd=max(vd[i2-1],gd[k][j2-1]);
ds=max(vs[i2-1],gs[k][j2-1]);
ll rez=ll(max(ds,du+dd))+lx+rx;
if (ans>rez)
ans=rez;
}
}
printf("%d\n",ans);
return 0;
}
Compilation message
cultivation.cpp: In function 'int main()':
cultivation.cpp:41:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
41 | scanf("%d%d%d",&h,&w,&n);
| ~~~~~^~~~~~~~~~~~~~~~~~~
cultivation.cpp:44:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
44 | scanf("%d%d",&a[i],&b[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
5 ms |
492 KB |
Output is correct |
27 |
Correct |
4 ms |
492 KB |
Output is correct |
28 |
Correct |
3 ms |
364 KB |
Output is correct |
29 |
Correct |
4 ms |
492 KB |
Output is correct |
30 |
Correct |
4 ms |
492 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
5 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
5 ms |
492 KB |
Output is correct |
27 |
Correct |
4 ms |
492 KB |
Output is correct |
28 |
Correct |
3 ms |
364 KB |
Output is correct |
29 |
Correct |
4 ms |
492 KB |
Output is correct |
30 |
Correct |
4 ms |
492 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
5 ms |
492 KB |
Output is correct |
33 |
Correct |
146 ms |
2280 KB |
Output is correct |
34 |
Correct |
149 ms |
2280 KB |
Output is correct |
35 |
Correct |
25 ms |
1260 KB |
Output is correct |
36 |
Correct |
150 ms |
2280 KB |
Output is correct |
37 |
Correct |
151 ms |
2408 KB |
Output is correct |
38 |
Correct |
180 ms |
2280 KB |
Output is correct |
39 |
Correct |
126 ms |
2280 KB |
Output is correct |
40 |
Correct |
150 ms |
2280 KB |
Output is correct |
41 |
Correct |
131 ms |
2408 KB |
Output is correct |
42 |
Correct |
27 ms |
1260 KB |
Output is correct |
43 |
Correct |
165 ms |
2408 KB |
Output is correct |
44 |
Correct |
36 ms |
1004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
7 ms |
640 KB |
Output is correct |
22 |
Correct |
8 ms |
620 KB |
Output is correct |
23 |
Correct |
8 ms |
620 KB |
Output is correct |
24 |
Correct |
9 ms |
620 KB |
Output is correct |
25 |
Correct |
9 ms |
620 KB |
Output is correct |
26 |
Correct |
3 ms |
492 KB |
Output is correct |
27 |
Correct |
8 ms |
620 KB |
Output is correct |
28 |
Correct |
8 ms |
620 KB |
Output is correct |
29 |
Correct |
8 ms |
620 KB |
Output is correct |
30 |
Correct |
8 ms |
620 KB |
Output is correct |
31 |
Correct |
10 ms |
620 KB |
Output is correct |
32 |
Correct |
8 ms |
620 KB |
Output is correct |
33 |
Correct |
8 ms |
620 KB |
Output is correct |
34 |
Correct |
8 ms |
620 KB |
Output is correct |
35 |
Correct |
9 ms |
620 KB |
Output is correct |
36 |
Correct |
8 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
3 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
3 ms |
384 KB |
Output is correct |
25 |
Correct |
2 ms |
364 KB |
Output is correct |
26 |
Correct |
5 ms |
492 KB |
Output is correct |
27 |
Correct |
4 ms |
492 KB |
Output is correct |
28 |
Correct |
3 ms |
364 KB |
Output is correct |
29 |
Correct |
4 ms |
492 KB |
Output is correct |
30 |
Correct |
4 ms |
492 KB |
Output is correct |
31 |
Correct |
4 ms |
492 KB |
Output is correct |
32 |
Correct |
5 ms |
492 KB |
Output is correct |
33 |
Correct |
146 ms |
2280 KB |
Output is correct |
34 |
Correct |
149 ms |
2280 KB |
Output is correct |
35 |
Correct |
25 ms |
1260 KB |
Output is correct |
36 |
Correct |
150 ms |
2280 KB |
Output is correct |
37 |
Correct |
151 ms |
2408 KB |
Output is correct |
38 |
Correct |
180 ms |
2280 KB |
Output is correct |
39 |
Correct |
126 ms |
2280 KB |
Output is correct |
40 |
Correct |
150 ms |
2280 KB |
Output is correct |
41 |
Correct |
131 ms |
2408 KB |
Output is correct |
42 |
Correct |
27 ms |
1260 KB |
Output is correct |
43 |
Correct |
165 ms |
2408 KB |
Output is correct |
44 |
Correct |
36 ms |
1004 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
364 KB |
Output is correct |
51 |
Correct |
1 ms |
364 KB |
Output is correct |
52 |
Correct |
1 ms |
364 KB |
Output is correct |
53 |
Correct |
1 ms |
364 KB |
Output is correct |
54 |
Correct |
1 ms |
364 KB |
Output is correct |
55 |
Correct |
1 ms |
364 KB |
Output is correct |
56 |
Correct |
1 ms |
364 KB |
Output is correct |
57 |
Correct |
1 ms |
364 KB |
Output is correct |
58 |
Correct |
1 ms |
364 KB |
Output is correct |
59 |
Correct |
1 ms |
364 KB |
Output is correct |
60 |
Correct |
1 ms |
364 KB |
Output is correct |
61 |
Correct |
1 ms |
364 KB |
Output is correct |
62 |
Correct |
1 ms |
364 KB |
Output is correct |
63 |
Correct |
1 ms |
364 KB |
Output is correct |
64 |
Correct |
1 ms |
364 KB |
Output is correct |
65 |
Correct |
7 ms |
640 KB |
Output is correct |
66 |
Correct |
8 ms |
620 KB |
Output is correct |
67 |
Correct |
8 ms |
620 KB |
Output is correct |
68 |
Correct |
9 ms |
620 KB |
Output is correct |
69 |
Correct |
9 ms |
620 KB |
Output is correct |
70 |
Correct |
3 ms |
492 KB |
Output is correct |
71 |
Correct |
8 ms |
620 KB |
Output is correct |
72 |
Correct |
8 ms |
620 KB |
Output is correct |
73 |
Correct |
8 ms |
620 KB |
Output is correct |
74 |
Correct |
8 ms |
620 KB |
Output is correct |
75 |
Correct |
10 ms |
620 KB |
Output is correct |
76 |
Correct |
8 ms |
620 KB |
Output is correct |
77 |
Correct |
8 ms |
620 KB |
Output is correct |
78 |
Correct |
8 ms |
620 KB |
Output is correct |
79 |
Correct |
9 ms |
620 KB |
Output is correct |
80 |
Correct |
8 ms |
620 KB |
Output is correct |
81 |
Correct |
64 ms |
1388 KB |
Output is correct |
82 |
Correct |
73 ms |
1388 KB |
Output is correct |
83 |
Correct |
103 ms |
1792 KB |
Output is correct |
84 |
Correct |
87 ms |
1644 KB |
Output is correct |
85 |
Correct |
154 ms |
2408 KB |
Output is correct |
86 |
Correct |
152 ms |
2536 KB |
Output is correct |
87 |
Correct |
107 ms |
1784 KB |
Output is correct |
88 |
Correct |
149 ms |
2408 KB |
Output is correct |
89 |
Correct |
139 ms |
2280 KB |
Output is correct |
90 |
Correct |
103 ms |
2024 KB |
Output is correct |
91 |
Correct |
132 ms |
2152 KB |
Output is correct |
92 |
Correct |
155 ms |
2336 KB |
Output is correct |
93 |
Correct |
136 ms |
2280 KB |
Output is correct |
94 |
Correct |
125 ms |
2280 KB |
Output is correct |
95 |
Correct |
156 ms |
2408 KB |
Output is correct |
96 |
Correct |
147 ms |
2408 KB |
Output is correct |
97 |
Correct |
100 ms |
2152 KB |
Output is correct |
98 |
Correct |
36 ms |
1896 KB |
Output is correct |