#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 |
18 ms |
4216 KB |
Output is correct |
2 |
Correct |
16 ms |
4456 KB |
Output is correct |
3 |
Correct |
42 ms |
4456 KB |
Output is correct |
4 |
Correct |
38 ms |
4480 KB |
Output is correct |
5 |
Correct |
36 ms |
4532 KB |
Output is correct |
6 |
Correct |
32 ms |
4532 KB |
Output is correct |
7 |
Correct |
46 ms |
4564 KB |
Output is correct |
8 |
Correct |
38 ms |
4564 KB |
Output is correct |
9 |
Correct |
45 ms |
4564 KB |
Output is correct |
10 |
Correct |
45 ms |
4564 KB |
Output is correct |
11 |
Correct |
53 ms |
4564 KB |
Output is correct |
12 |
Correct |
54 ms |
4568 KB |
Output is correct |
13 |
Correct |
42 ms |
4568 KB |
Output is correct |
14 |
Correct |
40 ms |
4568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
4216 KB |
Output is correct |
2 |
Correct |
16 ms |
4456 KB |
Output is correct |
3 |
Correct |
42 ms |
4456 KB |
Output is correct |
4 |
Correct |
38 ms |
4480 KB |
Output is correct |
5 |
Correct |
36 ms |
4532 KB |
Output is correct |
6 |
Correct |
32 ms |
4532 KB |
Output is correct |
7 |
Correct |
46 ms |
4564 KB |
Output is correct |
8 |
Correct |
38 ms |
4564 KB |
Output is correct |
9 |
Correct |
45 ms |
4564 KB |
Output is correct |
10 |
Correct |
45 ms |
4564 KB |
Output is correct |
11 |
Correct |
53 ms |
4564 KB |
Output is correct |
12 |
Correct |
54 ms |
4568 KB |
Output is correct |
13 |
Correct |
42 ms |
4568 KB |
Output is correct |
14 |
Correct |
40 ms |
4568 KB |
Output is correct |
15 |
Correct |
18 ms |
4588 KB |
Output is correct |
16 |
Correct |
26 ms |
5356 KB |
Output is correct |
17 |
Correct |
50 ms |
5484 KB |
Output is correct |
18 |
Correct |
53 ms |
5484 KB |
Output is correct |
19 |
Correct |
44 ms |
5604 KB |
Output is correct |
20 |
Correct |
58 ms |
5604 KB |
Output is correct |
21 |
Correct |
83 ms |
5604 KB |
Output is correct |
22 |
Correct |
62 ms |
5604 KB |
Output is correct |
23 |
Correct |
56 ms |
5604 KB |
Output is correct |
24 |
Correct |
60 ms |
5604 KB |
Output is correct |
25 |
Correct |
57 ms |
5604 KB |
Output is correct |
26 |
Correct |
61 ms |
5604 KB |
Output is correct |
27 |
Correct |
89 ms |
5628 KB |
Output is correct |
28 |
Correct |
76 ms |
5628 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
4216 KB |
Output is correct |
2 |
Correct |
16 ms |
4456 KB |
Output is correct |
3 |
Correct |
42 ms |
4456 KB |
Output is correct |
4 |
Correct |
38 ms |
4480 KB |
Output is correct |
5 |
Correct |
36 ms |
4532 KB |
Output is correct |
6 |
Correct |
32 ms |
4532 KB |
Output is correct |
7 |
Correct |
46 ms |
4564 KB |
Output is correct |
8 |
Correct |
38 ms |
4564 KB |
Output is correct |
9 |
Correct |
45 ms |
4564 KB |
Output is correct |
10 |
Correct |
45 ms |
4564 KB |
Output is correct |
11 |
Correct |
53 ms |
4564 KB |
Output is correct |
12 |
Correct |
54 ms |
4568 KB |
Output is correct |
13 |
Correct |
42 ms |
4568 KB |
Output is correct |
14 |
Correct |
40 ms |
4568 KB |
Output is correct |
15 |
Correct |
18 ms |
4588 KB |
Output is correct |
16 |
Correct |
26 ms |
5356 KB |
Output is correct |
17 |
Correct |
50 ms |
5484 KB |
Output is correct |
18 |
Correct |
53 ms |
5484 KB |
Output is correct |
19 |
Correct |
44 ms |
5604 KB |
Output is correct |
20 |
Correct |
58 ms |
5604 KB |
Output is correct |
21 |
Correct |
83 ms |
5604 KB |
Output is correct |
22 |
Correct |
62 ms |
5604 KB |
Output is correct |
23 |
Correct |
56 ms |
5604 KB |
Output is correct |
24 |
Correct |
60 ms |
5604 KB |
Output is correct |
25 |
Correct |
57 ms |
5604 KB |
Output is correct |
26 |
Correct |
61 ms |
5604 KB |
Output is correct |
27 |
Correct |
89 ms |
5628 KB |
Output is correct |
28 |
Correct |
76 ms |
5628 KB |
Output is correct |
29 |
Correct |
1486 ms |
19436 KB |
Output is correct |
30 |
Correct |
1350 ms |
20220 KB |
Output is correct |
31 |
Correct |
1459 ms |
20220 KB |
Output is correct |
32 |
Correct |
1606 ms |
20228 KB |
Output is correct |
33 |
Correct |
1219 ms |
20228 KB |
Output is correct |
34 |
Correct |
1470 ms |
20348 KB |
Output is correct |
35 |
Correct |
2610 ms |
20348 KB |
Output is correct |
36 |
Correct |
1856 ms |
20348 KB |
Output is correct |
37 |
Correct |
2597 ms |
20348 KB |
Output is correct |