#include<bits/stdc++.h>
using namespace std;
#define N 2005
#define F first
#define S second
int n,m,b[N][N],a[N][N],dp[N][N],ans=1e9;
typedef pair<int,int> pa;
bool cal(int n,int m,int v){
for(int i=0;i<=n;i++) for(int j=0;j<=m;j++) dp[i][j] = 0;
for(int i=0;i<=n;i++) dp[i][0] = 1;
for(int i=0;i<=m;i++) dp[0][i] = 1;
for(int i=1;i<=n;i++)for(int j=1;a[i][j] <=v && dp[i-1][j] && dp[i][j-1];j++) dp[i][j]=true;
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]);
}
if(!dp[1][1]) return 0;
if(dp[n][m]) return 1;
ans = min(ans,max(mx1-mn1,mx0-mn0));
if(mn1 >= mn0) return 0;
return mx1-mn1 > mx0-mn0;
}
void sol(int n,int m){
int be = 1,ed = 1e9;
while(be < ed){
int mid = (be+ed)/2;
if(cal(n,m,mid)) ed = mid;
else be = mid+1;
}
}
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]);
sol(n,m);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j] = a[i][j];
for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) a[i][j] = b[n-j+1][i];
sol(m,n);
for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) b[i][j] = a[i][j];
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) a[i][j] = b[m-j+1][i];
sol(n,m);
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) b[i][j] = a[i][j];
for(int i=1;i<=m;i++) for(int j=1;j<=n;j++) a[i][j] = b[n-j+1][i];
sol(m,n);
printf("%d",ans);
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:37:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
joioi.cpp:38:51: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | 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 |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
2932 KB |
Output is correct |
16 |
Correct |
18 ms |
3100 KB |
Output is correct |
17 |
Correct |
16 ms |
3204 KB |
Output is correct |
18 |
Correct |
15 ms |
3204 KB |
Output is correct |
19 |
Correct |
15 ms |
3208 KB |
Output is correct |
20 |
Correct |
16 ms |
3156 KB |
Output is correct |
21 |
Correct |
20 ms |
3204 KB |
Output is correct |
22 |
Correct |
21 ms |
3164 KB |
Output is correct |
23 |
Correct |
19 ms |
3208 KB |
Output is correct |
24 |
Correct |
17 ms |
3096 KB |
Output is correct |
25 |
Correct |
15 ms |
3132 KB |
Output is correct |
26 |
Correct |
15 ms |
3208 KB |
Output is correct |
27 |
Correct |
15 ms |
3156 KB |
Output is correct |
28 |
Correct |
15 ms |
3220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
5 ms |
2932 KB |
Output is correct |
16 |
Correct |
18 ms |
3100 KB |
Output is correct |
17 |
Correct |
16 ms |
3204 KB |
Output is correct |
18 |
Correct |
15 ms |
3204 KB |
Output is correct |
19 |
Correct |
15 ms |
3208 KB |
Output is correct |
20 |
Correct |
16 ms |
3156 KB |
Output is correct |
21 |
Correct |
20 ms |
3204 KB |
Output is correct |
22 |
Correct |
21 ms |
3164 KB |
Output is correct |
23 |
Correct |
19 ms |
3208 KB |
Output is correct |
24 |
Correct |
17 ms |
3096 KB |
Output is correct |
25 |
Correct |
15 ms |
3132 KB |
Output is correct |
26 |
Correct |
15 ms |
3208 KB |
Output is correct |
27 |
Correct |
15 ms |
3156 KB |
Output is correct |
28 |
Correct |
15 ms |
3220 KB |
Output is correct |
29 |
Correct |
1592 ms |
47384 KB |
Output is correct |
30 |
Correct |
1524 ms |
47408 KB |
Output is correct |
31 |
Correct |
1568 ms |
47472 KB |
Output is correct |
32 |
Correct |
1642 ms |
47452 KB |
Output is correct |
33 |
Correct |
1449 ms |
47192 KB |
Output is correct |
34 |
Correct |
1602 ms |
47380 KB |
Output is correct |
35 |
Correct |
1554 ms |
47384 KB |
Output is correct |
36 |
Correct |
1307 ms |
47100 KB |
Output is correct |
37 |
Correct |
1540 ms |
47384 KB |
Output is correct |