#include <iostream>
#include <fstream>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <iterator>
#include <ext/pb_ds/assoc_container.hpp>
#define x first
#define y second
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define bit __builtin_popcount
#define all(x) x.begin(),x.end()
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
const ll INF=1e18+123;
const ld EPS=1e-9;
const int inf=1e9+123;
const int MOD=1e9+7;
const int N=2001;
const int dx[]={0,0,1,-1};
const int dy[]={1,-1,0,0};
int a[N][N],m,n;
bool ioi[N][N];
bool check(int x){
memset(ioi,0,sizeof(ioi));
//---1
int mn=inf,mx=0,q=m;
for(int i=1;i<=n;i++){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x){
return 1;
}
//----2
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=1;i<=n;i++){
for(int j=m;j>=q;j--){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----3
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=m;
for(int i=n;i;i--){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----4
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=n;i;i--){
for(int j=m;j>=q;j--){
int curmx=max(mx,a[i][j]);
int curmn=min(mn,a[i][j]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[i][j]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----5
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=n;
for(int i=1;i<=m;i++){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----6
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=1;i<=m;i++){
for(int j=n;j>=q;j--){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----7
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=n;
for(int i=m;i;i--){
for(int j=1;j<=q;j++){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j-1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
//----8
memset(ioi,0,sizeof(ioi));
mn=inf,mx=0,q=1;
for(int i=m;i;i--){
for(int j=n;j>=q;j--){
int curmx=max(mx,a[j][i]);
int curmn=min(mn,a[j][i]);
if(curmx-curmn>x){
q=j+1;
break;
}
ioi[j][i]=1;
mx=curmx;
mn=curmn;
}
}
mn=inf,mx=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(!ioi[i][j]){
mn=min(a[i][j],mn);
mx=max(a[i][j],mx);
}
}
}
if((mx-mn)<=x)return 1;
return 0;
}
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]);
}
}
int l=0,r=1e9;
int ans=0;
while(l<=r){
int mid=(l+r)/2;
if(!check(mid)){
l=mid+1;
}
else{
r=mid-1;
ans=mid;
}
}
printf("%d",ans);
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:272:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&m);
~~~~~^~~~~~~~~~~~~~
joioi.cpp:275:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&a[i][j]);
~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
4216 KB |
Output is correct |
2 |
Correct |
17 ms |
4452 KB |
Output is correct |
3 |
Correct |
39 ms |
4452 KB |
Output is correct |
4 |
Correct |
42 ms |
4464 KB |
Output is correct |
5 |
Correct |
33 ms |
4464 KB |
Output is correct |
6 |
Correct |
34 ms |
4612 KB |
Output is correct |
7 |
Correct |
39 ms |
4612 KB |
Output is correct |
8 |
Correct |
39 ms |
4612 KB |
Output is correct |
9 |
Correct |
40 ms |
4612 KB |
Output is correct |
10 |
Correct |
50 ms |
4636 KB |
Output is correct |
11 |
Correct |
52 ms |
4660 KB |
Output is correct |
12 |
Correct |
53 ms |
4660 KB |
Output is correct |
13 |
Correct |
40 ms |
4660 KB |
Output is correct |
14 |
Correct |
40 ms |
4660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
4216 KB |
Output is correct |
2 |
Correct |
17 ms |
4452 KB |
Output is correct |
3 |
Correct |
39 ms |
4452 KB |
Output is correct |
4 |
Correct |
42 ms |
4464 KB |
Output is correct |
5 |
Correct |
33 ms |
4464 KB |
Output is correct |
6 |
Correct |
34 ms |
4612 KB |
Output is correct |
7 |
Correct |
39 ms |
4612 KB |
Output is correct |
8 |
Correct |
39 ms |
4612 KB |
Output is correct |
9 |
Correct |
40 ms |
4612 KB |
Output is correct |
10 |
Correct |
50 ms |
4636 KB |
Output is correct |
11 |
Correct |
52 ms |
4660 KB |
Output is correct |
12 |
Correct |
53 ms |
4660 KB |
Output is correct |
13 |
Correct |
40 ms |
4660 KB |
Output is correct |
14 |
Correct |
40 ms |
4660 KB |
Output is correct |
15 |
Correct |
16 ms |
4660 KB |
Output is correct |
16 |
Correct |
28 ms |
5500 KB |
Output is correct |
17 |
Correct |
46 ms |
5716 KB |
Output is correct |
18 |
Correct |
59 ms |
5900 KB |
Output is correct |
19 |
Correct |
44 ms |
6128 KB |
Output is correct |
20 |
Correct |
49 ms |
6500 KB |
Output is correct |
21 |
Correct |
83 ms |
7012 KB |
Output is correct |
22 |
Correct |
81 ms |
7140 KB |
Output is correct |
23 |
Correct |
60 ms |
7784 KB |
Output is correct |
24 |
Correct |
60 ms |
7784 KB |
Output is correct |
25 |
Correct |
52 ms |
8388 KB |
Output is correct |
26 |
Correct |
65 ms |
8888 KB |
Output is correct |
27 |
Correct |
68 ms |
9164 KB |
Output is correct |
28 |
Correct |
71 ms |
9552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
4216 KB |
Output is correct |
2 |
Correct |
17 ms |
4452 KB |
Output is correct |
3 |
Correct |
39 ms |
4452 KB |
Output is correct |
4 |
Correct |
42 ms |
4464 KB |
Output is correct |
5 |
Correct |
33 ms |
4464 KB |
Output is correct |
6 |
Correct |
34 ms |
4612 KB |
Output is correct |
7 |
Correct |
39 ms |
4612 KB |
Output is correct |
8 |
Correct |
39 ms |
4612 KB |
Output is correct |
9 |
Correct |
40 ms |
4612 KB |
Output is correct |
10 |
Correct |
50 ms |
4636 KB |
Output is correct |
11 |
Correct |
52 ms |
4660 KB |
Output is correct |
12 |
Correct |
53 ms |
4660 KB |
Output is correct |
13 |
Correct |
40 ms |
4660 KB |
Output is correct |
14 |
Correct |
40 ms |
4660 KB |
Output is correct |
15 |
Correct |
16 ms |
4660 KB |
Output is correct |
16 |
Correct |
28 ms |
5500 KB |
Output is correct |
17 |
Correct |
46 ms |
5716 KB |
Output is correct |
18 |
Correct |
59 ms |
5900 KB |
Output is correct |
19 |
Correct |
44 ms |
6128 KB |
Output is correct |
20 |
Correct |
49 ms |
6500 KB |
Output is correct |
21 |
Correct |
83 ms |
7012 KB |
Output is correct |
22 |
Correct |
81 ms |
7140 KB |
Output is correct |
23 |
Correct |
60 ms |
7784 KB |
Output is correct |
24 |
Correct |
60 ms |
7784 KB |
Output is correct |
25 |
Correct |
52 ms |
8388 KB |
Output is correct |
26 |
Correct |
65 ms |
8888 KB |
Output is correct |
27 |
Correct |
68 ms |
9164 KB |
Output is correct |
28 |
Correct |
71 ms |
9552 KB |
Output is correct |
29 |
Correct |
1617 ms |
27196 KB |
Output is correct |
30 |
Correct |
1323 ms |
49520 KB |
Output is correct |
31 |
Correct |
1413 ms |
72576 KB |
Output is correct |
32 |
Correct |
1443 ms |
95684 KB |
Output is correct |
33 |
Correct |
1351 ms |
113844 KB |
Output is correct |
34 |
Correct |
1475 ms |
138964 KB |
Output is correct |
35 |
Correct |
2287 ms |
177716 KB |
Output is correct |
36 |
Correct |
1858 ms |
211344 KB |
Output is correct |
37 |
Correct |
2364 ms |
250320 KB |
Output is correct |