#include<bits/stdc++.h>
#define F first
#define S second
#define MAX 3000
#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]={},a[MAX][MAX]={},m,n,c=0;
ll v[5000006];map<ll,ll> mp;
ll OK(ll x,ll z){
ll cl=-1,mx=0,mn=oo;
for(int i=0;i<n;i++){
while(cl+1<m && tmn[0][i][cl+1]<x)cl++;
if(cl!=-1)mx=max(mx,tmx[1][i][cl]);
}
mn=min(mn,mx);cl=m;mx=0;
for(int i=0;i<n;i++){
while(cl-1>=0 && tmn[1][i][cl-1]<x)cl--;
mx=max(mx,tmx[0][i][cl]);
}
mn=min(mn,mx);cl=-1;mx=0;
for(int i=n-1;i>=0;i--){
while(cl+1<m && tmn[0][i][cl+1]<x)cl++;
if(cl!=-1)mx=max(mx,tmx[1][i][cl]);
}
mn=min(mn,mx);cl=m;mx=0;
for(int i=n-1;i>=0;i--){
while(cl-1>=0 && tmn[1][i][cl-1]<x)cl--;
mx=max(mx,tmx[0][i][cl]);
}
mn=min(mn,mx);
if(z==1)return max(abs(v[0]-mn),abs(v[c-1]-x));
if(abs(v[0]-mn)<=abs(v[c-1]-x))return 1;
return 0;
}
int main(){
fast_in
cin>>n>>m;
for(int i=0;i<n;i++){
tmn[0][i][m]=tmn[1][i][0]=oo;
tmx[0][i][m]=tmx[1][i][0]=0;
for(int j=0;j<m;j++){
cin>>a[i][j];
if(mp[a[i][j]]==0){
mp[a[i][j]]=1;
v[c]=a[i][j];
c++;
}
tmx[1][i][j]=max(tmx[1][i][j],a[i][j]);
tmx[1][i][j+1]=tmx[1][i][j];
tmn[1][i][j]=min(tmn[1][i][j],a[i][j]);
tmn[1][i][j+1]=tmn[1][i][j];
}
for(int j=m-1;j>=0;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]);
}
}
sort(v,v+c);
if(c==1){cout<<0;return 0;}
ll p=1,f=c;
while(abs(f-p)!=1){
ll md=(p+f)/2;
if(md<0 || md>=c)while(true){md=md;}
if(OK(v[md],0)==1)p=md;
else f=md;
}
if(f==c){cout<<OK(v[p],1);return 0;}
cout<<min(OK(v[p],1),OK(v[f],1));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14804 KB |
Output is correct |
6 |
Correct |
2 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14804 KB |
Output is correct |
6 |
Correct |
2 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
6 ms |
35164 KB |
Output is correct |
17 |
Correct |
21 ms |
39796 KB |
Output is correct |
18 |
Correct |
29 ms |
39948 KB |
Output is correct |
19 |
Correct |
22 ms |
40028 KB |
Output is correct |
20 |
Correct |
19 ms |
37468 KB |
Output is correct |
21 |
Correct |
28 ms |
40020 KB |
Output is correct |
22 |
Correct |
25 ms |
40020 KB |
Output is correct |
23 |
Correct |
26 ms |
39972 KB |
Output is correct |
24 |
Correct |
20 ms |
37724 KB |
Output is correct |
25 |
Correct |
22 ms |
40028 KB |
Output is correct |
26 |
Correct |
22 ms |
40028 KB |
Output is correct |
27 |
Correct |
23 ms |
40024 KB |
Output is correct |
28 |
Correct |
22 ms |
40028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14684 KB |
Output is correct |
2 |
Correct |
2 ms |
14684 KB |
Output is correct |
3 |
Correct |
2 ms |
14684 KB |
Output is correct |
4 |
Correct |
2 ms |
14684 KB |
Output is correct |
5 |
Correct |
2 ms |
14804 KB |
Output is correct |
6 |
Correct |
2 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14684 KB |
Output is correct |
11 |
Correct |
2 ms |
14684 KB |
Output is correct |
12 |
Correct |
2 ms |
14684 KB |
Output is correct |
13 |
Correct |
2 ms |
14684 KB |
Output is correct |
14 |
Correct |
2 ms |
14684 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
6 ms |
35164 KB |
Output is correct |
17 |
Correct |
21 ms |
39796 KB |
Output is correct |
18 |
Correct |
29 ms |
39948 KB |
Output is correct |
19 |
Correct |
22 ms |
40028 KB |
Output is correct |
20 |
Correct |
19 ms |
37468 KB |
Output is correct |
21 |
Correct |
28 ms |
40020 KB |
Output is correct |
22 |
Correct |
25 ms |
40020 KB |
Output is correct |
23 |
Correct |
26 ms |
39972 KB |
Output is correct |
24 |
Correct |
20 ms |
37724 KB |
Output is correct |
25 |
Correct |
22 ms |
40028 KB |
Output is correct |
26 |
Correct |
22 ms |
40028 KB |
Output is correct |
27 |
Correct |
23 ms |
40024 KB |
Output is correct |
28 |
Correct |
22 ms |
40028 KB |
Output is correct |
29 |
Correct |
1355 ms |
226972 KB |
Output is correct |
30 |
Correct |
1224 ms |
233296 KB |
Output is correct |
31 |
Correct |
1265 ms |
240212 KB |
Output is correct |
32 |
Correct |
1305 ms |
237092 KB |
Output is correct |
33 |
Correct |
1014 ms |
215260 KB |
Output is correct |
34 |
Correct |
1311 ms |
240376 KB |
Output is correct |
35 |
Runtime error |
2643 ms |
262144 KB |
Execution killed with signal 9 |
36 |
Halted |
0 ms |
0 KB |
- |