#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vii;
typedef pair<ll,ll> pii;
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define all(v) v.begin(),v.end()
#define pow poww
const ll M=5e5+10;
const ll inf=2e18;
const ll mod=1e9+7;
ll pow(ll x,ll y){
ll res=1;
x%=mod;
while(y>0){
if((y&1))
res*=x,res%=mod;
x*=x,x%=mod;
y=(y>>1);
}
return res;
}
ll r,c,n;
pii a[M];
bool ck[41];
vii v,vv[41];
void chk(ll x,ll y,ll s){
if(v.empty())
return;
sort(all(v));
memset(ck,0,sizeof(ck));
for(auto it:v){
ll z=it;
for(int i=0;i<=x;i++){
if(z<0)
break;
ck[z]=1;
z--;
}
z=it;
for(int i=0;i<=y;i++){
if(z>r)
break;
ck[z]=1;
z++;
}
}
for(int i=1;i<=r;i++){
if(ck[i])
vv[i].pb(s);
}
}
ll solve(ll x,ll y){
v.clear();
for(int i=1;i<=r;i++)
vv[i].clear();
for(int i=0;i<n;i++){
if(i==0)
v.pb(a[i].F);
if(i>0){
if(a[i].S==a[i-1].S){
v.pb(a[i].F);
}
else{
chk(x,y,a[i-1].S);
v.clear();
v.pb(a[i].F);
}
}
}
chk(x,y,a[n-1].S);
ll w=0,e=0,ov=0;
for(int i=1;i<=r;i++){
if(vv[i].empty())
return inf;
w=max(w,vv[i][0]-1);
e=max(e,c-vv[i].back());
for(int j=1;j<vv[i].size();j++){
ov=max(ov,vv[i][j]-vv[i][j-1]-1);
}
}
return max(ov,e+w);
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int TC=1;
//cin>>TC;
while(TC--){
cin>>r>>c>>n;
for(int i=0;i<n;i++){
cin>>a[i].S>>a[i].F;
}
sort(a,a+n);
for(int i=0;i<n;i++)
swap(a[i].F,a[i].S);
ll ans=inf;
for(ll i=0;i<=50;i++){
for(ll j=0;j<=50;j++){
ans=min(ans,solve(i,j)+i+j);
}
}
cout<<ans<<'\n';
}
return 0;
}
/*
*/
Compilation message
cultivation.cpp: In function 'll solve(ll, ll)':
cultivation.cpp:81:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(int j=1;j<vv[i].size();j++){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
8 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
11 ms |
348 KB |
Output is correct |
22 |
Correct |
20 ms |
348 KB |
Output is correct |
23 |
Correct |
10 ms |
348 KB |
Output is correct |
24 |
Correct |
29 ms |
484 KB |
Output is correct |
25 |
Correct |
22 ms |
344 KB |
Output is correct |
26 |
Correct |
36 ms |
348 KB |
Output is correct |
27 |
Correct |
35 ms |
348 KB |
Output is correct |
28 |
Correct |
26 ms |
348 KB |
Output is correct |
29 |
Correct |
33 ms |
348 KB |
Output is correct |
30 |
Correct |
36 ms |
348 KB |
Output is correct |
31 |
Correct |
36 ms |
344 KB |
Output is correct |
32 |
Correct |
35 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
8 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
11 ms |
348 KB |
Output is correct |
22 |
Correct |
20 ms |
348 KB |
Output is correct |
23 |
Correct |
10 ms |
348 KB |
Output is correct |
24 |
Correct |
29 ms |
484 KB |
Output is correct |
25 |
Correct |
22 ms |
344 KB |
Output is correct |
26 |
Correct |
36 ms |
348 KB |
Output is correct |
27 |
Correct |
35 ms |
348 KB |
Output is correct |
28 |
Correct |
26 ms |
348 KB |
Output is correct |
29 |
Correct |
33 ms |
348 KB |
Output is correct |
30 |
Correct |
36 ms |
348 KB |
Output is correct |
31 |
Correct |
36 ms |
344 KB |
Output is correct |
32 |
Correct |
35 ms |
344 KB |
Output is correct |
33 |
Correct |
17 ms |
348 KB |
Output is correct |
34 |
Correct |
92 ms |
856 KB |
Output is correct |
35 |
Correct |
78 ms |
572 KB |
Output is correct |
36 |
Correct |
90 ms |
648 KB |
Output is correct |
37 |
Correct |
82 ms |
604 KB |
Output is correct |
38 |
Correct |
95 ms |
660 KB |
Output is correct |
39 |
Correct |
94 ms |
656 KB |
Output is correct |
40 |
Correct |
92 ms |
604 KB |
Output is correct |
41 |
Correct |
78 ms |
572 KB |
Output is correct |
42 |
Correct |
71 ms |
568 KB |
Output is correct |
43 |
Correct |
95 ms |
636 KB |
Output is correct |
44 |
Correct |
70 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
472 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
348 KB |
Output is correct |
18 |
Correct |
8 ms |
348 KB |
Output is correct |
19 |
Correct |
7 ms |
348 KB |
Output is correct |
20 |
Correct |
2 ms |
344 KB |
Output is correct |
21 |
Correct |
11 ms |
348 KB |
Output is correct |
22 |
Correct |
20 ms |
348 KB |
Output is correct |
23 |
Correct |
10 ms |
348 KB |
Output is correct |
24 |
Correct |
29 ms |
484 KB |
Output is correct |
25 |
Correct |
22 ms |
344 KB |
Output is correct |
26 |
Correct |
36 ms |
348 KB |
Output is correct |
27 |
Correct |
35 ms |
348 KB |
Output is correct |
28 |
Correct |
26 ms |
348 KB |
Output is correct |
29 |
Correct |
33 ms |
348 KB |
Output is correct |
30 |
Correct |
36 ms |
348 KB |
Output is correct |
31 |
Correct |
36 ms |
344 KB |
Output is correct |
32 |
Correct |
35 ms |
344 KB |
Output is correct |
33 |
Correct |
17 ms |
348 KB |
Output is correct |
34 |
Correct |
92 ms |
856 KB |
Output is correct |
35 |
Correct |
78 ms |
572 KB |
Output is correct |
36 |
Correct |
90 ms |
648 KB |
Output is correct |
37 |
Correct |
82 ms |
604 KB |
Output is correct |
38 |
Correct |
95 ms |
660 KB |
Output is correct |
39 |
Correct |
94 ms |
656 KB |
Output is correct |
40 |
Correct |
92 ms |
604 KB |
Output is correct |
41 |
Correct |
78 ms |
572 KB |
Output is correct |
42 |
Correct |
71 ms |
568 KB |
Output is correct |
43 |
Correct |
95 ms |
636 KB |
Output is correct |
44 |
Correct |
70 ms |
348 KB |
Output is correct |
45 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
46 |
Halted |
0 ms |
0 KB |
- |