#include<bits/stdc++.h>
using namespace std;
#define N 2005
#define F first
#define S second
int n,m,A[N][N],a[N][N],dp[N][N],ans=1e9;
typedef pair<int,int> pa;
pa cal(int n,int m,int v){
memset(dp,0,sizeof dp);
for(int i=1;i<=n;i++) dp[0][i] = 1;
for(int i=1;i<=m;i++) dp[i][0] = 1;
for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) if(a[i][j] <= v && dp[i-1][j] && dp[i][j-1]) dp[i][j] = 1;
int mx0=0,mx1=0,mn0=1e9,mn1=1e9;
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){
if(!dp[i][j]) mx0 = max(mx0,a[i][j]), mn0 = min(mn0,a[i][j]);
else mx1 = max(mx1,a[i][j]), mn1 = min(mn1,a[i][j]);
}
return {mx1-mn1,mx0-mn0};
}
int sol(int n,int m){
int be = 1,ed = 1e9,ans=1e9;
while(be < ed){
int mid = (be+ed)/2;
pa V = cal(n,m,mid);
ans = min(ans,max(V.F,V.S));
// printf("%d : %d %d\n",mid,V.F,V.S);
if(V.F <= V.S) be = mid+1;
else ed = mid;
}
return ans;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&A[i][j]);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j] = A[i][j];
ans = min(ans,sol(n,m));
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[j][i] = A[i][j];
ans = min(ans,sol(n,m));
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[(n-i)+1][j] = A[i][j];
ans = min(ans,sol(n,m));
for(int i=n;i>=1;i--) for(int j=m;j>=1;j--) a[i][(j-m)+1] = A[i][j];
ans = min(ans,sol(n,m));
printf("%d",ans);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:36:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joioi.cpp:37:51: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) scanf("%d",&A[i][j]);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
194 ms |
16084 KB |
Output is correct |
2 |
Incorrect |
220 ms |
16100 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
194 ms |
16084 KB |
Output is correct |
2 |
Incorrect |
220 ms |
16100 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
194 ms |
16084 KB |
Output is correct |
2 |
Incorrect |
220 ms |
16100 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |