#include<bits/stdc++.h>
#define F first
#define S second
#define MAX 2005
#define oo 1e18
#define mod 1000000007
#define fast_in ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);cout.setf(ios::fixed);cout.precision(0);
using namespace std;
typedef long long ll;
#define pll pair<ll , ll>
#define vll vector<ll>
#define vvll vector<vll>
#define vpll vector<pll>
ll tmn[5][MAX][MAX],tmx[5][MAX][MAX],n,a[MAX][MAX],m,v[MAX*MAX],d=0;
ll OK(ll x,ll z){
//cout<<x<<": ";
ll r=oo,mx=0,c=0;
for(int i=1;i<=n;i++){
while(c+1<=m && tmn[1][i][c+1]<x)c++;
mx=max(mx,tmx[0][i][c]);
}r=min(r,mx);
//cout<<mx<<" ";
mx=0;c=0;
for(int i=n;i>0;i--){
while(c+1<=m && tmn[1][i][c+1]<x)c++;
mx=max(mx,tmx[0][i][c]);
}r=min(r,mx);
//cout<<mx<<" ";
mx=0;c=m+1;
for(int i=1;i<=n;i++){
while(c-1>=0 && tmn[0][i][c-1]<x)c--;
mx=max(mx,tmx[1][i][c]);
}r=min(r,mx);
//cout<<mx<<" ";
mx=0;c=m+1;
for(int i=n;i>0;i--){
while(c-1>=0 && tmn[0][i][c-1]<x)c--;
mx=max(mx,tmx[1][i][c]);
}r=min(r,mx);
//cout<<v[0]<<" "<<v[d-1]<<"\n";
if(z==1)return max(abs(r-v[0]),abs(v[d-1]-x));
if(abs(r-v[0])>=abs(v[d-1]-x))return 0;
return 1;
}
int main(){
fast_in
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>a[i][j];
v[d]=a[i][j];d++;
// cout<<d;
}
tmx[0][i][0]=tmx[1][i][m+1]=0;
tmn[0][i][0]=tmn[1][i][m+1]=oo;
for(int j=1;j<=m;j++){
tmx[0][i][j]=max(tmx[0][i][j-1],a[i][j]);
tmn[0][i][j]=min(tmn[0][i][j-1],a[i][j]);
}
for(int j=m;j>0;j--){
tmx[1][i][j]=max(tmx[1][i][j+1],a[i][j]);
tmn[1][i][j]=min(tmn[1][i][j+1],a[i][j]);
//cout<<tmn[1][i][j]<<" ";
}//cout<<"\n";
}sort(v,v+d);//cout<<v[d-1];
if(d==1){cout<<0;return 0;}
ll p=1,f=d;//cout<<f<<" ";
while(abs(p-f)!=1){
ll md=(p+f)/2;//cout<<md<<":";
if(OK(v[md],0)==1)p=md;
else f=md;
}
cout<<min(OK(v[p],1),OK(v[f],1));
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10708 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10708 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
12632 KB |
Output is correct |
16 |
Correct |
8 ms |
29320 KB |
Output is correct |
17 |
Correct |
11 ms |
29532 KB |
Output is correct |
18 |
Correct |
11 ms |
29552 KB |
Output is correct |
19 |
Correct |
10 ms |
29532 KB |
Output is correct |
20 |
Correct |
11 ms |
25312 KB |
Output is correct |
21 |
Correct |
13 ms |
29528 KB |
Output is correct |
22 |
Correct |
10 ms |
29528 KB |
Output is correct |
23 |
Correct |
12 ms |
29976 KB |
Output is correct |
24 |
Correct |
10 ms |
25692 KB |
Output is correct |
25 |
Correct |
11 ms |
29532 KB |
Output is correct |
26 |
Correct |
11 ms |
29532 KB |
Output is correct |
27 |
Correct |
11 ms |
29532 KB |
Output is correct |
28 |
Correct |
11 ms |
29560 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
10584 KB |
Output is correct |
2 |
Correct |
2 ms |
10588 KB |
Output is correct |
3 |
Correct |
2 ms |
10588 KB |
Output is correct |
4 |
Correct |
3 ms |
10588 KB |
Output is correct |
5 |
Correct |
3 ms |
10588 KB |
Output is correct |
6 |
Correct |
2 ms |
10588 KB |
Output is correct |
7 |
Correct |
2 ms |
10588 KB |
Output is correct |
8 |
Correct |
2 ms |
10588 KB |
Output is correct |
9 |
Correct |
3 ms |
10588 KB |
Output is correct |
10 |
Correct |
2 ms |
10588 KB |
Output is correct |
11 |
Correct |
2 ms |
10840 KB |
Output is correct |
12 |
Correct |
2 ms |
10708 KB |
Output is correct |
13 |
Correct |
2 ms |
10844 KB |
Output is correct |
14 |
Correct |
2 ms |
10588 KB |
Output is correct |
15 |
Correct |
3 ms |
12632 KB |
Output is correct |
16 |
Correct |
8 ms |
29320 KB |
Output is correct |
17 |
Correct |
11 ms |
29532 KB |
Output is correct |
18 |
Correct |
11 ms |
29552 KB |
Output is correct |
19 |
Correct |
10 ms |
29532 KB |
Output is correct |
20 |
Correct |
11 ms |
25312 KB |
Output is correct |
21 |
Correct |
13 ms |
29528 KB |
Output is correct |
22 |
Correct |
10 ms |
29528 KB |
Output is correct |
23 |
Correct |
12 ms |
29976 KB |
Output is correct |
24 |
Correct |
10 ms |
25692 KB |
Output is correct |
25 |
Correct |
11 ms |
29532 KB |
Output is correct |
26 |
Correct |
11 ms |
29532 KB |
Output is correct |
27 |
Correct |
11 ms |
29532 KB |
Output is correct |
28 |
Correct |
11 ms |
29560 KB |
Output is correct |
29 |
Correct |
606 ms |
200700 KB |
Output is correct |
30 |
Correct |
498 ms |
203744 KB |
Output is correct |
31 |
Correct |
574 ms |
205312 KB |
Output is correct |
32 |
Correct |
558 ms |
205540 KB |
Output is correct |
33 |
Correct |
494 ms |
189624 KB |
Output is correct |
34 |
Correct |
558 ms |
205656 KB |
Output is correct |
35 |
Correct |
687 ms |
228092 KB |
Output is correct |
36 |
Correct |
586 ms |
221396 KB |
Output is correct |
37 |
Correct |
693 ms |
228348 KB |
Output is correct |