#include<bits/stdc++.h>
using namespace std;
int a[2010][2010];
struct A{
int i,j,w;
bool operator<(const A&o)const{
return w>o.w;
}
};
vector<A> g;
int main(){
int i,j,n,m;
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
scanf("%d",&a[i][j]);
}
}
for(i=1;i<=n;i++){
for(j=1;j<m;j++){
g.push_back({2*i-1,2*j,abs(a[i][j+1]-a[i][j])});
}
}
for(i=1;i<n;i++){
for(j=1;j<=m;j++){
g.push_back({2*i,2*j-1,abs(a[i+1][j]-a[i][j])});
}
}
sort(g.begin(),g.end());
/*for(auto x : g){
printf("%d %d %d\n",x.i,x.j,x.w);
}*/
int l=0,r=n+m+1,mid;
while(l<r){
mid=(l+r)/2;
int ch=0,ans=0;
vector<pair<int,int>> v;
v.clear();
for(i=0;i<=mid;i++){
v.push_back({g[i].i,g[i].j});
}
sort(v.begin(),v.end());
int inc=-1,dec=1000000007;
for(auto x: v){
if(x.second>inc){
inc=x.second;
}else{
ch=1;
break;
}
}
if(ch==0) ans=1;
ch=0;
for(auto x: v){
if(x.second<dec){
dec=x.second;
}else{
ch=1;
break;
}
}
if(ch==0) ans=1;
if(ans==1) l=mid+1;
else r=mid;
}
printf("%d\n",g[l].w);
return 0;
}
/*
6 8
9 8 8 7 9 8 8 6
1 3 9 6 8 3 10 6
10 5 10 7 4 4 3 8
2 3 7 9 8 1 9 9
4 10 10 8 2 7 9 3
6 6 7 7 2 8 1 7
*/
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:13:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
13 | scanf("%d %d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~
joioi.cpp:16:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d",&a[i][j]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |